|
4 | 4 | import json |
5 | 5 | import urllib.request |
6 | 6 |
|
7 | | -parser = argparse.ArgumentParser( |
8 | | - description='Fetch a list of contributors for a given GitHub repo.' |
9 | | -) |
10 | | -parser.add_argument( |
11 | | - '--repo', |
12 | | - required=True, |
13 | | - help='GitHub Project/Repo name. (e.g. "PixarAnimationStudios/OpenTimelineIO")' |
14 | | -) |
15 | | -parser.add_argument( |
16 | | - '--token', |
17 | | - required=True, |
18 | | - help='GitHub personal access token, used for authorization.' |
19 | | - ' Get one here: https://github.com/settings/tokens/new' |
20 | | -) |
21 | | -args = parser.parse_args() |
22 | | - |
23 | | -# Note: un-authenticated requests have a strict rate limit. |
24 | | -# We avoid this by using authentication for all our requests, |
25 | | -# even the ones that don't need it. |
26 | | -# |
27 | | -# You can fetch your limits with this API: |
28 | | -# |
29 | | -# request = urllib.request.Request( |
30 | | -# "https://api.github.com/rate_limit", |
31 | | -# headers = {"Authorization": "token {}".format(token)} |
32 | | -# ) |
33 | | -# response = urllib.request.urlopen(request).read().decode('utf-8') |
34 | | -# print("Rate limit: {}".format(response)) |
35 | | - |
36 | | -request = urllib.request.Request( |
37 | | - "https://api.github.com/repos/{}/stats/contributors".format(args.repo), |
38 | | - headers={"Authorization": "token {}".format(args.token)} |
39 | | -) |
40 | | -response = urllib.request.urlopen(request).read().decode('utf-8') |
41 | | - |
42 | | -contributors = json.loads(response) |
43 | | - |
44 | | -output_lines = [] |
45 | | - |
46 | | -for contributor in contributors: |
47 | | - |
48 | | - login = contributor['author']['login'] |
49 | | - url = contributor['author']['html_url'] |
| 7 | + |
| 8 | +def parse_args(): |
| 9 | + parser = argparse.ArgumentParser( |
| 10 | + description='Fetch a list of contributors for a given GitHub repo.' |
| 11 | + ) |
| 12 | + parser.add_argument( |
| 13 | + '--repo', |
| 14 | + required=True, |
| 15 | + help='GitHub Project/Repo name. (e.g. "PixarAnimationStudios/OpenTimelineIO")' |
| 16 | + ) |
| 17 | + parser.add_argument( |
| 18 | + '--token', |
| 19 | + required=True, |
| 20 | + help='GitHub personal access token, used for authorization.' |
| 21 | + ' Get one here: https://github.com/settings/tokens/new' |
| 22 | + ) |
| 23 | + return parser.parse_args() |
| 24 | + |
| 25 | + |
| 26 | +def main(): |
| 27 | + args = parse_args() |
| 28 | + |
| 29 | + # Note: un-authenticated requests have a strict rate limit. |
| 30 | + # We avoid this by using authentication for all our requests, |
| 31 | + # even the ones that don't need it. |
| 32 | + # |
| 33 | + # You can fetch your limits with this API: |
| 34 | + # |
| 35 | + # request = urllib.request.Request( |
| 36 | + # "https://api.github.com/rate_limit", |
| 37 | + # headers = {"Authorization": "token {}".format(token)} |
| 38 | + # ) |
| 39 | + # response = urllib.request.urlopen(request).read().decode('utf-8') |
| 40 | + # print("Rate limit: {}".format(response)) |
50 | 41 |
|
51 | 42 | request = urllib.request.Request( |
52 | | - "https://api.github.com/users/{}".format(login), |
| 43 | + "https://api.github.com/repos/{}/stats/contributors".format(args.repo), |
53 | 44 | headers={"Authorization": "token {}".format(args.token)} |
54 | 45 | ) |
55 | 46 | response = urllib.request.urlopen(request).read().decode('utf-8') |
56 | 47 |
|
57 | | - user = json.loads(response) |
58 | | - name = user['name'] or "?" |
| 48 | + contributors = json.loads(response) |
| 49 | + |
| 50 | + output_lines = [] |
| 51 | + |
| 52 | + if not contributors: |
| 53 | + print("No contributors found, something likely went wrong.") |
| 54 | + |
| 55 | + for contributor in contributors: |
| 56 | + |
| 57 | + login = contributor['author']['login'] |
| 58 | + url = contributor['author']['html_url'] |
| 59 | + |
| 60 | + request = urllib.request.Request( |
| 61 | + "https://api.github.com/users/{}".format(login), |
| 62 | + headers={"Authorization": "token {}".format(args.token)} |
| 63 | + ) |
| 64 | + response = urllib.request.urlopen(request).read().decode('utf-8') |
| 65 | + |
| 66 | + user = json.loads(response) |
| 67 | + name = user['name'] or "?" |
| 68 | + |
| 69 | + # Print the output in markdown format |
| 70 | + output_lines.append("* {} ([{}]({}))".format(name, login, url)) |
| 71 | + |
| 72 | + print("\n".join(sorted(output_lines, key=str.casefold))) |
59 | 73 |
|
60 | | - # Print the output in markdown format |
61 | | - output_lines.append("* {} ([{}]({}))".format(name, login, url)) |
62 | 74 |
|
63 | | -print("\n".join(sorted(output_lines, key=str.casefold))) |
| 75 | +if __name__ == '__main__': |
| 76 | + main() |
0 commit comments