forked from firebase/firebase-ios-sdk
- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake_release_notes.py
executable file
·287 lines (216 loc) · 7.6 KB
/
make_release_notes.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
#!/usr/bin/env python
# Copyright 2019 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Converts github flavored markdown changelogs to release notes.
"""
importargparse
importre
importsubprocess
importstring
importsix
NO_HEADING='PRODUCT HAS NO HEADING'
PRODUCTS= {
'FirebaseABTesting/CHANGELOG.md': '{{ab_testing}}',
'FirebaseAppDistribution/CHANGELOG.md': 'App Distribution',
'FirebaseAuth/CHANGELOG.md': '{{auth}}',
'FirebaseCore/CHANGELOG.md': NO_HEADING,
'Crashlytics/CHANGELOG.md': '{{crashlytics}}',
'FirebaseDatabase/CHANGELOG.md': '{{database}}',
'FirebaseDynamicLinks/CHANGELOG.md': '{{ddls}}',
'FirebaseInAppMessaging/CHANGELOG.md': '{{inapp_messaging}}',
'FirebaseInstallations/CHANGELOG.md': 'Installations',
'Firebase/InstanceID/CHANGELOG.md': 'InstanceID',
'FirebaseMessaging/CHANGELOG.md': '{{messaging}}',
'FirebaseStorage/CHANGELOG.md': '{{storage}}',
'Firestore/CHANGELOG.md': '{{firestore}}',
'Functions/CHANGELOG.md': '{{cloud_functions}}',
'FirebaseRemoteConfig/CHANGELOG.md': '{{remote_config}}',
# 'GoogleDataTransport/CHANGELOG.md': '?',
}
defmain():
local_repo=find_local_repo()
parser=argparse.ArgumentParser(description='Create release notes.')
parser.add_argument('--repo', '-r', default=local_repo,
help='Specify which GitHub repo is local.')
parser.add_argument('--only', metavar='VERSION',
help='Convert only a specific version')
parser.add_argument('--all', action='store_true',
help='Emits entries for all versions')
parser.add_argument('changelog',
help='The CHANGELOG.md file to parse')
args=parser.parse_args()
ifargs.all:
text=read_file(args.changelog)
else:
text=read_changelog_section(args.changelog, args.only)
product=None
ifnotargs.all:
product=PRODUCTS.get(args.changelog)
renderer=Renderer(args.repo, product)
translator=Translator(renderer)
result=translator.translate(text)
print(result)
deffind_local_repo():
url=six.ensure_text(
subprocess.check_output(['git', 'config', '--get', 'remote.origin.url']))
# ssh or https style URL
m=re.match(r'^(?:git@github\.com:|https://github\.com/)(.*)\.git$', url)
ifm:
returnm.group(1)
raiseLookupError('Can\'t figure local repo from remote URL %s'%url)
CHANGE_TYPE_MAPPING= {
'added': 'feature'
}
classRenderer(object):
def__init__(self, local_repo, product):
self.local_repo=local_repo
self.product=product
defheading(self, heading):
ifself.product:
ifself.product==NO_HEADING:
return''
else:
return'### %s\n'%self.product
returnheading
defbullet(self, spacing):
"""Renders a bullet in a list.
All bulleted lists in devsite are '*' style.
"""
return'%s* '%spacing
defchange_type(self, tag):
"""Renders a change type tag as the appropriate double-braced macro.
That is "[fixed]" is rendered as "{{fixed}}".
"""
tag=CHANGE_TYPE_MAPPING.get(tag, tag)
return'{{%s}}'%tag
defurl(self, url):
m=re.match(r'^(?:https:)?(//github.com/(.*)/issues/(\d+))$', url)
ifm:
link=m.group(1)
repo=m.group(2)
issue=m.group(3)
ifrepo==self.local_repo:
text='#'+issue
else:
text=repo+'#'+issue
return'[%s](%s)'% (text, link)
returnurl
deflocal_issue_link(self, issues):
"""Renders a local issue link as a proper markdown URL.
Transforms (#1234, #1235) into
([#1234](//github.com/firebase/firebase-ios-sdk/issues/1234),
[#1235](//github.com/firebase/firebase-ios-sdk/issues/1235)).
"""
issue_link_list= []
issue_list=issues.split(", ")
forissueinissue_list:
issue=issue.translate(None, string.punctuation)
link='//github.com/%s/issues/%s'% (self.local_repo, issue)
issue_link_list.append('[#%s](%s)'% (issue, link))
return"("+", ".join(issue_link_list) +")"
deftext(self, text):
"""Passes through any other text."""
returntext
classTranslator(object):
def__init__(self, renderer):
self.renderer=renderer
deftranslate(self, text):
result=''
whiletext:
forkeyinself.rules:
rule=getattr(self, key)
m=rule.match(text)
ifnotm:
continue
callback=getattr(self, 'parse_'+key)
callback_result=callback(m)
result+=callback_result
text=text[len(m.group(0)):]
break
returnresult
heading=re.compile(
r'^#{1,6} .*'
)
defparse_heading(self, m):
returnself.renderer.heading(m.group(0))
bullet=re.compile(
r'^(\s*)[*+-] '
)
defparse_bullet(self, m):
returnself.renderer.bullet(m.group(1))
change_type=re.compile(
r'\['# opening square bracket
r'(\w+)'# tag word (like "feature" or "changed")
r'\]'# closing square bracket
r'(?!\()'# not followed by opening paren (that would be a link)
)
defparse_change_type(self, m):
returnself.renderer.change_type(m.group(1))
url=re.compile(r'^(https?://[^\s<]+[^<.,:;"\')\]\s])')
defparse_url(self, m):
returnself.renderer.url(m.group(1))
local_issue_link=re.compile(
r'\('# opening paren
r'(#(\d+)(, )?)+'# list of hash and issue number, comma-delimited
r'\)'# closing paren
)
defparse_local_issue_link(self, m):
returnself.renderer.local_issue_link(m.group(0))
text=re.compile(
r'^[\s\S]+?(?=[(\[\n]|https?://|$)'
)
defparse_text(self, m):
returnself.renderer.text(m.group(0))
rules= [
'heading', 'bullet', 'change_type', 'url', 'local_issue_link', 'text'
]
defread_file(filename):
"""Reads the contents of the file as a single string."""
withopen(filename, 'r') asfd:
returnfd.read()
defread_changelog_section(filename, single_version=None):
"""Reads a single section of the changelog from the given filename.
If single_version is None, reads the first section with a number in its
heading. Otherwise, reads the first section with single_version in its
heading.
Args:
- single_version: specifies a string to look for in headings.
Returns:
A string containing the heading and contents of the heading.
"""
withopen(filename, 'r') asfd:
# Discard all lines until we see a heading that either has the version the
# user asked for or any version.
ifsingle_version:
initial_heading=re.compile(r'^#{1,6} .*%s'%re.escape(single_version))
else:
initial_heading=re.compile(r'^#{1,6} ([^\d]*)\d')
heading=re.compile(r'^#{1,6} ')
initial=True
result= []
forlineinfd:
ifinitial:
ifinitial_heading.match(line):
initial=False
result.append(line)
else:
ifheading.match(line):
break
result.append(line)
# Prune extra newlines
whileresultandresult[-1] =='\n':
result.pop()
return''.join(result)
if__name__=='__main__':
main()