From e803bad6487dedd00f76118d7977022091c58297 Mon Sep 17 00:00:00 2001 From: Miroslav Vadkerti Date: Jan 24 2019 09:37:44 +0000 Subject: PR flags: adds support for running/pending state This patch adds support for runinng state for Fedora CI. It keeps only the recent flag around, so running transitions nicely to complete/error state and vice versa (for reruns). Resolves https://pagure.io/fedora-ci/general/issue/3 Signed-off-by: Miroslav Vadkerti --- diff --git a/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.complete.yml b/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.complete.yml index a650db3..e45a348 100644 --- a/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.complete.yml +++ b/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.complete.yml @@ -1,5 +1,5 @@ --- -- name: buildsys.build.state.change +- name: ci.pipeline.allpackages-pr.complete hosts: localhost gather_facts: false @@ -10,5 +10,5 @@ - debug: var=msg roles: - - {role: flag_ci_pr, msg: msg} + - {role: flag_ci_pr, msg: msg, state: "running", seed_flag_ci_pr: seed_flag_ci_pr} diff --git a/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.package.running.yml b/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.package.running.yml new file mode 100644 index 0000000..dfdbf92 --- /dev/null +++ b/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.package.running.yml @@ -0,0 +1,14 @@ +--- +- name: ci.pipeline.allpackages-pr.package.running + hosts: localhost + gather_facts: false + + vars_files: + - "/srv/private/vars_loopabull.yml" + + tasks: + - debug: var=msg + + roles: + - {role: flag_ci_pr, msg: msg, state: "running", seed_flag_ci_pr: seed_flag_ci_pr} + diff --git a/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py b/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py index 7f1daf3..66a74e7 100644 --- a/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py +++ b/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py @@ -12,6 +12,7 @@ Authors: Pierre-Yves Chibon """ from __future__ import unicode_literals +import hashlib import json import os import sys @@ -19,11 +20,13 @@ import sys import requests from requests.packages.urllib3.util import retry - - -def main(msg): +def main(msg, pipeline_state='complete', seed='empty'): """ Check if the build was successful and if so, flag the commit in pagure. + + :param str msg: the fedmsg message body + :param str pipeline_state: state of the testing, one of 'complete' + or 'running' """ timeout = (30, 30) @@ -37,16 +40,30 @@ def main(msg): requests_session.mount( 'https://', requests.adapters.HTTPAdapter(max_retries=retry_conf)) - done_states = { - 'SUCCESS': {'api': 'success', 'human': 'passed'}, - 'UNSTABLE': {'api': 'failure', 'human': 'failed'}, - 'FAILURE': {'api': 'error', 'human': 'errored'}, - } - state = msg['status'] - if state not in done_states: - print('Build is not in one of the expected status, ignoring') + if pipeline_state not in ['complete', 'running']: + print("Pipeline state is not 'complete' or 'running'.") return + # test complete messages + if pipeline_state == 'complete': + done_states = { + 'SUCCESS': {'api': 'success', 'human': 'passed'}, + 'UNSTABLE': {'api': 'failure', 'human': 'failed'}, + 'FAILURE': {'api': 'error', 'human': 'errored'}, + } + state = msg['status'] + if state not in done_states: + print('Build is not in one of the expected status, ignoring') + return + + status = done_states[state]['api'] + human_status = done_states[state]['human'] + + # test running messages + elif pipeline_state == 'running': + status = 'pending' + human_status = 'running' + pr_id = msg['rev'].partition('PR-')[2] if not pr_id: print( @@ -54,12 +71,12 @@ def main(msg): msg['rev']) return - data = { 'username': 'Fedora CI', - 'status': done_states[state]['api'], - 'comment': 'Package tests: %s' % (done_states[state]['human']), + 'status': status, + 'comment': 'Package tests: %s' % human_status, 'url': msg['build_url'], + 'uid': hashlib.md5(pr_id + seed).hexdigest() } pagure_url = 'https://src.fedoraproject.org' @@ -107,4 +124,4 @@ def main(msg): if __name__ == '__main__': msg = sys.argv[1] msg = json.loads(msg) - sys.exit(main(msg)) + sys.exit(main(msg, sys.argv[2], sys.argv[3])) diff --git a/playbooks/roles/flag_ci_pr/tasks/main.yml b/playbooks/roles/flag_ci_pr/tasks/main.yml index a18070b..177f092 100644 --- a/playbooks/roles/flag_ci_pr/tasks/main.yml +++ b/playbooks/roles/flag_ci_pr/tasks/main.yml @@ -5,7 +5,7 @@ mode: 0755 - name: Run the script - command: /usr/local/bin/flag_ci_pr.py {{ msg | to_json | quote }} + command: /usr/local/bin/flag_ci_pr.py {{ msg | to_json | quote }} {{ state | quote }} {{ seed_flag_pr_ci | quote }} register: output environment: API_TOKEN: "{{ api_token_flag_pr_ci }}"