Skip to content

Commit f0cfa9f

Browse files
committed
feat: introduce changelog_helper script
1 parent 562c173 commit f0cfa9f

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

ci/changelog_helper.py

+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import json
2+
import re
3+
import subprocess
4+
import sys
5+
6+
7+
def extract_usernames(text):
8+
return sorted(set(re.findall(r"@([\w-]+)", text)), key=str.casefold)
9+
10+
11+
def github_name(username):
12+
# url = f"https://api.github.com/users/{username}"
13+
# response = urlopen(url)
14+
if username == "renovate":
15+
return "Renovate Bot"
16+
try:
17+
response = subprocess.check_output(
18+
[
19+
"gh",
20+
"api",
21+
"-H",
22+
"Accept: application/vnd.github+json",
23+
"-H",
24+
"X-GitHub-Api-Version: 2022-11-28",
25+
f"/users/{username}",
26+
]
27+
)
28+
data = json.loads(response)
29+
return data["name"]
30+
except Exception as e:
31+
print("An error occurred:", str(e))
32+
33+
34+
def read_file(file_name):
35+
try:
36+
with open(file_name, "r") as file:
37+
return file.read()
38+
except FileNotFoundError:
39+
print("File not found")
40+
except Exception as e:
41+
print("An error occurred:", str(e))
42+
43+
44+
def help():
45+
print("Usage:")
46+
print(" python changelog_helper.py usernames GITHUB_GENERATED_CHANGELOG")
47+
print(" python changelog_helper.py replace-nums CHANGELOG_MARKDOWN")
48+
print()
49+
print(
50+
"A logged-in GitHub CLI (https://cli.github.com) is required for the `usernames` subcommand"
51+
)
52+
print(
53+
"For a GitHub-generated changelog, see https://github.com/rust-lang/rustup/releases/new"
54+
)
55+
sys.exit(1)
56+
57+
58+
def main():
59+
if len(sys.argv) < 3:
60+
help()
61+
62+
_, subcmd, file_name = sys.argv[:3]
63+
64+
if subcmd == "usernames":
65+
content = read_file(file_name)
66+
if not content:
67+
return
68+
for username in extract_usernames(content):
69+
print(f"- {github_name(username)} ({username})")
70+
elif subcmd == "replace-nums":
71+
content = read_file(file_name)
72+
footer = ""
73+
if not content:
74+
return
75+
for match in re.findall(r"(?<=#)(\d+)", content):
76+
# Replace issue number with fully-qualified link
77+
link = f"[pr#{match}]"
78+
footer += f"{link}: https://github.com/rust-lang/rustup/pull/{match}\n"
79+
content = content.replace(f"#{match}", link)
80+
print(f"{content}\n{footer}")
81+
else:
82+
help()
83+
84+
85+
if __name__ == "__main__":
86+
main()

0 commit comments

Comments
 (0)