Skip to content

Commit b8ea048

Browse files
committed
feat(dist): improve changelog_helper script
1 parent 09e3e83 commit b8ea048

File tree

1 file changed

+26
-19
lines changed

1 file changed

+26
-19
lines changed

ci/changelog_helper.py

+26-19
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,32 @@
33
import subprocess
44
import sys
55

6+
USAGE = """
7+
Usage:
8+
python changelog_helper.py replace-nums CHANGELOG_MARKDOWN
9+
Replace Rustup PR numbers or links with `[pr#1234]`, moving the actual links to the bottom
10+
11+
python changelog_helper.py usernames GITHUB_GENERATED_CHANGELOG
12+
Generate a Markdown list of contributors to be pasted below the line `Thanks go to:`
13+
A logged-in GitHub CLI (https://cli.github.com) is required for this subcommand
14+
For a GitHub-generated changelog, see https://github.com/rust-lang/rustup/releases/new
15+
"""
16+
17+
BOTS = {"renovate": "Renovate Bot"}
18+
619

720
def extract_usernames(text):
8-
return sorted(set(re.findall(r"@([\w-]+)", text)), key=str.casefold)
21+
return sorted(
22+
set(re.findall(r"@([\w-]+)", text)),
23+
key=lambda name: (name in BOTS, str.casefold(name)),
24+
)
925

1026

1127
def github_name(username):
1228
# url = f"https://api.github.com/users/{username}"
1329
# response = urlopen(url)
14-
if username == "renovate":
15-
return "Renovate Bot"
30+
if username in BOTS:
31+
return BOTS[username]
1632
try:
1733
response = subprocess.check_output(
1834
[
@@ -26,7 +42,7 @@ def github_name(username):
2642
]
2743
)
2844
data = json.loads(response)
29-
return data["name"]
45+
return data["name"] or username
3046
except Exception as e:
3147
print("An error occurred:", str(e))
3248

@@ -42,16 +58,7 @@ def read_file(file_name):
4258

4359

4460
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-
)
61+
print(USAGE)
5562
sys.exit(1)
5663

5764

@@ -72,11 +79,11 @@ def main():
7279
footer = ""
7380
if not content:
7481
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)
82+
issue_pat = r"(#|https://github\.com/rust-lang/rustup/pull/)(\d+)"
83+
for prefix, num in re.findall(issue_pat, content):
84+
link = f"[pr#{num}]"
85+
footer += f"{link}: https://github.com/rust-lang/rustup/pull/{num}\n"
86+
content = content.replace(prefix + num, link)
8087
print(f"{content}\n{footer}")
8188
else:
8289
help()

0 commit comments

Comments
 (0)