2018-05-10 19:46:17 -05:00
|
|
|
#!/usr/bin/python
|
2014-09-16 13:41:15 +02:00
|
|
|
|
|
|
|
import argparse
|
|
|
|
from datetime import datetime, timedelta
|
2014-09-16 17:59:34 +02:00
|
|
|
from collections import defaultdict
|
2014-09-16 13:41:15 +02:00
|
|
|
import json
|
|
|
|
|
|
|
|
from osclib.comments import CommentAPI
|
2015-02-19 10:57:55 +01:00
|
|
|
from osclib.conf import Config
|
2014-09-16 13:41:15 +02:00
|
|
|
from osclib.stagingapi import StagingAPI
|
|
|
|
|
|
|
|
import osc
|
|
|
|
|
2014-09-16 14:47:20 +02:00
|
|
|
MARGIN_HOURS = 4
|
2014-09-22 18:41:56 +02:00
|
|
|
MAX_LINES = 6
|
2014-09-16 13:41:15 +02:00
|
|
|
|
2014-09-16 17:59:34 +02:00
|
|
|
|
2014-09-16 13:41:15 +02:00
|
|
|
class OpenQAReport(object):
|
|
|
|
def __init__(self, api):
|
|
|
|
self.api = api
|
2015-02-26 10:56:04 +01:00
|
|
|
self.comment = CommentAPI(api.apiurl)
|
2014-09-16 13:41:15 +02:00
|
|
|
|
2014-09-16 17:59:34 +02:00
|
|
|
def _package_url(self, package):
|
2018-07-05 20:43:07 +02:00
|
|
|
link = '/package/live_build_log/%s/%s/%s/%s'
|
2016-05-04 14:37:03 +02:00
|
|
|
link = link % (package['project'],
|
|
|
|
package['package'],
|
|
|
|
package['repository'],
|
|
|
|
package['arch'])
|
2014-09-16 17:59:34 +02:00
|
|
|
text = '[%s](%s)' % (package['arch'], link)
|
|
|
|
return text
|
|
|
|
|
2014-09-16 13:41:15 +02:00
|
|
|
def old_enough(self, _date):
|
|
|
|
time_delta = datetime.utcnow() - _date
|
2014-09-16 14:47:20 +02:00
|
|
|
safe_margin = timedelta(hours=MARGIN_HOURS)
|
2014-09-16 13:41:15 +02:00
|
|
|
return safe_margin <= time_delta
|
|
|
|
|
2014-09-30 11:29:06 +02:00
|
|
|
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):
|
2014-09-16 13:41:15 +02:00
|
|
|
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
|
|
|
|
|
2014-09-30 11:29:06 +02:00
|
|
|
if write_comment or force:
|
|
|
|
if osc.conf.config['debug']:
|
|
|
|
print 'Updating comment'
|
2014-09-16 13:41:15 +02:00
|
|
|
self.comment.add_comment(project_name=project, comment=report)
|
|
|
|
|
2014-09-16 17:59:34 +02:00
|
|
|
def _report_broken_packages(self, info):
|
2018-07-05 16:34:19 +08:00
|
|
|
broken_package_status = info['broken_packages']
|
2014-09-16 17:59:34 +02:00
|
|
|
|
|
|
|
# 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()
|
|
|
|
]
|
|
|
|
|
2014-09-22 18:41:56 +02:00
|
|
|
report = '\n'.join(failing_lines[:MAX_LINES])
|
|
|
|
if len(failing_lines) > MAX_LINES:
|
|
|
|
report += '* and more (%s) ...' % (len(failing_lines) - MAX_LINES)
|
|
|
|
return report
|
2014-09-16 17:59:34 +02:00
|
|
|
|
2018-11-09 15:13:58 -06:00
|
|
|
def report_checks(self, info):
|
2014-09-16 14:47:20 +02:00
|
|
|
failing_lines, green_lines = [], []
|
2014-09-16 13:41:15 +02:00
|
|
|
|
2018-11-09 15:13:58 -06:00
|
|
|
links_state = {}
|
|
|
|
for check in info['checks']:
|
|
|
|
links_state.setdefault(check['state'], [])
|
|
|
|
links_state[check['state']].append('[{}]({})'.format(check['name'], check['url']))
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
failure = False
|
|
|
|
for state, links in links_state.items():
|
|
|
|
if len(links) > MAX_LINES:
|
|
|
|
extra = len(links) - MAX_LINES
|
|
|
|
links = links[:MAX_LINES]
|
|
|
|
links.append('and {} more...'.format(extra))
|
|
|
|
|
|
|
|
lines.append('- {}'.format(state))
|
|
|
|
if state != 'success':
|
|
|
|
lines.extend([' - {}'.format(link) for link in links])
|
|
|
|
failure = True
|
2014-09-16 13:41:15 +02:00
|
|
|
else:
|
2018-11-09 15:13:58 -06:00
|
|
|
lines[-1] += ': {}'.format(', '.join(links))
|
2014-09-16 14:47:20 +02:00
|
|
|
|
2018-11-09 15:13:58 -06:00
|
|
|
return '\n'.join(lines).strip(), failure
|
2014-09-16 13:41:15 +02:00
|
|
|
|
2018-11-09 15:12:27 -06:00
|
|
|
def report(self, project, aggregate=True):
|
|
|
|
info = self.api.project_status(project, aggregate)
|
2014-09-19 14:50:57 +02:00
|
|
|
|
|
|
|
# Some staging projects do not have info like
|
|
|
|
# openSUSE:Factory:Staging:Gcc49
|
|
|
|
if not info:
|
|
|
|
return
|
|
|
|
|
|
|
|
if info['overall_state'] == 'empty':
|
|
|
|
return
|
|
|
|
|
2014-10-01 11:54:38 +02:00
|
|
|
# The 'unacceptable' status means that the project will be
|
|
|
|
# replaced soon. Better do not disturb with noise.
|
|
|
|
if info['overall_state'] == 'unacceptable':
|
|
|
|
return
|
|
|
|
|
2014-09-16 17:59:34 +02:00
|
|
|
report_broken_packages = self._report_broken_packages(info)
|
2018-11-09 15:13:58 -06:00
|
|
|
report_checks, check_failure = self.report_checks(info)
|
2014-09-16 14:47:20 +02:00
|
|
|
|
2018-11-09 15:13:58 -06:00
|
|
|
if report_broken_packages or check_failure:
|
2017-06-15 17:07:19 -05:00
|
|
|
if report_broken_packages:
|
|
|
|
report_broken_packages = 'Broken:\n\n' + report_broken_packages
|
2018-11-09 15:13:58 -06:00
|
|
|
if report_checks:
|
|
|
|
report_checks = 'Checks:\n\n' + report_checks
|
|
|
|
report = '\n\n'.join((report_broken_packages, report_checks))
|
2014-09-19 14:50:57 +02:00
|
|
|
report = report.strip()
|
|
|
|
if report:
|
2014-09-29 10:07:05 +02:00
|
|
|
if osc.conf.config['debug']:
|
|
|
|
print project
|
|
|
|
print '-' * len(project)
|
|
|
|
print report
|
2014-09-19 14:50:57 +02:00
|
|
|
self.update_status_comment(project, report)
|
2014-09-30 11:29:06 +02:00
|
|
|
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)
|
2014-09-16 14:47:20 +02:00
|
|
|
|
2014-09-16 13:41:15 +02:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
parser = argparse.ArgumentParser(
|
2018-11-09 15:11:26 -06:00
|
|
|
description='Publish openQA status in staging projects')
|
2014-09-16 13:41:15 +02:00
|
|
|
parser.add_argument('-s', '--staging', type=str, default=None,
|
2018-11-09 15:11:26 -06:00
|
|
|
help='staging project')
|
2014-09-16 14:47:20 +02:00
|
|
|
parser.add_argument('-f', '--force', action='store_true', default=False,
|
2018-11-09 15:11:26 -06:00
|
|
|
help='force a comment to be written')
|
|
|
|
parser.add_argument('-p', '--project', type=str, default='openSUSE:Factory',
|
|
|
|
help='project to check (ex. openSUSE:Factory, openSUSE:Leap:15.1)')
|
2014-09-16 13:41:15 +02:00
|
|
|
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
|
|
|
|
|
2014-09-16 14:47:20 +02:00
|
|
|
if args.force:
|
|
|
|
MARGIN_HOURS = 0
|
|
|
|
|
2018-08-16 21:46:05 -05:00
|
|
|
apiurl = osc.conf.config['apiurl']
|
|
|
|
Config(apiurl, args.project)
|
|
|
|
api = StagingAPI(apiurl, args.project)
|
2014-09-16 13:41:15 +02:00
|
|
|
openQA = OpenQAReport(api)
|
|
|
|
|
|
|
|
if args.staging:
|
2018-11-09 15:12:27 -06:00
|
|
|
openQA.report(api.prj_from_letter(args.staging), False)
|
2014-09-16 13:41:15 +02:00
|
|
|
else:
|
2018-07-05 20:43:07 +02:00
|
|
|
for staging in api.get_staging_projects():
|
2017-10-11 17:38:11 -05:00
|
|
|
openQA.report(staging)
|