生成个人的 Github PR List

写了一个小脚本,可以生成任意用户的 pr list

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
from collections import defaultdict

import requests

USERNAME = 'hunshcn'
TARGET = 'source/about/index.md'

def generate_contributing_table(username, target):
url = f'https://api.github.com/search/issues?per_page=100&q=is%3Apr+author%3A{username}+archived%3Afalse+is%3Aclosed+is%3Amerged+is%3Apublic+type%3Apr&page='
data = []
page = 1
while True:
r = requests.get(url + str(page)).json()
total = r['total_count']
data.extend(r['items'])
if total <= page * 100:
break
page += 1

repos = defaultdict(list)

for item in data:
repo = item['repository_url'].removeprefix('https://api.github.com/repos/')
number = item['number']
repos[repo].append(number)

items = sorted(repos.items(), key=lambda x: len(x[1]), reverse=True)

# Generate markdown table
table_lines = [
"| # | 仓库 | PR |",
"|:---|:---|:---|"
]

for index, (repo, numbers) in enumerate(items, 1):
pr_links = [f"[#{n}](https://github.com/{repo}/pull/{n})" for n in numbers]
pr_list = " ".join(pr_links)
table_lines.append(f"| {index} | [{repo}](https://github.com/{repo}) | {pr_list} |")

# Read the index.md file
with open(target, 'r', encoding='utf-8') as f:
content = f.read()

# Replace the table block
start_marker = "<!-- Contributing block start -->\n"
end_marker = "\n<!-- Contributing block end -->"
table_content = "\n".join(table_lines)
start_index = content.find(start_marker)
end_index = content.find(end_marker)
assert start_index != -1 and end_index != -1, "Contributing block not found"
new_content = content[:start_index+len(start_marker)] + table_content + content[end_index:]

# Write back to the file
with open(target, 'w', encoding='utf-8') as f:
f.write(new_content)

print("done")

if __name__ == "__main__":
generate_contributing_table(USERNAME, TARGET)

这个主要是为了我的 about 页面而写的,会将 pr list 作为 table 写入指定的 markdown 中,只需要在 markdown 中准备一个 Contributing block

1
2
<!-- Contributing block start -->
<!-- Contributing block end -->

脚本就会自动填充,效果可以见 我的 aboout


生成个人的 Github PR List
https://hunsh.net/20250518/生成个人的-Github-PR-List/
发布于
2025年5月18日
许可协议