-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathgenerate_release_notes.py
362 lines (301 loc) · 10.3 KB
/
generate_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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
"""Generate the release notes automatically from GitHub pull requests.
1. Clone napari/napari locally.
2. Install the requirements (PyGitHub and tqdm) with
```
python -m pip install -r requirements.txt
```
3. Start with:
```
export GH_TOKEN=<your-gh-api-token>
```
(you don't need to select any permissions when creating this token.)
Then, to include everything set for the a chosen milestone:
```
python generate_release_notes.py <milestone> --target-directory=/path/to/docs/release/
```
To include a PR that has not been merged, use the `--with-pr` option:
```
python generate_release_notes.py <milestone> --target-directory=/path/to/docs/release/ --with-pr=org/repo#pr_number
```
To substitute GitHub handles for author names, use the `--correction-file` option:
```
python generate_release_notes.py <milestone> --target-directory=/path/to/docs/release/ --correction-file /path/to/name_corrections.yaml
```
References:
- https://github.com/scikit-image/scikit-image/blob/main/tools/generate_release_notes.py
- https://github.com/scikit-image/scikit-image/issues/3404
- https://github.com/scikit-image/scikit-image/issues/3405
"""
import argparse
import re
import sys
from pathlib import Path
from typing import NamedTuple
from github.PullRequest import PullRequest
from github.Repository import Repository
from release_utils import (
BOT_LIST,
GH,
GH_DOCS_REPO,
GH_REPO,
GH_USER,
REPO_DIR_NAME,
get_correction_dict,
get_corrections_from_citation_cff,
get_repo,
iter_pull_request,
setup_cache,
)
LOCAL_DIR = Path(__file__).parent
PR_REGEXP = re.compile(r'(?P<user>[\w-]+)/(?P<repo>[\w-]+)#(?P<pr>\d+)')
class PRInfo(NamedTuple):
user: str
repo: str
pr: int
def parse_pr_num(pr_num):
if match := PR_REGEXP.match(pr_num):
return PRInfo(
match.group('user'), match.group('repo'), int(match.group('pr'))
)
try:
return int(pr_num)
except ValueError:
raise argparse.ArgumentTypeError(f'{pr_num} is not a valid PR number.')
parser = argparse.ArgumentParser(usage=__doc__)
parser.add_argument('milestone', help='The milestone to list')
parser.add_argument('--target-directory', type=Path, default=None)
parser.add_argument(
'--correction-file',
help='The file with the corrections',
default=LOCAL_DIR / 'name_corrections.yaml',
)
parser.add_argument(
'--with-pr',
help='Include PR numbers for not merged PRs',
type=parse_pr_num,
default=None,
nargs='+',
)
args = parser.parse_args()
setup_cache()
repo = get_repo()
correction_dict = get_correction_dict(
args.correction_file
) | get_corrections_from_citation_cff(
LOCAL_DIR / REPO_DIR_NAME / 'CITATION.cff'
)
def add_to_users(users_dkt, new_user):
if new_user.login in users_dkt:
# reduce obsolete requests to GitHub API
return
if new_user.login in correction_dict:
users_dkt[new_user.login] = correction_dict[new_user.login]
elif new_user.name is None:
users_dkt[new_user.login] = new_user.login
else:
users_dkt[new_user.login] = new_user.name
authors = set()
committers = set()
docs_authors = set()
docs_committers = set()
reviewers = set()
docs_reviewers = set()
users = {}
highlights = {
'Highlights': {},
'New Features': {},
'Improvements': {},
'Performance': {},
'Bug Fixes': {},
'API Changes': {},
'Deprecations': {},
'Build Tools': {},
'Documentation': {},
}
other_pull_requests = {}
label_to_section = {
'bug': 'Bug Fixes',
'bugfix': 'Bug Fixes',
'feature': 'New Features',
'api': 'API Changes',
'highlight': 'Highlights',
'performance': 'Performance',
'enhancement': 'Improvements',
'deprecation': 'Deprecations',
'dependencies': 'Build Tools',
'documentation': 'Documentation',
}
def parse_pull(pull: PullRequest, repo_: Repository = repo):
# assert pull.merged or pull.number in args.with_pr
commit = repo_.get_commit(pull.merge_commit_sha)
if commit.committer is not None:
add_to_users(users, commit.committer)
committers.add(commit.committer.login)
if commit.author is not None:
add_to_users(users, commit.author)
authors.add(commit.author.login)
summary = pull.title
for review in pull.get_reviews():
if review.user is not None:
add_to_users(users, review.user)
reviewers.add(review.user.login)
assigned_to_section = False
pr_labels = {label.name.lower() for label in pull.labels}
for label_name, section in label_to_section.items():
if label_name in pr_labels:
highlights[section][pull.number] = {
'summary': summary,
'repo': repo_.full_name.split('/')[1],
}
assigned_to_section = True
if not assigned_to_section:
other_pull_requests[pull.number] = {
'summary': summary,
'repo': repo_.full_name.split('/')[1],
}
for pull_ in iter_pull_request(f'milestone:{args.milestone} is:merged'):
parse_pull(pull_)
if args.with_pr is not None:
for pr_num in args.with_pr:
if isinstance(pr_num, int):
pull = repo.get_pull(pr_num)
r = repo
else:
r = get_repo(pr_num.user, pr_num.repo)
pull = r.get_pull(pr_num.pr)
parse_pull(pull, r)
for pull in iter_pull_request(
f'milestone:{args.milestone} is:merged', repo=GH_DOCS_REPO
):
issue = pull.as_issue()
assert pull.merged
add_to_users(users, issue.user)
docs_authors.add(issue.user.login)
summary = pull.title
for review in pull.get_reviews():
if review.user is not None:
add_to_users(users, review.user)
docs_reviewers.add(review.user.login)
assigned_to_section = False
pr_labels = {label.name.lower() for label in pull.labels}
if 'maintenance' in pr_labels:
other_pull_requests[pull.number] = {
'summary': summary,
'repo': GH_DOCS_REPO,
}
else:
highlights['Documentation'][pull.number] = {
'summary': summary,
'repo': GH_DOCS_REPO,
}
# add Other PRs to the ordered dict to make doc generation easier.
highlights['Other Pull Requests'] = other_pull_requests
# remove these bots.
committers -= BOT_LIST
authors -= BOT_LIST
docs_committers -= BOT_LIST
docs_authors -= BOT_LIST
user_name_pattern = re.compile(r'@([\w-]+)') # pattern for GitHub usernames
pr_number_pattern = re.compile(r'#(\d+)') # pattern for GitHub PR numbers
old_contributors = set()
if args.target_directory is None:
file_handle = sys.stdout
else:
res_file_name = f"release_{args.milestone.replace('.', '_')}.md"
file_handle = open(args.target_directory / res_file_name, 'w')
for file_path in args.target_directory.glob('release_*.md'):
if file_path.name == res_file_name:
continue
with open(file_path) as f:
old_contributors.update(user_name_pattern.findall(f.read()))
# Now generate the release notes
title = f'# napari {args.milestone}'
print(title, file=file_handle)
notes_dir = LOCAL_DIR / 'additional_notes' / args.milestone
if not notes_dir.glob('*.md'):
print(
'There is no prepared sections in the additional_notes directory.',
file=sys.stderr,
)
if (fn := notes_dir / 'header.md').exists():
intro = fn.open().read()
else:
intro = f"""
We're happy to announce the release of napari {args.milestone}!
napari is a fast, interactive, multi-dimensional image viewer for Python.
It's designed for browsing, annotating, and analyzing large multi-dimensional
images. It's built on top of Qt (for the GUI), vispy (for performant GPU-based
rendering), and the scientific Python stack (numpy, scipy).
For more information, examples, and documentation, please visit our website,
https://napari.org.
"""
print(intro, file=file_handle)
for section, pull_request_dicts in highlights.items():
if not pull_request_dicts:
continue
print(f'## {section}\n', file=file_handle)
section_path = (
LOCAL_DIR
/ 'additional_notes'
/ args.milestone
/ f'{section.lower()}.md'
)
mentioned_pr = set()
if section_path.exists():
with section_path.open() as f:
text = f.read()
for pr_number in pr_number_pattern.findall(text):
mentioned_pr.add(int(pr_number))
print(text, file=file_handle)
for number, pull_request_info in pull_request_dicts.items():
if number in mentioned_pr:
continue
repo_str = pull_request_info['repo']
repo_prefix = repo_str if repo_str != 'napari' else ''
print(
f'- {pull_request_info["summary"]} ([{repo_prefix}#{number}]'
f"(https://{GH}/{GH_USER}/{repo_str}/pull/{number}))",
file=file_handle,
)
print('', file=file_handle)
contributors = {
'authors': authors | docs_authors,
'reviewers': reviewers | docs_reviewers,
}
# ignore committers
# contributors['committers'] = committers
new_contributors = (authors | docs_authors) - old_contributors
for section_name, contributor_set in contributors.items():
print('', file=file_handle)
if None in contributor_set:
contributor_set.remove(None)
committer_str = (
f'## {len(contributor_set)} {section_name} added to this '
'release (alphabetical)'
)
print(committer_str, file=file_handle)
print('', file=file_handle)
print('(+) denotes first-time contributors 🥳', file=file_handle)
print('', file=file_handle)
for c in sorted(contributor_set, key=lambda x: users[x].lower()):
if c in authors and c in docs_authors:
first_repo_name = GH_REPO
second_repo_str = (
f' ([docs](https://{GH}/{GH_USER}/'
f'{GH_DOCS_REPO}/commits?author={c})) '
)
elif c in authors:
first_repo_name = GH_REPO
second_repo_str = ''
else: # docs only
first_repo_name = GH_DOCS_REPO
second_repo_str = ''
first = ' +' if c in new_contributors else ''
commit_link = (
f'https://{GH}/{GH_USER}/{first_repo_name}/' f'commits?author={c}'
)
print(
f'- [{users[c]}]({commit_link}){second_repo_str} - @{c}{first}',
file=file_handle,
)
print('', file=file_handle)