From 02ebaf53fad159fdedb327617b2380c0349b0fe8 Mon Sep 17 00:00:00 2001
From: Michal Kovarik <mkovarik@redhat.com>
Date: Mar 16 2020 12:09:15 +0000
Subject: Get message from datagrepper when not triggered by user


When job is not triggered by user and CI_MESSAGE is empty then get
message from datagrepper.

---

diff --git a/roles/c3i/defaults/main.yml b/roles/c3i/defaults/main.yml
index 0a12b45..5455855 100644
--- a/roles/c3i/defaults/main.yml
+++ b/roles/c3i/defaults/main.yml
@@ -54,6 +54,11 @@ c3i_skip_service_accounts: false
 c3i_quay_address: quay.io
 c3i_quay_namespace: factory2
 
+c3i_datagrepper_url: https://datagrepper.engineering.redhat.com
+c3i_messaging_topic_consumer_part: "Consumer.rh-jenkins-ci-plugin"
+c3i_messaging_greenwave_topic_virtualtopic_part: "VirtualTopic.eng.greenwave.decision.update"
+c3i_messaging_repotracker_topic_virtualtopic_part: "VirtualTopic.eng.repotracker.container.tag.>"
+
 c3i_dev_image_tag: "latest"
 
 c3i_lib_branch: master
diff --git a/roles/c3i/templates/greenwave-promote.Jenkinsfile b/roles/c3i/templates/greenwave-promote.Jenkinsfile
index 6a7add9..b564224 100644
--- a/roles/c3i/templates/greenwave-promote.Jenkinsfile
+++ b/roles/c3i/templates/greenwave-promote.Jenkinsfile
@@ -43,7 +43,29 @@ pipeline {
             // e.g. quay.io/factory2/waiverdb@sha256:35201c572fc8a137862b7a256476add8d7465fa5043d53d117f4132402f8ef6b
             env.IMAGE = "${params.SOURCE_CONTAINER_REPO}@${digest}"
           } else if (!params.IMAGE) {
-            error("This build is not started by a CI message. Only configurations were done.")
+            // Triggred by CI_MESSAGE but it's empty - CID-5291
+            if (!currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')) {
+              def datagrepperdata = sh(returnStdout: true, script: "curl -k {{ c3i_datagrepper_url }}/raw?delta=300&topic=/topic/{{ c3i_messaging_greenwave_topic_virtualtopic_part }}&contains=${params.DECISION_CONTEXT_REGEX}")
+              def messages = readJSON text: datagrepperdata
+              messages.raw_messages.any {
+                def msg = it.msg
+                if ( msg?.decision_context.find(/${env.DECISION_CONTEXT_REGEX}/) &&
+                     msg?.subject_type == 'container-image' &&
+                     msg?.policies_satisfied == true &&
+                     msg?.subject_identifier.find(/${env.SUBJECT_IDENTIFIER_REGEX}/)
+                   ){
+                  def digest = (msg.subject_identifier =~ /@(sha256:\w+)$/)[0][1]
+                  env.IMAGE = "${params.SOURCE_CONTAINER_REPO}@${digest}"
+                  return true
+                }
+              }
+              if (!env.IMAGE) {
+                error('Triggered by CI_MESSAGE but message is empty and not found in datagrepper')
+              }
+            }
+            else {
+              error("This build is not started by a CI message. Only configurations were done.")
+            }
           }
           echo "Starting promotion of image ${env.IMAGE} to ${env.PROMOTING_DESTINATION}:${params.TARGET_TAG}..."
           // Setting up registry credentials
diff --git a/roles/c3i/templates/greenwave-promote.yml b/roles/c3i/templates/greenwave-promote.yml
index efc5996..e4e18ac 100644
--- a/roles/c3i/templates/greenwave-promote.yml
+++ b/roles/c3i/templates/greenwave-promote.yml
@@ -36,7 +36,7 @@ spec:
       - name: SOURCE_CONTAINER_REPO
         value: {{ job_vars.source_container_repo_prefix }}/{{ item }}
       - name: MESSAGING_TOPIC
-        value: {{ job_vars.messaging_greenwave_topic_pre }}-{{ item }}{{ c3i_test_subscriber }}-{{ job_vars.messaging_greenwave_topic_post }}
+        value: {{ c3i_messaging_topic_consumer_part }}.c3i-{{ item }}-{{ job_vars.name_post }}{{ c3i_test_subscriber }}.{{ c3i_messaging_greenwave_topic_virtualtopic_part }}
       - name: MAIL_ADDRESS
         value: {{ c3i_mail_address }}
       # CI_MESSAGE is used internally by JMS messaging plugin
diff --git a/roles/c3i/templates/jobs/greenwave-promote-to-prod.yml b/roles/c3i/templates/jobs/greenwave-promote-to-prod.yml
index 93f9d88..5ece4f9 100644
--- a/roles/c3i/templates/jobs/greenwave-promote-to-prod.yml
+++ b/roles/c3i/templates/jobs/greenwave-promote-to-prod.yml
@@ -1,8 +1,6 @@
 ---
 name_post: "greenwave-promote-to-prod"
 template: greenwave-promote.yml
-messaging_greenwave_topic_pre: "Consumer.rh-jenkins-ci-plugin.c3i"
-messaging_greenwave_topic_post: "greenwave-promote-to-prod.VirtualTopic.eng.greenwave.decision.update"
 decision_context_regex: c3i_promote_stage_to_prod
 target_tag: prod
 promoting_destination_prefix: "{{ c3i_quay_address }}/{{ c3i_quay_namespace }}"
diff --git a/roles/c3i/templates/jobs/greenwave-promote-to-stage.yml b/roles/c3i/templates/jobs/greenwave-promote-to-stage.yml
index d2f5fd2..3caf8c8 100644
--- a/roles/c3i/templates/jobs/greenwave-promote-to-stage.yml
+++ b/roles/c3i/templates/jobs/greenwave-promote-to-stage.yml
@@ -1,8 +1,6 @@
 ---
 name_post: "greenwave-promote-to-stage"
 template: greenwave-promote.yml
-messaging_greenwave_topic_pre: "Consumer.rh-jenkins-ci-plugin.c3i-"
-messaging_greenwave_topic_post: "greenwave-promote-to-stage.VirtualTopic.eng.greenwave.decision.update"
 decision_context_regex: c3i_promote_dev_to_stage
 target_tag: stage
 promoting_destination_prefix: "{{ c3i_quay_address }}/{{ c3i_quay_namespace }}"
diff --git a/roles/c3i/templates/jobs/trigger-on-latest-tag.yml b/roles/c3i/templates/jobs/trigger-on-latest-tag.yml
index 6070470..c0f24f0 100644
--- a/roles/c3i/templates/jobs/trigger-on-latest-tag.yml
+++ b/roles/c3i/templates/jobs/trigger-on-latest-tag.yml
@@ -1,6 +1,5 @@
 ---
 name: "{{ c3i_component }}-trigger-on-latest-tag"
 template: trigger-on-tag.yml
-messaging_repotracker_topic: Consumer.rh-jenkins-ci-plugin.c3i-{{ c3i_test_subscriber | default(c3i_component) }}-trigger-on-latest-tag.VirtualTopic.eng.repotracker.container.tag.>
 environment: stage
 tracked_tag: latest
diff --git a/roles/c3i/templates/jobs/trigger-on-stage-tag.yml b/roles/c3i/templates/jobs/trigger-on-stage-tag.yml
index f03ecb7..3d8a43a 100644
--- a/roles/c3i/templates/jobs/trigger-on-stage-tag.yml
+++ b/roles/c3i/templates/jobs/trigger-on-stage-tag.yml
@@ -1,6 +1,5 @@
 ---
 name: "{{ c3i_component }}-trigger-on-stage-tag"
 template: trigger-on-tag.yml
-messaging_repotracker_topic: Consumer.rh-jenkins-ci-plugin.c3i-{{ c3i_test_subscriber | default(c3i_component) }}-trigger-on-stage-tag.VirtualTopic.eng.repotracker.container.tag.>
 environment: prod
 tracked_tag: stage
diff --git a/roles/c3i/templates/trigger-on-tag.Jenkinsfile b/roles/c3i/templates/trigger-on-tag.Jenkinsfile
index 526485c..637f10b 100644
--- a/roles/c3i/templates/trigger-on-tag.Jenkinsfile
+++ b/roles/c3i/templates/trigger-on-tag.Jenkinsfile
@@ -32,7 +32,26 @@ pipeline {
           } else if (params.IMAGE) {
             env.PIPELINE_ID = "c3i-{{ c3i_component }}-tag-${params.TRACKED_TAG}-manual-${UUID.randomUUID().toString().substring(0, 4)}"
           } else {
-            error("This build is not started by a CI message. Only configurations were done.")
+            // Triggred by CI_MESSAGE but it's empty - CID-5291
+            if (!currentBuild.getBuildCauses('hudson.model.Cause$UserIdCause')) {
+              def datagrepperdata = sh(returnStdout: true, script: "curl -k {{ c3i_datagrepper_url }}/raw?delta=300&category=repotracker&contains=${params.TRACKED_CONTAINER_REPO}")
+              def messages = readJSON text: datagrepperdata
+              messages.raw_messages.any {
+                def msg = it.msg
+                if ( msg?.action in ('added', 'updated') &&
+                     msg?.tag == params.TRACKED_TAG &&
+                     msg?.repo == params.TRACKED_CONTAINER_REPO
+                   ){
+                  echo "Tag :${msg.tag} is ${msg.action} in ${msg.repo}. New digest: ${msg.digest}"
+                  env.IMAGE = "${msg.repo}@${msg.digest}"
+                  env.PIPELINE_ID = "c3i-{{ c3i_component }}-tag-${params.TRACKED_TAG}-${msg.digest[-9..-1]}"
+                  return true
+                }
+              }
+              if (!env.IMAGE) {
+                error('Triggered by CI_MESSAGE but message is empty and not found in datagrepper')
+              }
+            }
           }
           echo "Triggering a job to test if ${env.IMAGE} meets all criteria of desired tag :${params.TRACKED_TAG}"
           env.IMAGE_IS_SCRATCH = false
diff --git a/roles/c3i/templates/trigger-on-tag.yml b/roles/c3i/templates/trigger-on-tag.yml
index d98b5c1..d7654b8 100644
--- a/roles/c3i/templates/trigger-on-tag.yml
+++ b/roles/c3i/templates/trigger-on-tag.yml
@@ -16,7 +16,7 @@ spec:
       - name: GIT_REPO_REF
         value: {{ c3i_git_main_branch }}
       - name: MESSAGING_TOPIC
-        value: {{ job_vars.messaging_repotracker_topic }}
+        value: {{ c3i_messaging_topic_consumer_part }}.c3i-{{ job_vars.name }}{{ c3i_test_subscriber }}.{{ c3i_messaging_repotracker_topic_virtualtopic_part }}
       - name: IMAGE
         value:
       - name: OPENSHIFT_CLOUD_NAME

