forked from boegel/eb-scripts
/
gen_pr_overview_page.py
executable file
·237 lines (197 loc) · 7.95 KB
/
gen_pr_overview_page.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
#!/usr/bin/env python
##
# Copyright 2014 Ghent University
#
# This file is part of EasyBuild,
# originally created by the HPC team of the University of Ghent (http://ugent.be/hpc).
#
# http://github.com/hpcugent/easybuild
#
# EasyBuild is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation v2.
#
# EasyBuild is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with EasyBuild. If not, see <http://www.gnu.org/licenses/>.
##
"""
Generate PR overview HTML page, with interactive table.
@author: Kenneth Hoste (Ghent University)
"""
from datetime import datetime
from vsc.utils.dateandtime import date_parser, datetime_parser
from vsc.utils.generaloption import simple_option
from vsc.utils.missing import nub
from plot_pr_stats import fetch_pr_data
HTML_FILE = '%s_pr_overview.html'
HTML_HEADER = """
<html>
<head>
<script type="text/javascript" src="https://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("visualization", "1", {packages:["table"]});
google.setOnLoadCallback(drawTable);
function drawTable() {
var data = new google.visualization.DataTable();
"""
HTML_FOOTER = """
var table = new google.visualization.Table(document.getElementById('table_div'));
var red_or_green = new google.visualization.ColorFormat();
red_or_green.addRange(null, 0, 'red', 'white');
red_or_green.addRange(1, null, 'green', 'white');
red_or_green.format(data, 5);
red_or_green.format(data, 6);
red_or_green.format(data, 7);
var red_green_gradient = new google.visualization.ColorFormat();
red_green_gradient.addGradientRange(-3, 4, 'black', '#DD0000', '#00BB00');
red_green_gradient.format(data, 8);
table.draw(data, {allowHtml: true, showRowNumber: false, sortColumn: 0});
}
</script>
</head>
<body>
<p align='center'>
Overview of <strong>open</strong> %(repo)s pull requests (%(pr_cnt)s):<br>
<i>(tip: columns are sortable)</i><br>
[last update: %(timestamp)s (UTC) - <strong>%(merged_today)s PRs merged today</strong>]<br>
</p>
<p align='center'>
<div id="table_div" align='center'></div>
</p>
</body>
</html>
"""
TEST_VALUES_MAP = {
'error': -1,
'failure': -1,
'pending': 0,
'success': 1,
}
TEST_RESULTS = [
"{v: -1, f: 'FAIL'}",
"{v: 0, f: '???'}",
"{v: 1, f: 'OK'}",
]
def gen_table_header():
"""Generate table header."""
columns = [
('PR #', 'number'),
('user', 'string'),
('title', 'string'),
('age', 'number'),
('last update (UTC)', 'datetime'),
('unit tests', 'number'),
('style', 'number'),
('test report(s)', 'number'),
('status', 'number'),
('signed off', 'boolean'),
('# participants', 'number'),
('votes', 'number'),
]
lines = []
for label, typ in columns:
lines.append(" data.addColumn('%s', '%s');" % (typ, label))
return '\n'.join(lines) + '\n'
def gen_table_rows(prs):
"""Generate table rows."""
col_tmpls = [
"{v: %(number)s, f: '<a href=\"%(html_url)s\">#%(number)s</a>'}",
"'%(user_login)s'",
"'%(title)s'",
"{v: %(age)s, f: '%(age)s days'}",
"%(last_update)s",
"%(unit_test)s",
"%(style_check)s",
"%(test_reports)s",
"%(status)s",
"%(signed_off)s",
"%(participants)s", # FIXME
"0", # FIXME
]
row_tmpl = " [" + ', '.join(col_tmpls) + "],"
example_ut = [-1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1]
example_sc = [-1, -1, -1, 0, 0, 0, 1, 1, 1, -1, -1, -1, 0, 0, 0, 1, 1, 1, -1, -1, -1, 0, 0, 0, 1, 1, 1]
example_tr = [-1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1]
example_statuses = [-3, -2, -1, -2, -1, 0, -1, 0, 1, -2, -1, 0, -1, 0, 1, 0, 1, 2, -1, 0, 1, 0, 1, 2, 1, 2, 3]
merged_today = 0
todays_date = date_parser('TODAY')
last_update = datetime_parser(prs[0]['updated_at'].replace('T', ' ')[:-1])
lines = [' data.addRows([']
for pr in prs:
if pr['closed_at']:
if date_parser(pr['closed_at'].split('T')[0]) == todays_date:
merged_today += 1
# only consider open PRs
if pr['state'] != 'open':
continue
# shorten long titles, escape single quotes
if len(pr['title']) > 30:
pr['title'] = pr['title'][:30] + '...'
pr['title'] = pr['title'].replace("'", "\\'")
# determine status based on results of unit tests, style check and test reports
unit_test = TEST_VALUES_MAP[pr['combined_status']]
# initial value: '???' (unknown)
style_check = 0
test_reports = 0
signed_off = False
# iterate over all comments, last hit wins
for comment in pr['issue_comments']['bodies']:
if 'lgtm' in comment.lower() or 'style review ok' in comment.lower():
style_check = 1
if comment.lower().startswith('test report'):
if 'SUCCESS' in comment:
test_reports = 1
elif 'FAILED' in comment:
test_reports = -1
if 'good to go' in comment.lower():
signed_off = True
status = unit_test + style_check + test_reports
participants = len(nub(pr['issue_comments']['users']))
year, month, day, hour, minutes, seconds = pr['updated_at'].replace('T', '@')[:-1].replace(':', '@').replace('-', '@').split('@')
pr.update({
'age': (datetime_parser('TODAY') - datetime_parser(pr['created_at'].split('T')[0])).days,
'last_update': "new Date(%s, %s, %s, %s, %s, %s)" % (year, month, day, hour, minutes, seconds),
'participants': participants,
'signed_off': ['', 'true'][signed_off],
'status': status,
'style_check': TEST_RESULTS[style_check+1],
'test_reports': TEST_RESULTS[test_reports+1],
'unit_test': TEST_RESULTS[unit_test+1],
'user_login': pr['user']['login'],
})
lines.append(row_tmpl % pr)
last_update = max(datetime_parser(pr['updated_at'].replace('T', ' ')[:-1]), last_update)
lines.append(' ]);')
return len(lines)-2, '\n'.join(lines), merged_today, last_update
def main():
opts = {
'github-account': ("GitHub account where repository is located", None, 'store', 'hpcugent', 'a'),
'github-user': ("GitHub user to use (for authenticated access)", None, 'store', 'boegel', 'u'),
'repository': ("Repository to use", None, 'store', 'easybuild-easyconfigs', 'r'),
}
go = simple_option(go_dict=opts, descr="Script to print overview of pull requests for a GitHub repository")
pickle_file = None
if go.args:
pickle_file = go.args[0]
prs = fetch_pr_data(pickle_file, go.options.github_user, go.options.github_account, go.options.repository)
html_file = HTML_FILE % go.options.repository
print("Generating %s..." % html_file)
handle = open(html_file, 'w')
handle.write(HTML_HEADER)
handle.write(gen_table_header())
pr_cnt, table_rows, merged_today, last_update = gen_table_rows(prs)
handle.write(table_rows)
handle.write(HTML_FOOTER % {
'merged_today': merged_today,
'pr_cnt': pr_cnt,
'repo': '%s/%s' % (go.options.github_account, go.options.repository),
'timestamp': last_update, #datetime.now().strftime(format='%d %B %Y %H:%M:%S'),
})
handle.close()
if __name__ == '__main__':
main()