-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathchangelog.py
executable file
·73 lines (57 loc) · 1.97 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
#!/usr/bin/env python3
import subprocess
import re
from pathlib import Path
root_path = Path(__file__).parent
def get_prev_version():
"""
>>> len(get_prev_version().splitlines())
1
"""
return (root_path / "dp_wizard" / "VERSION").read_text().strip()
def log_until(match): # pragma: no cover
lines = subprocess.check_output(["git", "log", "--oneline"], text=True).splitlines()
if match is None:
return lines
head = []
for line in lines:
if match in line:
break
head.append(line)
return head
def parse_log(lines):
"""
>>> print(parse_log([
... 'abcd0000 bar! (#2)',
... 'abcd0001 foo? (#1)',
... ]))
- bar! [#2](https://github.com/opendp/dp-wizard/pull/2)
- foo? [#1](https://github.com/opendp/dp-wizard/pull/1)
"""
output_lines = []
for line in lines:
line = re.sub(r"^\w+\s+", "", line) # Remove hash
line = re.sub(r"^\([^)]+\)\s+", "", line) # Remove tag
line = re.sub(
r"\(#(\d+)\)", r"[#\1](https://github.com/opendp/dp-wizard/pull/\1)", line
)
output_lines.append(f"- {line}")
return "\n".join(output_lines)
def main(): # pragma: no cover
old_changelog_lines = (root_path / "CHANGELOG.md").read_text().splitlines()
new_changelog_lines = []
prev_version = get_prev_version()
# Nothing in the changelog says "v0.0.1"
# but I'll remember on the next release!
# For now, just hardcode the name of the last PR in v0.0.1.
# TODO: Replace this with "prev_version".
log_lines = log_until("add badge")
changelog_update = parse_log(log_lines)
for line in old_changelog_lines:
if prev_version in line:
new_changelog_lines.append(changelog_update)
new_changelog_lines.append("")
new_changelog_lines.append(line)
(root_path / "CHANGELOG.md").write_text("\n".join(new_changelog_lines))
if __name__ == "__main__": # pragma: no cover
main()