-
Notifications
You must be signed in to change notification settings - Fork 46
/
changelog.py
executable file
·227 lines (201 loc) · 7.43 KB
/
changelog.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
#!/usr/bin/env python3
"""
Changelog generator
"""
import argparse
import re
import subprocess
import sys
import jinja2
import requests
from logzero import logger
def main(args):
issue_keys = get_issues_from_git(args.current_release, args.new_release)
issues = get_issue_info(issue_keys, args.email, args.api_token)
if len(issues) == 0:
logger.warning('No issues in release')
return
if args.html:
save_html(
{
'issues': issues,
'tags': {
'current': args.current_release,
'new': args.new_release,
},
},
args.html,
)
if sys.platform == 'darwin':
subprocess.run(['open', args.html])
else:
print_changelog(args.current_release, args.new_release, issues)
# git log v0.9.0-rc3..v0.9.0-rc4 --oneline | grep -E '[A-Z]+-\d+' | perl -ne '/([A-Z]+-\d+)/; print "$1\n"' | sort -u
def get_issues_from_git(current_release, new_release):
git_log = subprocess.run(
['git', 'log', f'{current_release}..{new_release}', '--oneline'],
capture_output=True,
)
git_log = git_log.stdout.splitlines()
issue_re = re.compile(r'[A-Z]+-\d+')
issue_keys = []
for msg in git_log:
matches = issue_re.findall(msg.decode('utf-8'))
for m in matches:
issue_keys.append(m)
return sorted(set(issue_keys))
def get_issue_info(issue_keys, email, api_token):
issues = []
for key in issue_keys:
r = requests.get(
f'https://insolar.atlassian.net/rest/api/3/issue/{key}',
{
'fields': 'summary,components,status,resolution,issuetype,priority,fixVersions',
},
auth=(email, api_token)
)
try:
i = r.json()
if 'errors' in i:
logger.warning(f'Failed to get info for issue {key}')
else:
issues.append(i)
except ValueError:
logger.error(f'Failed to get info for issue {key}')
return issues
def print_changelog(current, new, issues):
print(f'Changelog from {current} to {new}:')
for i in issues:
print(f"{i['key']}: {i['fields']['summary']}")
def prepare_context(context):
status_colors = {
'new': 'secondary',
'indeterminate': 'info',
'done': 'success',
}
parsed_issues = []
for i in context['issues']:
try:
key = i['fields']['status']['statusCategory']['key']
if key in status_colors:
i['fields']['status']['statusCategory']['status'] = status_colors[key]
parsed_issues.append(i)
except KeyError:
logger.error(f'No fields in issue: {i}')
context['issues'] = parsed_issues
return context
def save_html(context, filename):
context = prepare_context(context)
html = render_html(context)
with open(filename, 'w+') as f:
f.write(html)
def render_html(context):
tpl = jinja2.Environment().from_string(PAGE_TPL)
return tpl.render(context)
PAGE_TPL = '''
<!doctype html>
<html lang="ru">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<link
rel="stylesheet"
href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T"
crossorigin="anonymous"
>
<title>Release info</title>
</head>
<body>
<div class="container">
<div class="row mt-5 mb-3">
<div class="col-sm-12">
<h3>Changelog from <b>{{ tags.current }}</b> to <b>{{ tags.new }}</b></h3>
</div>
</div>
<div class="row">
<div class="col-sm-12">
<table class="table">
<tbody>
{% for issue in issues %}
<tr>
<td>
<img src="{{ issue.fields.issuetype.iconUrl }}"
title="{{ issue.fields.issuetype.name }}"
width="16"
height="16"
>
</td>
<td class="text-nowrap">
<a href="https://insolar.atlassian.net/browse/{{ issue.key }}"
target="_blank"
>{{ issue.key }}</a>
</td>
<td>
<a href="https://insolar.atlassian.net/browse/{{ issue.key }}"
target="_blank"
>{{ issue.fields.summary }}</a>
</td>
<td>
<img src="{{ issue.fields.priority.iconUrl }}"
title="{{ issue.fields.priority.name }}"
width="16"
height="16"
>
</td>
<td class="h5">
<span
class="badge badge-{{ issue.fields.status.statusCategory.status }}"
title="{{ issue.fields.resolution.name }}"
data-toggle="tooltip"
>{{ issue.fields.status.name }}</span>
</td>
<td class="text-nowrap">
{% for cmp in issue.fields.components %}
{{ cmp.name }}{% if not loop.last %},{% endif %}
{% endfor %}
</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</div>
<script
src="https://code.jquery.com/jquery-3.3.1.slim.min.js"
integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo"
crossorigin="anonymous"
></script>
<script
src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"
integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1"
crossorigin="anonymous"
></script>
<script
src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"
></script>
<script>
$(function () {
$('[data-toggle="tooltip"]').tooltip()
})
</script>
</body>
</html>
'''
if __name__ == '__main__':
''' This is executed when run from the command line '''
parser = argparse.ArgumentParser()
parser.add_argument('current_release', help='Current release tag')
parser.add_argument('new_release', help='New release tag')
parser.add_argument('-e', '--email', help='JIRA account email', action='store', dest='email', required=True)
parser.add_argument(
'-t', '--api-token',
help='JIRA API token. You can get one here: https://id.atlassian.com/manage/api-tokens',
action='store', dest='api_token', required=True,
)
parser.add_argument('--html', help='Generate HTML file', action='store', dest='html', required=False)
args = parser.parse_args()
main(args)