-
Notifications
You must be signed in to change notification settings - Fork 0
/
aggregate_github_issues.py
executable file
·64 lines (51 loc) · 2.03 KB
/
aggregate_github_issues.py
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
61
62
63
64
#!/usr/bin/env python3
import argparse
import json
import os
import sys
from collections import defaultdict
import itertools
import requests
REPOS_API = 'https://api.github.com/repos/'
def aggregate_issues(repo, token, session):
issues = []
pulls = []
page = 1
label_counts = defaultdict(lambda: [0, 0]) # {label_name: [issue_count, pull_count]}
labels = {}
while True:
print('fetching page %s' % page, file=sys.stderr)
res = session.get(REPOS_API + repo + '/issues', params=dict(per_page=100, page=page, q='is:issue'),
headers={'Authorization': 'token ' + token})
res.raise_for_status()
results = res.json()
if len(results) == 0:
break
for result in res.json():
issue = dict(num=result['number'], title=result['title'], labels=[l['name'] for l in result['labels']])
# /issues of the GitHub v3 API returns both issues and pulls
is_pull = 'pull_request' in result
if is_pull:
pulls.append(issue)
else:
issues.append(issue)
for label in result['labels']:
labels[label['name']] = dict(name=label['name'], description=label['description'])
label_counts[label['name']][is_pull] += 1
page += 1
sorted_labels = []
for name, (issue_count, pull_count) in sorted(label_counts.items(), key = lambda x: x[0].lower()):
labels[name]['issueCount'] = issue_count
labels[name]['pullCount'] = pull_count
sorted_labels.append(labels[name])
return dict(
repo = repo,
labels = sorted_labels,
issues = sorted(issues, key = lambda x: x['title'].lower()),
pulls = sorted(pulls, key = lambda x: x['title'].lower())
)
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument('repo', help='e.g. gittenburg/instant-issues')
args = parser.parse_args()
print(json.dumps(aggregate_issues(args.repo, os.environ['TOKEN'], requests.Session())))