forked from scipy/scipy
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgh_lists.py
266 lines (217 loc) · 8.51 KB
/
gh_lists.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
#!/usr/bin/env python3
"""
gh_lists.py MILESTONE
Functions for Github API requests.
"""
import os
import re
import sys
import json
import collections
import argparse
import datetime
import time
from urllib.request import urlopen, Request, HTTPError
Issue = collections.namedtuple('Issue', ('id', 'title', 'url'))
def main():
p = argparse.ArgumentParser(usage=__doc__.lstrip())
p.add_argument('--project', default='scipy/scipy')
p.add_argument('milestone')
args = p.parse_args()
getter = CachedGet('gh_cache.json', GithubGet())
try:
milestones = get_milestones(getter, args.project)
if args.milestone not in milestones:
msg = "Milestone {0} not available. Available milestones: {1}"
msg = msg.format(args.milestone, ", ".join(sorted(milestones)))
p.error(msg)
issues = get_issues(getter, args.project, args.milestone)
issues.sort()
finally:
getter.save()
prs = [x for x in issues if '/pull/' in x.url]
issues = [x for x in issues if x not in prs]
def print_list(title, items):
print()
print(title)
print("-"*len(title))
print()
def backtick_repl(matchobj):
"""repl to add an escaped space following a code block if needed"""
if matchobj.group(2) != ' ':
post = '\ ' + matchobj.group(2)
else:
post = matchobj.group(2)
return '``' + matchobj.group(1) + '``' + post
for issue in items:
msg = "* `#{0} <{1}>`__: {2}"
# sanitize whitespace
title = re.sub("\\s+", " ", issue.title.strip())
# substitute any single backtick not adjacent to a backtick
# for a double backtick
title = re.sub("(?P<pre>(?:^|(?<=[^`])))`(?P<post>(?=[^`]|$))",
"\g<pre>``\g<post>",
title)
# add an escaped space if code block is not followed by a space
title = re.sub("``(.*?)``(.)", backtick_repl, title)
# sanitize asterisks
title = title.replace('*', '\\*')
if len(title) > 60:
remainder = re.sub("\\s.*$", "...", title[60:])
if len(remainder) > 20:
# just use the first 80 characters, with ellipses.
# note: this was previously bugged,
# assigning to `remainder` rather than `title`
title = title[:80] + "..."
else:
# use the first 60 characters and the next word
title = title[:60] + remainder
if title.count('`') % 4 != 0:
# ellipses have cut in the middle of a code block,
# so finish the code block before the ellipses
title = title[:-3] + '``...'
msg = msg.format(issue.id, issue.url, title)
print(msg)
print()
msg = f"Issues closed for {args.milestone}"
print_list(msg, issues)
msg = f"Pull requests for {args.milestone}"
print_list(msg, prs)
return 0
def get_milestones(getter, project):
url = f"https://api.github.com/repos/{project}/milestones"
data = getter.get(url)
milestones = {}
for ms in data:
milestones[ms['title']] = ms['number']
return milestones
def get_issues(getter, project, milestone):
milestones = get_milestones(getter, project)
mid = milestones[milestone]
url = "https://api.github.com/repos/{project}/issues?milestone={mid}&state=closed&sort=created&direction=asc"
url = url.format(project=project, mid=mid)
data = getter.get(url)
issues = []
# data contains both PR and issue data
for issue_data in data:
# don't include PRs that were closed instead
# of merged
if "pull" in issue_data['html_url']:
merge_status = issue_data['pull_request']['merged_at']
if merge_status is None:
continue
issues.append(Issue(issue_data['number'],
issue_data['title'],
issue_data['html_url']))
return issues
class CachedGet:
def __init__(self, filename, getter):
self._getter = getter
self.filename = filename
if os.path.isfile(filename):
print(f"[gh_lists] using {filename} as cache "
f"(remove it if you want fresh data)",
file=sys.stderr)
with open(filename, encoding='utf-8') as f:
self.cache = json.load(f)
else:
self.cache = {}
def get(self, url):
if url not in self.cache:
data = self._getter.get_multipage(url)
self.cache[url] = data
return data
else:
print("[gh_lists] (cached):", url, file=sys.stderr, flush=True)
return self.cache[url]
def save(self):
tmp = self.filename + ".new"
with open(tmp, 'w', encoding='utf-8') as f:
json.dump(self.cache, f)
os.rename(tmp, self.filename)
class GithubGet:
def __init__(self, auth=False):
self.headers = {'User-Agent': 'gh_lists.py',
'Accept': 'application/vnd.github.v3+json'}
if auth:
self.authenticate()
req = self.urlopen('https://api.github.com/rate_limit')
try:
if req.getcode() != 200:
raise RuntimeError()
info = json.loads(req.read().decode('utf-8'))
finally:
req.close()
self.ratelimit_remaining = int(info['rate']['remaining'])
self.ratelimit_reset = float(info['rate']['reset'])
def authenticate(self):
print("Input a Github API access token.\n"
"Personal tokens can be created at https://github.com/settings/tokens\n"
"This script does not require any permissions (so don't give it any).",
file=sys.stderr, flush=True)
print("Access token: ", file=sys.stderr, end='', flush=True)
token = input()
self.headers['Authorization'] = f'token {token.strip()}'
def urlopen(self, url, auth=None):
assert url.startswith('https://')
req = Request(url, headers=self.headers)
return urlopen(req, timeout=60)
def get_multipage(self, url):
data = []
while url:
page_data, info, next_url = self.get(url)
data += page_data
url = next_url
return data
def get(self, url):
while True:
# Wait until rate limit
while self.ratelimit_remaining == 0 and self.ratelimit_reset > time.time():
s = self.ratelimit_reset + 5 - time.time()
if s <= 0:
break
print(
"[gh_lists] rate limit exceeded: waiting until {} ({} s remaining)"
.format(datetime.datetime.fromtimestamp(self.ratelimit_reset)
.strftime('%Y-%m-%d %H:%M:%S'),
int(s)),
file=sys.stderr, flush=True
)
time.sleep(min(5*60, s))
# Get page
print("[gh_lists] get:", url, file=sys.stderr, flush=True)
try:
req = self.urlopen(url)
try:
code = req.getcode()
info = req.info()
data = json.loads(req.read().decode('utf-8'))
finally:
req.close()
except HTTPError as err:
code = err.getcode()
info = err.info()
data = None
if code not in (200, 403):
raise RuntimeError()
# Parse reply
next_url = None
if 'Link' in info:
m = re.search('<([^<>]*)>; rel="next"', info['Link'])
if m:
next_url = m.group(1)
# Update rate limit info
if 'X-RateLimit-Remaining' in info:
self.ratelimit_remaining = int(info['X-RateLimit-Remaining'])
if 'X-RateLimit-Reset' in info:
self.ratelimit_reset = float(info['X-RateLimit-Reset'])
# Deal with rate limit exceeded
if code != 200 or data is None:
if self.ratelimit_remaining == 0:
continue
else:
raise RuntimeError()
# Done.
return data, info, next_url
if __name__ == "__main__":
sys.exit(main())