From 4120c9c71dbcf6e229093e9470219a5c35d9a07e Mon Sep 17 00:00:00 2001
From: Bruno Goncalves <bgoncalv@redhat.com>
Date: Nov 21 2019 15:42:29 +0000
Subject: [PATCH 1/2] uses CI message version 0.2.X


process CI messages as defined on https://pagure.io/fedora-ci/messages/blob/master/f/schemas

---

diff --git a/playbooks/org.centos.prod.ci.dist-git-pr.test.complete.yml b/playbooks/org.centos.prod.ci.dist-git-pr.test.complete.yml
new file mode 100644
index 0000000..a34df68
--- /dev/null
+++ b/playbooks/org.centos.prod.ci.dist-git-pr.test.complete.yml
@@ -0,0 +1,14 @@
+---
+- name: ci.dist-git-pr.test.complete
+  hosts: localhost
+  gather_facts: false
+
+  vars_files:
+   - "/srv/private/vars_loopabull.yml"
+
+  tasks:
+    - debug: var=msg
+
+  roles:
+    - {role: flag_ci_pr, msg: msg, state: "complete"}
+
diff --git a/playbooks/org.centos.prod.ci.dist-git-pr.test.error.yml b/playbooks/org.centos.prod.ci.dist-git-pr.test.error.yml
new file mode 100644
index 0000000..0d6ea87
--- /dev/null
+++ b/playbooks/org.centos.prod.ci.dist-git-pr.test.error.yml
@@ -0,0 +1,14 @@
+---
+- name: ci.dist-git-pr.test.error
+  hosts: localhost
+  gather_facts: false
+
+  vars_files:
+   - "/srv/private/vars_loopabull.yml"
+
+  tasks:
+    - debug: var=msg
+
+  roles:
+    - {role: flag_ci_pr, msg: msg, state: "error"}
+
diff --git a/playbooks/org.centos.prod.ci.dist-git-pr.test.running.yml b/playbooks/org.centos.prod.ci.dist-git-pr.test.running.yml
new file mode 100644
index 0000000..673497a
--- /dev/null
+++ b/playbooks/org.centos.prod.ci.dist-git-pr.test.running.yml
@@ -0,0 +1,14 @@
+---
+- name: ci.dist-git-pr.test.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"}
+
diff --git a/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.complete.yml b/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.complete.yml
deleted file mode 100644
index 856a885..0000000
--- a/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.complete.yml
+++ /dev/null
@@ -1,14 +0,0 @@
----
-- name: ci.pipeline.allpackages-pr.complete
-  hosts: localhost
-  gather_facts: false
-
-  vars_files:
-   - "/srv/private/vars_loopabull.yml"
-
-  tasks:
-    - debug: var=msg
-
-  roles:
-    - {role: flag_ci_pr, msg: msg, state: "complete"}
-
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
deleted file mode 100644
index 26ca63e..0000000
--- a/playbooks/org.centos.prod.ci.pipeline.allpackages-pr.package.running.yml
+++ /dev/null
@@ -1,14 +0,0 @@
----
-- 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"}
-
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 ea0e407..2e39099 100644
--- a/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py
+++ b/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py
@@ -40,56 +40,69 @@ def main(msg, pipeline_state='complete', seed='empty'):
     requests_session.mount(
         'https://', requests.adapters.HTTPAdapter(max_retries=retry_conf))
 
-    if pipeline_state not in ['complete', 'running']:
-        print("Pipeline state is not 'complete' or 'running'.")
+    if pipeline_state not in ['complete', 'running', 'error']:
+        print("Pipeline state is not 'complete' or 'running' or 'error'.")
+        return
+
+    if 'version' not in msg or not msg['version'].startswith('0.2.'):
+        print('Unsupported msg version, ignoring')
         return
 
     commit_hash_text = ''
     # test complete messages
     if pipeline_state == 'complete':
         done_states = {
-            'SUCCESS': {'api': 'success', 'human': 'passed'},
-            'UNSTABLE': {'api': 'failure', 'human': 'failed'},
-            'FAILURE': {'api': 'error', 'human': 'errored'},
+            'passed': {'api': 'success', 'human': 'passed'},
+            'error': {'api': 'error', 'human': 'errored'},
+            'failed': {'api': 'failure', 'human': 'failed'},
+            'needs_inspection': {'api': 'failure', 'human': 'failed'},
         }
-        state = msg['status']
+        state = msg['test']['result']
         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']
-        if 'commit_hash' in msg:
-            commit_hash_text = ' for %s' % msg['commit_hash'][:8]
+        if 'commit_hash' in msg['artifact']:
+            commit_hash_text = ' for %s' % msg['artifact']['commit_hash'][:8]
 
     # test running messages
     elif pipeline_state == 'running':
         status = 'pending'
         human_status = 'running'
 
-    pr_id = str(msg['rev']).partition('PR-')[2]
-    if not pr_id:
+    # test error messages
+    elif pipeline_state == 'error':
+        status = 'error'
+        human_status = 'errored'
+
+    if 'id' not in msg['artifact']:
         print(
-            'Invalid revision: %s, could not extract the PR id from it' %
-            msg['rev'])
+            'Invalid message: %s, could not extract the PR id from it' %
+            msg['artifact'])
         return
+    pr_id = str(msg['artifact']['id'])
 
     data = {
         'username': 'Fedora CI',
         'status': status,
         'comment': 'Package tests%s: %s' % (commit_hash_text, human_status),
-        'url': msg['build_url'],
+        'url': msg['run']['url'],
         'uid': hashlib.md5(pr_id + seed).hexdigest()
     }
 
     pagure_url = 'https://src.fedoraproject.org'
     env_var = 'API_TOKEN'
 
+    namespace = msg['artifact']['repository'].split('/')[-2]
+    repo = msg['artifact']['repository'].split('/')[-1]
+
     target_url = '/'.join([
         'api',
         '0',
-        msg['namespace'],
-        msg['repo'],
+        namespace,
+        repo,
         'pull-request',
         pr_id,
         'flag'
@@ -116,9 +129,7 @@ def main(msg, pipeline_state='complete', seed='empty'):
         return 1
     else:
         print('All clear')
-        print('User-URL: %s' % pagure_url + '/' + '/'.join([
-            msg['namespace'],
-            msg['repo'],
+        print('User-URL: %s' % msg['artifact']['repository'] + '/'.join([
             'pull-request',
             pr_id,
         ]))

From a73c3b4078e9eb4fb710d792a3e8dd958a6bae30 Mon Sep 17 00:00:00 2001
From: Bruno Goncalves <bgoncalv@redhat.com>
Date: Nov 21 2019 15:42:29 +0000
Subject: [PATCH 2/2] always show commit_hash


---

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 2e39099..6503c71 100644
--- a/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py
+++ b/playbooks/roles/flag_ci_pr/files/flag_ci_pr.py
@@ -48,7 +48,6 @@ def main(msg, pipeline_state='complete', seed='empty'):
         print('Unsupported msg version, ignoring')
         return
 
-    commit_hash_text = ''
     # test complete messages
     if pipeline_state == 'complete':
         done_states = {
@@ -64,9 +63,6 @@ def main(msg, pipeline_state='complete', seed='empty'):
 
         status = done_states[state]['api']
         human_status = done_states[state]['human']
-        if 'commit_hash' in msg['artifact']:
-            commit_hash_text = ' for %s' % msg['artifact']['commit_hash'][:8]
-
     # test running messages
     elif pipeline_state == 'running':
         status = 'pending'
@@ -84,6 +80,8 @@ def main(msg, pipeline_state='complete', seed='empty'):
         return
     pr_id = str(msg['artifact']['id'])
 
+    commit_hash_text = ' for %s' % msg['artifact']['commit_hash'][:8]
+
     data = {
         'username': 'Fedora CI',
         'status': status,

