forked from PrefectHQ/server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
update_changelog.py
149 lines (123 loc) · 4.28 KB
/
update_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
import argparse
import datetime
import glob
import os
import sys
import yaml
SKIPLINES = 1
SECTIONS = [
("feature", "Features"),
("enhancement", "Enhancements"),
("fix", "Fixes"),
("deprecation", "Deprecations"),
("breaking", "Breaking Changes"),
("migration", "Database Migrations"),
("contributor", "Contributors"),
]
DEDUPLICATE_SECTIONS = ["contributor"]
TEMPLATE = """\
## {version} <Badge text="beta" type="success" />
Released on {date}.
{sections}
"""
REPO_DIR = os.path.abspath(os.path.dirname(__file__))
CHANGELOG_PATH = os.path.join(REPO_DIR, "CHANGELOG.md")
CHANGES_DIR = os.path.join(REPO_DIR, "changes")
def get_change_files():
change_files = sorted(glob.glob(os.path.join(CHANGES_DIR, "*.yaml")))
change_files.extend(sorted(glob.glob(os.path.join(CHANGES_DIR, "*.yml"))))
change_files = [p for p in change_files if not p.endswith("EXAMPLE.yaml")]
return change_files
def bad_entry(path):
return ValueError(
f"{path!r} does not contain a valid changelog entry, see the "
f"`changes/README.md` file for more information."
)
def generate_new_section(version):
# Load changes
changes = {s: [] for s, _ in SECTIONS}
for path in get_change_files():
with open(path) as f:
try:
data = yaml.safe_load(f)
except Exception:
raise ValueError(
f"{path!r} does not contain valid YAML - perhaps you forgot to quote a string?"
)
if not isinstance(data, dict):
raise bad_entry(path)
for k, v in data.items():
if k in changes:
if isinstance(v, list) and all(isinstance(i, str) for i in v):
changes[k].extend(v)
else:
raise bad_entry(path)
else:
raise bad_entry(path)
# Build up subsections
sections = []
for name, header in SECTIONS:
values = changes[name]
if name in DEDUPLICATE_SECTIONS:
values = sorted(set(values))
if values:
text = "\n".join("- %s" % v for v in values)
sections.append(f"### {header}\n\n{text}")
# Build new release section
date = "{dt:%B} {dt.day}, {dt:%Y}".format(dt=datetime.date.today())
return TEMPLATE.format(version=version, date=date, sections="\n\n".join(sections))
def lint():
try:
generate_new_section("Upcoming Release")
except Exception as exc:
print("Linting the `changes/` directory failed:\n")
print(exc)
sys.exit(1)
else:
print("All `changes/` files are in excellent condition!")
def preview():
print(generate_new_section("Upcoming Release"))
def generate(version):
new = generate_new_section(version)
# Insert new section in existing changelog
with open(CHANGELOG_PATH) as f:
existing = f.readlines()
head = existing[:SKIPLINES]
tail = existing[SKIPLINES:]
with open(CHANGELOG_PATH, "w") as f:
f.writelines(head)
f.write("\n")
f.write(new)
f.writelines(tail)
# Remove change files that were added
for path in get_change_files():
os.remove(path)
def main():
parser = argparse.ArgumentParser(description="Update the server changelog")
subparser = parser.add_subparsers(metavar="command", dest="command")
subparser.required = True
preview_parser = subparser.add_parser(
"preview", help="Preview just the new section of of the changelog"
)
preview_parser.set_defaults(command="preview")
generate_parser = subparser.add_parser(
"generate",
help="Generate the changelog for a release, and cleanup the `changes` directory",
)
generate_parser.set_defaults(command="generate")
generate_parser.add_argument(
"version", help="The version number to name this release section"
)
lint_parser = subparser.add_parser(
"lint", help="Ensure the changelog entries are all valid"
)
lint_parser.set_defaults(command="lint")
args = parser.parse_args()
if args.command == "generate":
generate(args.version)
elif args.command == "preview":
preview()
elif args.command == "lint":
lint()
if __name__ == "__main__":
main()