-
Notifications
You must be signed in to change notification settings - Fork 20
/
changelog.py
executable file
·296 lines (234 loc) · 8.94 KB
/
changelog.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
#!/usr/bin/env python3
import argparse
import datetime
import os
import re
import urllib.request
from dataclasses import dataclass
from typing import Set, List, Optional, Dict, TextIO
# https://github.com/PyGithub/PyGithub
from github import Github
from github.Milestone import Milestone
from github.Repository import Repository
PLUGIN_REPO = "intellij-rust/intellij-rust"
MAINTAINERS = [
"matklad",
"Undin",
"vlad20012",
"mchernyavsky",
"ortem",
"amakeev",
"MarinaKalashina",
"dima74",
"avrong",
"lancelote",
"ozkriff",
"mili-l",
"neonaot",
"yopox",
"konrad-sz",
"wbars",
"missingdays",
"intellij-rust-bot",
"dependabot[bot]"
]
@dataclass
class ChangelogItem:
pull_request_id: int
description: str
username: str
def display(self):
if self.username in MAINTAINERS:
return "* [#{}] {}"\
.format(self.pull_request_id, self.description)
else:
return "* [#{}] {} (by [@{}])"\
.format(self.pull_request_id, self.description, self.username)
class ChangelogSection(object):
header: str
items: List[ChangelogItem]
def __init__(self, header: str):
self.header = header
self.items = []
def add_item(self, item: ChangelogItem):
self.items.append(item)
def display(self):
return """## {}\n\n""".format(self.header) + "\n\n".join(map(lambda l: l.display(), self.items))
class Changelog(object):
labels: List[str]
sections: Dict[str, ChangelogSection]
contributors: Set[str]
milestone_id: Optional[int]
def __init__(self, milestone_id=None):
self.milestone_id = milestone_id
self.labels = []
self.sections = {}
self.contributors = set()
self.__add_section("feature", ChangelogSection("New Features"))
self.__add_section("performance", ChangelogSection("Performance Improvements"))
self.__add_section("fix", ChangelogSection("Fixes"))
self.__add_section("internal", ChangelogSection("Internal Improvements"))
def __add_section(self, label: str, section: ChangelogSection):
self.labels.append(label)
self.sections[label] = section
def add_item(self, label: str, item: ChangelogItem):
section = self.sections.get(label)
if section is not None:
section.add_item(item)
if item.username not in MAINTAINERS:
self.contributors.add(item.username)
def write(self, f: TextIO):
for label in self.labels:
section = self.sections[label]
if section.items:
f.write(section.display())
f.write("\n\n")
if self.milestone_id is not None:
f.write("Full set of changes can be found [here]"
f"(https://github.com/{PLUGIN_REPO}/milestone/{self.milestone_id}?closed=1)\n")
if len(self.contributors) > 0:
f.write("\n")
sorted_contributors = sorted(self.contributors)
for name in sorted_contributors:
url = contributor_url(name)
f.write(url)
if len(self.labels) > 0:
pull_request_ids = set()
for label in self.labels:
section = self.sections[label]
for item in section.items:
pull_request_ids.add(item.pull_request_id)
f.write("\n")
for pull_request_id in sorted(pull_request_ids):
f.write(f"[#{pull_request_id}]: https://github.com/{PLUGIN_REPO}/pull/{pull_request_id}\n")
def collect_changelog(repo: Repository, milestone: Milestone):
print(f"Collecting changelog issues for `{milestone.title}` milestone")
changelog = Changelog(milestone.number)
issues = repo.get_issues(milestone=milestone, state="all")
comment_pattern = re.compile("<!--.*-->", re.RegexFlag.DOTALL)
changelog_description_pattern = re.compile("[Cc]hangelog:\\s*(?P<description>([^\n]+\n?)*)")
for issue in issues:
if issue.pull_request is None:
continue
labels: Set[str] = set(map(lambda l: l.name, issue.labels))
if len(labels) == 0:
continue
if issue.body is not None:
issue_text = re.sub(comment_pattern, "", issue.body).replace("\r\n", "\n")
else:
issue_text = ""
result = re.search(changelog_description_pattern, issue_text)
if result is not None:
description = result.group("description").strip().rstrip(".")
else:
description = issue.title
changelog_item = ChangelogItem(issue.number, description, issue.user.login)
for label in labels:
changelog.add_item(label, changelog_item)
return changelog
def main():
parser = argparse.ArgumentParser()
parser.add_argument("-c", action='store_true', help="add contributor links")
parser.add_argument("--offline", action='store_true', help="do not preform net requests")
parser.add_argument("--token", type=str, help="github token")
parser.add_argument("--login", type=str, help="github login")
parser.add_argument("--password", type=str, help="github password")
parser.add_argument("--list", type=int, nargs=2, metavar=('first', 'last'),
help="collect external contributors for all releases between first and last")
args = parser.parse_args()
if args.c:
contributors()
elif args.list:
contributor_list(args)
else:
new_post(args)
def construct_repo(args: argparse.Namespace) -> Repository:
if args.token is not None:
login_or_token = args.token
password = None
else:
login_or_token = args.login
password = args.password
g = Github(login_or_token, password)
return g.get_repo(PLUGIN_REPO)
def new_post(args: argparse.Namespace):
next_post = len(os.listdir("_posts"))
date = datetime.datetime.now()
changelog = Changelog()
if not args.offline:
expected_milestone_title = f"v{next_post}"
repo: Repository = construct_repo(args)
milestone: Optional[Milestone] = None
for m in repo.get_milestones():
if m.title == expected_milestone_title:
milestone = m
break
if milestone is None:
raise RuntimeError(f"Milestone `{expected_milestone_title}` is not found")
due_on = milestone.due_on
if due_on is not None:
date = due_on.replace(hour=13, minute=0, second=0) # 13:00 is our desired release time
changelog = collect_changelog(repo, milestone)
name = "_posts/{}-changelog-{}.markdown".format(date.date().isoformat(), next_post)
with open(name, 'w') as f:
f.write("""---
layout: post
title: "IntelliJ Rust Changelog #{}"
date: {}
---
""".format(next_post, date.strftime("%Y-%m-%d %H:%M:%S +0300")))
changelog.write(f)
def contributors():
last_post = "_posts/" + sorted(os.listdir("_posts"))[-1]
with open(last_post) as f:
text = f.read()
names = sorted({n[2:-1] for n in re.findall(r"\[@[^]]+]", text)})
with open(last_post) as f:
old_text = f.read()
with open(last_post, 'a') as f:
f.write("\n")
for name in names:
line = contributor_url(name)
if line not in old_text:
f.write(line)
def contributor_url(username: str):
url = "https://github.com/" + username
print("checking " + url)
req = urllib.request.Request(url, method="HEAD")
with urllib.request.urlopen(req) as _:
pass # will thrown on 404
line = "[@{}]: {}\n".format(username, url)
return line
def contributor_list(args: argparse.Namespace) -> None:
if args.list is None:
raise ValueError("list flag should be set")
first = args.list[0]
last = args.list[1]
if first >= last:
raise ValueError("`first` should be less than `last`")
repo = construct_repo(args)
milestones = repo.get_milestones(state="all", sort="due_on")
versions = {f"v{i}" for i in range(first, last + 1)}
contributors = set()
for milestone in milestones:
if milestone.title in versions:
milestone_contributors = collect_contributors(repo, milestone)
print(f"Milestone {milestone.title}: {len(milestone_contributors)} external contributors")
contributors.update(milestone_contributors)
print()
for c in sorted(contributors):
print(c)
def collect_contributors(repo: Repository, milestone: Milestone) -> Set[str]:
milestone_contributors = set()
issues = repo.get_issues(milestone=milestone, state="all")
for issue in issues:
user = issue.user
login = user.login
user_name = user.name
if user_name is None:
user_name = login
if login not in MAINTAINERS:
milestone_contributors.add(f"{user_name} (https://github.com/{login})")
return milestone_contributors
if __name__ == '__main__':
main()