forked from openSUSE/openSUSE-release-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopenqa-comments.py
232 lines (191 loc) · 8.6 KB
/
openqa-comments.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
# Copyright (C) 2014 SUSE Linux Products GmbH
#
# This program 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; either version 2 of the License, or
# (at your option) any later version.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import argparse
from datetime import datetime, timedelta
from collections import defaultdict
import json
from osclib.comments import CommentAPI
from osclib.conf import Config
from osclib.stagingapi import StagingAPI
import osc
MARGIN_HOURS = 4
MAX_LINES = 6
class OpenQAReport(object):
def __init__(self, api):
self.api = api
self.comment = CommentAPI(api.apiurl)
def _package_url(self, package):
link = 'https://build.opensuse.org/package/live_build_log/%s/%s/%s/%s'
link = link % (package['project'],
package['package'],
package['repository'],
package['arch'])
text = '[%s](%s)' % (package['arch'], link)
return text
def _openQA_url(self, job):
test_name = job['name'].split('-')[-1]
link = '%s/tests/%s' % (self.api.copenqa, job['id'])
text = '[%s](%s)' % (test_name, link)
return text
def _openQA_module_url(self, job, module):
link = '%s/tests/%s/modules/%s/steps/1' % (
self.api.copenqa, job['id'], module['name']
)
text = '[%s](%s)' % (module['name'], link)
return text
def old_enough(self, _date):
time_delta = datetime.utcnow() - _date
safe_margin = timedelta(hours=MARGIN_HOURS)
return safe_margin <= time_delta
def get_info(self, project):
_prefix = '{}:'.format(self.api.cstaging)
if project.startswith(_prefix):
project = project.replace(_prefix, '')
query = {'format': 'json'}
url = self.api.makeurl(('project', 'staging_projects',
self.api.project, project), query=query)
info = json.load(self.api.retried_GET(url))
return info
def get_broken_package_status(self, info):
status = info['broken_packages']
subproject = info['subproject']
if subproject:
status.extend(subproject['broken_packages'])
return status
def get_openQA_status(self, info):
status = info['openqa_jobs']
subproject = info['subproject']
if subproject:
status.extend(subproject['openqa_jobs'])
return status
def is_there_openqa_comment(self, project):
"""Return True if there is a previous comment."""
signature = '<!-- openQA status -->'
comments = self.comment.get_comments(project_name=project)
comment = [c for c in comments.values() if signature in c['comment']]
return len(comment) > 0
def update_status_comment(self, project, report, force=False):
signature = '<!-- openQA status -->'
report = '%s\n%s' % (signature, str(report))
write_comment = False
comments = self.comment.get_comments(project_name=project)
comment = [c for c in comments.values() if signature in c['comment']]
if comment and len(comment) > 1:
print 'ERROR. There are more than one openQA status comment in %s' % project
# for c in comment:
# self.comment.delete(c['id'])
# write_comment = True
elif comment and comment[0]['comment'] != report and self.old_enough(comment[0]['when']):
self.comment.delete(comment[0]['id'])
write_comment = True
elif not comment:
write_comment = True
if write_comment or force:
if osc.conf.config['debug']:
print 'Updating comment'
self.comment.add_comment(project_name=project, comment=report)
def _report_broken_packages(self, info):
broken_package_status = self.get_broken_package_status(info)
# Group packages by name
groups = defaultdict(list)
for package in broken_package_status:
groups[package['package']].append(package)
failing_lines = [
'* Build failed %s (%s)' % (key, ', '.join(self._package_url(p) for p in value))
for key, value in groups.iteritems()
]
report = '\n'.join(failing_lines[:MAX_LINES])
if len(failing_lines) > MAX_LINES:
report += '* and more (%s) ...' % (len(failing_lines) - MAX_LINES)
return report
def _report_openQA(self, info):
failing_lines, green_lines = [], []
openQA_status = self.get_openQA_status(info)
for job in openQA_status:
test_name = job['name'].split('-')[-1]
fails = [
' * %s (%s)' % (test_name, self._openQA_module_url(job, module))
for module in job['modules'] if module['result'] == 'failed'
]
if fails:
failing_lines.extend(fails)
else:
green_lines.append(self._openQA_url(job))
failing_report, green_report = '', ''
if failing_lines:
failing_report = '* Failing openQA tests:\n' + '\n'.join(failing_lines[:MAX_LINES])
if len(failing_lines) > MAX_LINES:
failing_report += '\n * and more (%s) ...' % (len(failing_lines) - MAX_LINES)
if green_lines:
green_report = '* Succeeding tests:' + ', '.join(green_lines[:MAX_LINES])
if len(green_lines) > MAX_LINES:
green_report += ', and more (%s) ...' % (len(green_lines) - MAX_LINES)
return '\n'.join((failing_report, green_report)), bool(failing_lines)
def report(self, project):
info = self.get_info(project)
# Some staging projects do not have info like
# openSUSE:Factory:Staging:Gcc49
if not info:
return
if info['overall_state'] == 'empty':
return
# The 'unacceptable' status means that the project will be
# replaced soon. Better do not disturb with noise.
if info['overall_state'] == 'unacceptable':
return
report_broken_packages = self._report_broken_packages(info)
report_openQA, some_openqa_fail = self._report_openQA(info)
if report_broken_packages or some_openqa_fail:
report = '\n\n'.join((report_broken_packages, report_openQA))
report = report.strip()
if report:
if osc.conf.config['debug']:
print project
print '-' * len(project)
print report
self.update_status_comment(project, report)
elif not info['overall_state'] == 'acceptable' and self.is_there_openqa_comment(project):
report = 'Congratulations! All fine now.'
if osc.conf.config['debug']:
print project
print '-' * len(project)
print report
self.update_status_comment(project, report, force=True)
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Command to publish openQA status in Staging projects')
parser.add_argument('-s', '--staging', type=str, default=None,
help='staging project letter')
parser.add_argument('-f', '--force', action='store_true', default=False,
help='force the write of the comment')
parser.add_argument('-p', '--project', type=str, default='Factory',
help='openSUSE version to make the check (Factory, 13.2)')
parser.add_argument('-d', '--debug', action='store_true', default=False,
help='enable debug information')
args = parser.parse_args()
osc.conf.get_config()
osc.conf.config['debug'] = args.debug
if args.force:
MARGIN_HOURS = 0
Config('openSUSE:%s' % args.project)
api = StagingAPI(osc.conf.config['apiurl'], 'openSUSE:%s' % args.project)
openQA = OpenQAReport(api)
if args.staging:
openQA.report(api.prj_from_letter(args.staging))
else:
for staging in api.get_staging_projects():
if not staging.endswith(':DVD'):
openQA.report(staging)