3
3
import subprocess
4
4
import sys
5
5
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
+
6
19
7
20
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
+ )
9
25
10
26
11
27
def github_name (username ):
12
28
# url = f"https://api.github.com/users/{username}"
13
29
# response = urlopen(url)
14
- if username == "renovate" :
15
- return "Renovate Bot"
30
+ if username in BOTS :
31
+ return BOTS [ username ]
16
32
try :
17
33
response = subprocess .check_output (
18
34
[
@@ -26,7 +42,7 @@ def github_name(username):
26
42
]
27
43
)
28
44
data = json .loads (response )
29
- return data ["name" ]
45
+ return data ["name" ] or username
30
46
except Exception as e :
31
47
print ("An error occurred:" , str (e ))
32
48
@@ -42,16 +58,7 @@ def read_file(file_name):
42
58
43
59
44
60
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 )
55
62
sys .exit (1 )
56
63
57
64
@@ -72,11 +79,11 @@ def main():
72
79
footer = ""
73
80
if not content :
74
81
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 )
80
87
print (f"{ content } \n { footer } " )
81
88
else :
82
89
help ()
0 commit comments