Add code to flag PR with the results from the CI pipeline

Signed-off-by: Pierre-Yves Chibon <pingou@pingoured.fr>
This commit is contained in:
Pierre-Yves Chibon 2018-08-08 17:59:08 +02:00
parent 93a33ac797
commit fed379c81b
3 changed files with 148 additions and 0 deletions

View file

@ -0,0 +1,14 @@
---
- name: buildsys.build.state.change
hosts: localhost
gather_facts: false
vars_files:
- "/srv/private/vars_loopabull.yml"
tasks:
- debug: var=msg
roles:
- {role: flag_ci_pr, msg: msg}

View file

@ -0,0 +1,113 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
This script takes as input the fedmsg messages published under the topic
``ci.pipeline.allpackages-pr.complete`` and flag the corresponding PR in
dist-git with the result of the tests.
Authors: Pierre-Yves Chibon <pingou@pingoured.fr>
"""
from __future__ import unicode_literals
import json
import os
import sys
import requests
from requests.packages.urllib3.util import retry
def main(msg):
""" Check if the build was successful and if so, flag the commit in
pagure.
"""
timeout = (30, 30)
retries = 3
requests_session = requests.Session()
retry_conf = retry.Retry(
total=retries, connect=retries, read=retries, backoff_factor=1)
retry_conf.BACKOFF_MAX = 5
requests_session.mount(
'http://', requests.adapters.HTTPAdapter(max_retries=retry_conf))
requests_session.mount(
'https://', requests.adapters.HTTPAdapter(max_retries=retry_conf))
done_states = {
'SUCCESS': 'success',
'UNSTABLE': 'failed',
'FAILURE': 'error',
}
state = msg['status']
if state not in done_states:
print('Build is not in one of the expected status, ignoring')
return
pr_id = msg['rev'].partition('PR-')[2]
if not pr_id:
print(
'Invalid revision: %s, could not extract the PR id from it' %
msg['rev'])
return
data = {
'username': 'All packages PR',
'status': done_states[state],
'comment': 'Package tests: %s' % (done_states[state]),
'url': _koji_link(msg),
}
pagure_url = 'https://src.fedoraproject.org'
env_var = 'API_TOKEN'
if '.stg.' in request:
pagure_url = 'https://src.stg.fedoraproject.org'
env_var = 'API_TOKEN_STG'
target_url = '/'.join([
'api',
'0',
msg['namespace'],
msg['repo'],
'pull-request',
pr_id,
'flag'
])
flag_url = pagure_url + '/' + target_url
print('Flagging commit at: %s' % flag_url)
headers = {
"Authorization": "token " + os.environ.get(env_var),
'User-Agent': 'loopabull@fedora-infra',
}
print('payload: %s' % data)
req = requests_session.request(
method='POST',
url=flag_url,
headers=headers,
data=data,
)
print('Request to %s returned: %s' % (pagure_url, req.status_code))
if not req.ok:
print(req.text)
return 1
else:
print('All clear')
print('User-URL: %s' % pagure_url + '/' + '/'.join([
msg['namespace'],
msg['repo'],
'pull-request',
pr_id,
]))
if __name__ == '__main__':
msg = sys.argv[1]
msg = json.loads(msg)
sys.exit(main(msg))

View file

@ -0,0 +1,21 @@
- name: Install required packages
package: name="{{ item }}" state=present
with_items:
- python2-requests
- name: Install script flagging PRs on dist-git upon test results being published
copy:
src: files/flag_ci_pr.py
dest: /usr/local/bin/flag_ci_pr.py
mode: 0755
- name: Run the script
command: /usr/local/bin/flag_ci_pr.py '{{ msg | to_json }}'
register: output
environment:
API_TOKEN: "{{ api_token_flag_pr_build }}"
API_TOKEN_STG: "{{ api_token_flag_pr_build_stg }}"
- name: Show the output of the script
debug:
msg: "Output of the script: {{ output }}"