-
Notifications
You must be signed in to change notification settings - Fork 0
/
closed_issues.py
executable file
·76 lines (66 loc) · 2.29 KB
/
closed_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
65
66
67
68
69
70
71
72
73
74
75
76
#!/usr/bin/env python
import os
from datetime import datetime
import argparse
import requests
parser = argparse.ArgumentParser(description='Show issues closed on a week')
parser.add_argument('org', type=str, help='an organization name')
parser.add_argument('week', type=int, help='a number of week')
args = parser.parse_args()
org = args.org
week = args.week
token_file = 'token.txt'
if not os.path.exists(token_file):
raise RuntimeError('{file} is not exists'.format(file=token_file))
if not os.path.isfile(token_file):
raise RuntimeError('{file} is not a regular file'.format(file=token_file))
with open(token_file, 'r') as f:
token = f.read().strip()
year = datetime.now().strftime('%Y')
since = datetime.strptime('{}-W{}-1'.format(year, week), "%G-W%V-%w")
until = datetime.strptime('{}-W{}-1'.format(year, week + 1), "%G-W%V-%w")
session = requests.Session()
headers = {
'Accept': 'application/vnd.github.v3+json',
'Authorization': 'token ' + token,
}
params = {
'filter': 'all',
'state': 'closed',
'sort': 'updated',
'since': since.isoformat(timespec='seconds'),
}
base_url = 'https://api.github.com/orgs/{}/issues'.format(org)
r = session.get(base_url, headers=headers, params=params)
data = []
data.extend(r.json())
while 'next' in r.links:
next_url = r.links['next']['url']
r = session.get(next_url, headers=headers)
data.extend(r.json())
issues_per_repo = {}
for issue in data:
closed_at = datetime.strptime(issue['closed_at'], '%Y-%m-%dT%H:%M:%SZ')
if closed_at < since or closed_at >= until:
continue
number = '#' + str(issue['number'])
if 'pull_request' in issue:
number = 'PR ' + number
title = issue['title'].strip()
milestone = issue['milestone']['title'] if issue['milestone'] else None
repo = issue['repository']['name']
result = {
'number': number,
'milestone': milestone,
'title': title,
'repo': repo,
}
if repo not in issues_per_repo.keys():
issues_per_repo[repo] = []
issues_per_repo[repo].append(result)
for repo, issues in issues_per_repo.items():
for issue in issues:
if issue['milestone']:
print('{repo}: [{number}, {milestone}] {title}'.format(**issue))
else:
print('{repo}: [{number}] {title}'.format(**issue))