1
|
|
|
# -*- coding: utf-8 -*- |
2
|
|
|
# ----------------------------------------------------------------------------- |
3
|
|
|
# Copyright (c) The Spyder Development Team |
4
|
|
|
# |
5
|
|
|
# Licensed under the terms of the MIT License |
6
|
|
|
# (See LICENSE.txt for details) |
7
|
|
|
# ----------------------------------------------------------------------------- |
8
|
|
|
"""Build a list of issues and pull requests per Github milestone.""" |
9
|
|
|
|
10
|
|
|
from __future__ import print_function |
11
|
|
|
|
12
|
|
|
# Standard library imports |
13
|
|
|
import argparse |
14
|
|
|
import datetime |
15
|
|
|
import getpass |
16
|
|
|
import re |
17
|
|
|
import sys |
18
|
|
|
import time |
19
|
|
|
|
20
|
|
|
# Third party imports |
21
|
|
|
from jinja2 import Template |
22
|
|
|
|
23
|
|
|
# Local imports |
24
|
|
|
from loghub.external.github import GitHub |
25
|
|
|
from loghub.templates import CHANGELOG_TEMPLATE_PATH, RELEASE_TEMPLATE_PATH |
26
|
|
|
|
27
|
|
|
PY2 = sys.version[0] == '2' |
28
|
|
|
|
29
|
|
|
|
30
|
|
|
def main(): |
31
|
|
|
"""Main script.""" |
32
|
|
|
# Cli options |
33
|
|
|
parser = argparse.ArgumentParser( |
34
|
|
|
description='Script to print the list of issues and pull requests ' |
35
|
|
|
'closed in a given milestone') |
36
|
|
|
parser.add_argument( |
37
|
|
|
'repository', |
38
|
|
|
help="Repository name to generate the Changelog for, in the form " |
39
|
|
|
"user/repo or org/repo (e.g. spyder-ide/spyder)") |
40
|
|
|
parser.add_argument( |
41
|
|
|
'-m', |
42
|
|
|
'--milestone', |
43
|
|
|
action="store", |
44
|
|
|
dest="milestone", |
45
|
|
|
default='', |
46
|
|
|
help="Github milestone to get issues and pull requests for") |
47
|
|
|
parser.add_argument( |
48
|
|
|
'-il', |
49
|
|
|
'--issue-label-regex', |
50
|
|
|
action="store", |
51
|
|
|
dest="issue_label_regex", |
52
|
|
|
default='', |
53
|
|
|
help="Label issue filter using a regular expression filter") |
54
|
|
|
parser.add_argument( |
55
|
|
|
'-pl', |
56
|
|
|
'--pr-label-regex', |
57
|
|
|
action="store", |
58
|
|
|
dest="pr_label_regex", |
59
|
|
|
default='', |
60
|
|
|
help="Label pull requets filter using a regular expression filter") |
61
|
|
|
parser.add_argument( |
62
|
|
|
'-st', |
63
|
|
|
'--since-tag', |
64
|
|
|
action="store", |
65
|
|
|
dest="since_tag", |
66
|
|
|
default='', |
67
|
|
|
help="Github issues and pull requests since tag") |
68
|
|
|
parser.add_argument( |
69
|
|
|
'-ut', |
70
|
|
|
'--until-tag', |
71
|
|
|
action="store", |
72
|
|
|
dest="until_tag", |
73
|
|
|
default='', |
74
|
|
|
help="Github issues and pull requests until tag") |
75
|
|
|
parser.add_argument( |
76
|
|
|
'-f', |
77
|
|
|
'--format', |
78
|
|
|
action="store", |
79
|
|
|
dest="output_format", |
80
|
|
|
default='changelog', |
81
|
|
|
help="Format for print, either 'changelog' (for " |
82
|
|
|
"Changelog.md file) or 'release' (for the Github " |
83
|
|
|
"Releases page). Default is 'changelog'. The " |
84
|
|
|
"'release' option doesn't generate Markdown " |
85
|
|
|
"hyperlinks.") |
86
|
|
|
parser.add_argument( |
87
|
|
|
'--template', |
88
|
|
|
action="store", |
89
|
|
|
dest="template", |
90
|
|
|
default='', |
91
|
|
|
help="Use a custom Jinja2 template file ") |
92
|
|
|
parser.add_argument( |
93
|
|
|
'-u', |
94
|
|
|
'--user', |
95
|
|
|
action="store", |
96
|
|
|
dest="user", |
97
|
|
|
default='', |
98
|
|
|
help="Github user name") |
99
|
|
|
parser.add_argument( |
100
|
|
|
'-p', |
101
|
|
|
'--password', |
102
|
|
|
action="store", |
103
|
|
|
dest="password", |
104
|
|
|
default='', |
105
|
|
|
help="Github user password") |
106
|
|
|
parser.add_argument( |
107
|
|
|
'-t', |
108
|
|
|
'--token', |
109
|
|
|
action="store", |
110
|
|
|
dest="token", |
111
|
|
|
default='', |
112
|
|
|
help="Github access token") |
113
|
|
|
options = parser.parse_args() |
114
|
|
|
|
115
|
|
|
username = options.user |
116
|
|
|
password = options.password |
117
|
|
|
milestone = options.milestone |
118
|
|
|
|
119
|
|
|
if username and not password: |
120
|
|
|
password = getpass.getpass() |
121
|
|
|
|
122
|
|
|
# Check if repo given |
123
|
|
|
if not options.repository: |
124
|
|
|
print('Please define a repository name to this script. See its help') |
125
|
|
|
sys.exit(1) |
126
|
|
|
|
127
|
|
|
# Check if milestone or tag given |
128
|
|
|
if not milestone and not options.since_tag: |
129
|
|
|
print('\nQuerying all issues\n') |
130
|
|
|
elif milestone: |
131
|
|
|
print('\nQuerying issues for milestone {0}\n'.format(milestone)) |
132
|
|
|
|
133
|
|
|
create_changelog( |
134
|
|
|
repo=options.repository, |
135
|
|
|
username=username, |
136
|
|
|
password=password, |
137
|
|
|
token=options.token, |
138
|
|
|
milestone=milestone, |
139
|
|
|
since_tag=options.since_tag, |
140
|
|
|
until_tag=options.until_tag, |
141
|
|
|
output_format=options.output_format, |
142
|
|
|
issue_label_regex=options.issue_label_regex, |
143
|
|
|
pr_label_regex=options.pr_label_regex, |
144
|
|
|
template_file=options.template) |
145
|
|
|
|
146
|
|
|
|
147
|
|
|
def create_changelog(repo, username, password, token, milestone, since_tag, |
148
|
|
|
until_tag, output_format, issue_label_regex, |
149
|
|
|
pr_label_regex, template_file): |
150
|
|
|
"""Create changelog data.""" |
151
|
|
|
# Instantiate Github API |
152
|
|
|
gh = GitHubRepo( |
153
|
|
|
username=username, |
154
|
|
|
password=password, |
155
|
|
|
token=token, |
156
|
|
|
repo=repo, ) |
157
|
|
|
|
158
|
|
|
version = until_tag or None |
159
|
|
|
milestone_number = None |
160
|
|
|
closed_at = None |
161
|
|
|
since = None |
162
|
|
|
until = None |
163
|
|
|
|
164
|
|
|
# Set milestone or from tag |
165
|
|
|
if milestone and not since_tag: |
166
|
|
|
milestone_data = gh.milestone(milestone) |
167
|
|
|
milestone_number = milestone_data['number'] |
168
|
|
|
closed_at = milestone_data['closed_at'] |
169
|
|
|
version = milestone.replace('v', '') |
170
|
|
|
elif not milestone and since_tag: |
171
|
|
|
since = gh.tag(since_tag)['tagger']['date'] |
172
|
|
|
if until_tag: |
173
|
|
|
until = gh.tag(until_tag)['tagger']['date'] |
174
|
|
|
closed_at = until |
175
|
|
|
|
176
|
|
|
# This returns issues and pull requests |
177
|
|
|
issues = gh.issues( |
178
|
|
|
milestone=milestone_number, state='closed', since=since, until=until) |
179
|
|
|
|
180
|
|
|
# Filter by regex if available |
181
|
|
|
filtered_issues, filtered_prs = [], [] |
182
|
|
|
issue_pattern = re.compile(issue_label_regex) |
183
|
|
|
pr_pattern = re.compile(pr_label_regex) |
184
|
|
|
for issue in issues: |
185
|
|
|
is_pr = bool(issue.get('pull_request')) |
186
|
|
|
is_issue = not is_pr |
187
|
|
|
labels = ' '.join(issue.get('_label_names')) |
188
|
|
|
|
189
|
|
|
if is_issue and issue_label_regex: |
190
|
|
|
issue_valid = bool(issue_pattern.search(labels)) |
191
|
|
|
if issue_valid: |
192
|
|
|
filtered_issues.append(issue) |
193
|
|
|
elif is_pr and pr_label_regex: |
194
|
|
|
pr_valid = bool(pr_pattern.search(labels)) |
195
|
|
|
if pr_valid: |
196
|
|
|
filtered_prs.append(issue) |
197
|
|
|
elif is_issue and not issue_label_regex: |
198
|
|
|
filtered_issues.append(issue) |
199
|
|
|
elif is_pr and not pr_label_regex: |
200
|
|
|
filtered_prs.append(issue) |
201
|
|
|
|
202
|
|
|
format_changelog( |
203
|
|
|
repo, |
204
|
|
|
filtered_issues, |
205
|
|
|
filtered_prs, |
206
|
|
|
version, |
207
|
|
|
closed_at=closed_at, |
208
|
|
|
output_format=output_format, |
209
|
|
|
template_file=template_file) |
210
|
|
|
|
211
|
|
|
|
212
|
|
|
def format_changelog(repo, |
213
|
|
|
issues, |
214
|
|
|
prs, |
215
|
|
|
version, |
216
|
|
|
closed_at=None, |
217
|
|
|
output_format='changelog', |
218
|
|
|
output_file='CHANGELOG.temp', |
219
|
|
|
template_file=None): |
220
|
|
|
"""Create changelog data.""" |
221
|
|
|
# Header |
222
|
|
|
if version and version[0] == 'v': |
223
|
|
|
version = version.replace('v', '') |
224
|
|
|
else: |
225
|
|
|
version = '<RELEASE_VERSION>' |
226
|
|
|
|
227
|
|
|
if closed_at: |
228
|
|
|
close_date = closed_at.split('T')[0] |
229
|
|
|
else: |
230
|
|
|
close_date = time.strftime("%Y/%m/%d") |
231
|
|
|
|
232
|
|
|
# Load template |
233
|
|
|
if template_file: |
234
|
|
|
filepath = template_file |
235
|
|
|
else: |
236
|
|
|
if output_format == 'changelog': |
237
|
|
|
filepath = CHANGELOG_TEMPLATE_PATH |
238
|
|
|
else: |
239
|
|
|
filepath = RELEASE_TEMPLATE_PATH |
240
|
|
|
|
241
|
|
|
with open(filepath) as f: |
242
|
|
|
data = f.read() |
243
|
|
|
|
244
|
|
|
template = Template(data) |
245
|
|
|
rendered = template.render( |
246
|
|
|
issues=issues, |
247
|
|
|
pull_requests=prs, |
248
|
|
|
version=version, |
249
|
|
|
close_date=close_date, |
250
|
|
|
repo=repo, ) |
251
|
|
|
print('\n') |
252
|
|
|
print(rendered) |
253
|
|
|
print('\n') |
254
|
|
|
|
255
|
|
|
with open(output_file, 'w') as f: |
256
|
|
|
f.write(rendered) |
257
|
|
|
|
258
|
|
|
|
259
|
|
|
class GitHubRepo(object): |
260
|
|
|
"""Github repository wrapper.""" |
261
|
|
|
|
262
|
|
|
def __init__(self, username=None, password=None, token=None, repo=None): |
263
|
|
|
"""Github repository wrapper.""" |
264
|
|
|
self.gh = GitHub( |
265
|
|
|
username=username, |
266
|
|
|
password=password, |
267
|
|
|
access_token=token, ) |
268
|
|
|
repo_organization, repo_name = repo.split('/') |
269
|
|
|
self.repo = self.gh.repos(repo_organization)(repo_name) |
270
|
|
|
|
271
|
|
|
def tags(self): |
272
|
|
|
"""Return all tags.""" |
273
|
|
|
return self.repo('git')('refs')('tags').get() |
274
|
|
|
|
275
|
|
|
def tag(self, tag_name): |
276
|
|
|
"""Get tag information.""" |
277
|
|
|
refs = self.repo('git')('refs')('tags').get() |
278
|
|
|
sha = -1 |
279
|
|
|
for ref in refs: |
280
|
|
|
ref_name = 'refs/tags/{tag}'.format(tag=tag_name) |
281
|
|
|
if 'object' in ref and ref['ref'] == ref_name: |
282
|
|
|
sha = ref['object']['sha'] |
283
|
|
|
break |
284
|
|
|
|
285
|
|
|
if sha == -1: |
286
|
|
|
print("You didn't pass a valid tag name!") |
287
|
|
|
sys.exit(1) |
288
|
|
|
|
289
|
|
|
return self.repo('git')('tags')(sha).get() |
290
|
|
|
|
291
|
|
|
def milestones(self): |
292
|
|
|
"""Return all milestones.""" |
293
|
|
|
return self.repo.milestones.get(state='all') |
294
|
|
|
|
295
|
|
|
def milestone(self, milestone_title): |
296
|
|
|
"""Return milestone with given title.""" |
297
|
|
|
milestones = self.milestones() |
298
|
|
|
milestone_number = -1 |
299
|
|
|
for milestone in milestones: |
300
|
|
|
if milestone['title'] == milestone_title: |
301
|
|
|
milestone_number = milestone['number'] |
302
|
|
|
break |
303
|
|
|
|
304
|
|
|
if milestone_number == -1: |
305
|
|
|
print("You didn't pass a valid milestone name!") |
306
|
|
|
sys.exit(1) |
307
|
|
|
|
308
|
|
|
return milestone |
309
|
|
|
|
310
|
|
|
def issues(self, |
311
|
|
|
milestone=None, |
312
|
|
|
state=None, |
313
|
|
|
assignee=None, |
314
|
|
|
creator=None, |
315
|
|
|
mentioned=None, |
316
|
|
|
labels=None, |
317
|
|
|
sort=None, |
318
|
|
|
direction=None, |
319
|
|
|
since=None, |
320
|
|
|
until=None): |
321
|
|
|
"""Return Issues and Pull Requests.""" |
322
|
|
|
page = 1 |
323
|
|
|
issues = [] |
324
|
|
|
while True: |
325
|
|
|
result = self.repo.issues.get(page=page, |
326
|
|
|
per_page=100, |
327
|
|
|
milestone=milestone, |
328
|
|
|
state=state, |
329
|
|
|
assignee=assignee, |
330
|
|
|
creator=creator, |
331
|
|
|
mentioned=mentioned, |
332
|
|
|
labels=labels, |
333
|
|
|
sort=sort, |
334
|
|
|
firection=direction, |
335
|
|
|
since=since) |
336
|
|
|
if len(result) > 0: |
337
|
|
|
issues += result |
338
|
|
|
page = page + 1 |
339
|
|
|
else: |
340
|
|
|
break |
341
|
|
|
|
342
|
|
|
# If since was provided, filter the issue |
343
|
|
|
if since: |
344
|
|
|
since_date = self.str_to_date(since) |
345
|
|
|
for issue in issues[:]: |
346
|
|
|
close_date = self.str_to_date(issue['closed_at']) |
347
|
|
|
if close_date < since_date: |
348
|
|
|
issues.remove(issue) |
349
|
|
|
|
350
|
|
|
# If until was provided, filter the issue |
351
|
|
|
if until: |
352
|
|
|
until_date = self.str_to_date(until) |
353
|
|
|
for issue in issues[:]: |
354
|
|
|
close_date = self.str_to_date(issue['closed_at']) |
355
|
|
|
if close_date > until_date: |
356
|
|
|
issues.remove(issue) |
357
|
|
|
|
358
|
|
|
# If it is a pr check if it is merged or closed, removed closed ones |
359
|
|
|
for issue in issues[:]: |
360
|
|
|
pr = issue.get('pull_request', '') |
361
|
|
|
|
362
|
|
|
# Add label names inside additional key |
363
|
|
|
issue['_label_names'] = [l['name'] for l in issue.get('labels')] |
364
|
|
|
|
365
|
|
|
if pr: |
366
|
|
|
number = issue['number'] |
367
|
|
|
if not self.is_merged(number): |
368
|
|
|
issues.remove(issue) |
369
|
|
|
|
370
|
|
|
return issues |
371
|
|
|
|
372
|
|
|
def is_merged(self, pr): |
373
|
|
|
""" |
374
|
|
|
Return wether a PR was merged, or if it was closed and discarded. |
375
|
|
|
|
376
|
|
|
https://developer.github.com/v3/pulls/#get-if-a-pull-request-has-been-merged |
377
|
|
|
""" |
378
|
|
|
merged = True |
379
|
|
|
try: |
380
|
|
|
self.repo('pulls')(str(pr))('merge').get() |
381
|
|
|
except Exception: |
382
|
|
|
merged = False |
383
|
|
|
return merged |
384
|
|
|
|
385
|
|
|
@staticmethod |
386
|
|
|
def str_to_date(string): |
387
|
|
|
"""Convert ISO date string to datetime object.""" |
388
|
|
|
parts = string.split('T') |
389
|
|
|
date_parts = parts[0] |
390
|
|
|
time_parts = parts[1][:-1] |
391
|
|
|
year, month, day = [int(i) for i in date_parts.split('-')] |
392
|
|
|
hour, minutes, seconds = [int(i) for i in time_parts.split(':')] |
393
|
|
|
return datetime.datetime(year, month, day, hour, minutes, seconds) |
394
|
|
|
|
395
|
|
|
|
396
|
|
|
if __name__ == '__main__': # yapf: disable |
397
|
|
|
main() |
398
|
|
|
|