forked from MystenLabs/sui
-
Notifications
You must be signed in to change notification settings - Fork 0
/
release_notes.py
executable file
·288 lines (220 loc) · 7.11 KB
/
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
#!/usr/bin/env python3
# Copyright (c) Mysten Labs, Inc.
# SPDX-License-Identifier: Apache-2.0
import argparse
from collections import defaultdict
import json
import os
import re
import subprocess
import sys
from typing import NamedTuple
RE_NUM = re.compile("[0-9_]+")
RE_PR = re.compile(
r"^.*\(#(\d+)\)$",
re.MULTILINE,
)
RE_HEADING = re.compile(
r"#+ Release notes(.*)",
re.DOTALL | re.IGNORECASE,
)
RE_CHECK = re.compile(
r"^\s*-\s*\[.\]",
re.MULTILINE,
)
RE_NOTE = re.compile(
r"^\s*-\s*\[( |x)?\]\s*([^:]+):",
re.MULTILINE | re.IGNORECASE,
)
# Only commits that affect changes in these directories will be
# considered when generating release notes.
INTERESTING_DIRECTORIES = [
"crates",
"dashboards",
"doc",
"docker",
"external-crates",
"kiosk",
"narwhal",
"nre",
"sui-execution",
]
# Start release notes with these sections, if they contain relevant
# information (helps us keep a consistent order for impact areas we
# know about).
NOTE_ORDER = [
"Protocol",
"Nodes (Validators and Full nodes)",
"Indexer",
"JSON-RPC",
"GraphQL",
"CLI",
"Rust SDK",
]
class Note(NamedTuple):
checked: bool
note: str
def parse_args():
"""Parse command line arguments."""
parser = argparse.ArgumentParser(
description=(
"Extract release notes from git commits. Check help for the "
"`generate` and `check` subcommands for more information."
),
)
sub_parser = parser.add_subparsers(dest="command")
generate_p = sub_parser.add_parser(
"generate",
description="Generate release notes from git commits.",
)
generate_p.add_argument(
"from",
help="The commit to start from (exclusive)",
)
generate_p.add_argument(
"to",
nargs="?",
default="HEAD",
help="The commit to end at (inclusive), defaults to HEAD.",
)
check_p = sub_parser.add_parser(
"check",
description=(
"Check if the release notes section of a givne commit is complete, "
"i.e. that every impacted component has a non-empty note."
),
)
check_p.add_argument(
"commit",
nargs="?",
default="HEAD",
help="The commit to check, defaults to HEAD.",
)
return vars(parser.parse_args())
def git(*args):
"""Run a git command and return the output as a string."""
return subprocess.check_output(["git"] + list(args)).decode().strip()
def extract_notes(commit):
"""Get release notes from a commit message.
Find the 'Release notes' section in the commit message, and
extract the notes for each impacted area (area that has been
ticked).
Returns a tuple of the PR number and a dictionary of impacted
areas mapped to their release note. Each release note indicates
whether it has a note and whether it was checked (ticked).
"""
message = git("show", "-s", "--format=%B", commit)
# Extract PR number
match = RE_PR.match(message)
pr = match.group(1) if match else None
result = {}
# Find the release notes section
match = RE_HEADING.search(message)
if not match:
return pr, result
start = 0
notes = match.group(1)
while True:
# Find the next possible release note
match = RE_NOTE.search(notes, start)
if not match:
break
checked = match.group(1)
impacted = match.group(2)
begin = match.end()
# Find the end of the note, or the end of the commit
match = RE_CHECK.search(notes, begin)
end = match.start() if match else len(notes)
result[impacted] = Note(
checked = checked in 'xX',
note = notes[begin:end].strip(),
)
start = end
return pr, result
def extract_protocol_version(commit):
"""Find the max protocol version at this commit.
Assumes that it is being called from the root of the sui repository."""
for line in git("show", f"{commit}:crates/sui-protocol-config/src/lib.rs").splitlines():
if "const MAX_PROTOCOL_VERSION" not in line:
continue
_, _, assign = line.partition("=")
if not assign:
continue
match = RE_NUM.search(assign)
if not match:
continue
return match[0]
def print_changelog(pr, log):
if pr:
print(f"https://github.com/MystenLabs/sui/pull/{pr}:")
print(log)
def do_check(commit):
"""Check if the release notes section of a given commit is complete.
This means that every impacted component has a non-empty note,
every note is attached to a checked checkbox, and every impact
area is known.
"""
_, notes = extract_notes(commit)
issues = []
for impacted, note in notes.items():
if impacted not in NOTE_ORDER:
issues.append(f" - Found unfamiliar impact area '{impacted}'.")
if note.checked and not note.note:
issues.append(f" - '{impacted}' is checked but has no release note.")
if not note.checked and note.note:
issues.append(f" - '{impacted}' has a release note but is not checked: {note.note}")
if not issues:
return
print(f"Found issues with release notes in {commit}:")
for issue in issues:
print(issue)
sys.exit(1)
def do_generate(from_, to):
"""Generate release notes from git commits.
This will extract the release notes from all commits between
`from_` (exclusive) and `to` (inclusive), and print out a markdown
snippet with a heading for each impact area that has a note,
followed by a list of its relevant changelog.
Only looks for commits affecting INTERESTING_DIRECTORIES.
Additionally injects the current protocol version into the
"Protocol" changelog.
"""
results = defaultdict(list)
root = git("rev-parse", "--show-toplevel")
os.chdir(root)
protocol_version = extract_protocol_version(to) or "XX"
commits = git(
"log", "--pretty=format:%H",
f"{from_}..{to}",
"--", *INTERESTING_DIRECTORIES,
).strip();
if not commits:
return
for commit in commits.split("\n"):
pr, notes = extract_notes(commit)
for impacted, note in notes.items():
if note.checked:
results[impacted].append((pr, note.note))
# Print the impact areas we know about first
for impacted in NOTE_ORDER:
notes = results.pop(impacted, None)
if not notes:
continue
print(f"## {impacted}")
if impacted == "Protocol":
print(f"Sui Protocol Version in this release: {protocol_version}")
print()
for pr, note in reversed(notes):
print_changelog(pr, note)
print()
# Print any remaining impact areas
for impacted, notes in results.items():
print(f"## {impacted}\n")
for pr, note in reversed(notes):
print_changelog(pr, note)
print()
args = parse_args()
if args["command"]== "generate":
do_generate(args["from"], args["to"])
elif args["command"] == "check":
do_check(args["commit"])