From 0df6f7833e6275427c2201363042999c95f1531f Mon Sep 17 00:00:00 2001
From: Pavlina Bortlova <pbortlov@redhat.com>
Date: Jun 16 2020 07:46:17 +0000
Subject: Send message if pull-request build fails


---

diff --git a/roles/c3i/templates/build-workflow/build-pagure.Jenkinsfile b/roles/c3i/templates/build-workflow/build-pagure.Jenkinsfile
index 98212a8..df52540 100644
--- a/roles/c3i/templates/build-workflow/build-pagure.Jenkinsfile
+++ b/roles/c3i/templates/build-workflow/build-pagure.Jenkinsfile
@@ -97,28 +97,20 @@ pipeline {
           // with special bash characters (like whitespaces, #, etc).
           // https://bugzilla.redhat.com/show_bug.cgi?id=1625518
           currentBuild.displayName = "PR#${env.PR_NO}"
-          // To enable HTML syntax in build description, go to `Jenkins/Global Security/Markup Formatter` and select 'Safe HTML'.
-          def pagureLink = """<a href="${env.PR_URL}">${currentBuild.displayName}</a>"""
-          try {
-            def prInfo = pagure.getPR(env.PR_NO)
-            pagureLink = """<a href="${env.PR_URL}">PR#${env.PR_NO}: ${escapeHtml(prInfo.title)}</a>"""
-            // set PR status to Pending
-            if (params.PAGURE_API_KEY_SECRET_NAME)
-              pagure.setBuildStatusOnPR(null, 'Building...')
-          } catch (Exception e) {
-            echo "Error using pagure API: ${e}"
+
+          def prInfo = pagure.getPR(env.PR_NO)
+          def pagureLink = """<a href="${env.PR_URL}">PR#${env.PR_NO}: ${escapeHtml(prInfo.title)}</a>"""
+          // set PR status to Pending
+          if (params.PAGURE_API_KEY_SECRET_NAME) {
+            pagure.setBuildStatusOnPR(null, 'Building...')
           }
           currentBuild.description = pagureLink
           {% else %}
           currentBuild.displayName = "${env.GIT_REPO_REF}: ${env.GIT_COMMIT.substring(0, 7)}"
           currentBuild.description = """<a href="${pagure_repo_home}/c/${env.GIT_COMMIT}">${currentBuild.displayName}</a>"""
           if (params.PAGURE_API_KEY_SECRET_NAME) {
-            try {
-              pagure.flagCommit('pending', null, 'Building...')
-              echo "Updated commit ${env.GIT_COMMIT} status to PENDING."
-            } catch (e) {
-              echo "Error updating commit ${env.GIT_COMMIT} status to PENDING: ${e}"
-            }
+            pagure.flagCommit('pending', null, 'Building...')
+            echo "Updated commit ${env.GIT_COMMIT} status to PENDING."
           }
           {% endif %}
           {% include "get_paas_domain.groovy" %}
@@ -132,32 +124,28 @@ pipeline {
     {% include "build-workflow/build-default.groovy" %}
     {% endif %}
 
-  } // stages
-  post {
-    success {
-      script {
-        // on pre-merge workflow success
-        if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == "premerge") {
-          try {
+    stage('Report results') {
+      steps {
+        script {
+          if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == "premerge") {
             pagure.setBuildStatusOnPR(100, 'Build passed.')
             echo "Updated PR #${env.PR_NO} status to PASS."
-          } catch (e) {
-            echo "Error updating PR #${env.PR_NO} status to PASS: ${e}"
           }
-        }
-        // on post-merge workflow success
-        if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == 'postmerge') {
-          try {
+          // on post-merge workflow success
+          if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == 'postmerge') {
             pagure.flagCommit('success', 100, 'Build passed.')
             echo "Updated commit ${env.GIT_COMMIT} status to PASS."
-          } catch (e) {
-            echo "Error updating commit ${env.GIT_COMMIT} status to PASS: ${e}"
           }
-        }
-      } // script
-    } // success
+        } // script
+      } // steps
+    } // stage
+  } // stages
+  post {
     failure {
       script {
+        def subject = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."
+        def body = "Build URL: ${env.BUILD_URL}. "
+
         // on pre-merge workflow failure
         if (params.PAGURE_API_KEY_SECRET_NAME && env.C3I_JOB_WORKFLOW == 'premerge') {
           // updating Pagure PR flag
@@ -166,6 +154,9 @@ pipeline {
             echo "Updated PR #${env.PR_NO} status to FAILURE."
           } catch (e) {
             echo "Error updating PR #${env.PR_NO} status to FAILURE: ${e}"
+            if (params.MAIL_ADDRESS){
+              emailext to: params.MAIL_ADDRESS, subject: subject, body: body
+            }
           }
           // making a comment
           try {
@@ -176,6 +167,9 @@ pipeline {
             echo "Comment made."
           } catch (e) {
             echo "Error making a comment on PR #${env.PR_NO}: ${e}"
+            if (params.MAIL_ADDRESS){
+              emailext to: params.MAIL_ADDRESS, subject: subject, body: body
+            }
           }
         }
         // on post-merge workflow failure
@@ -187,18 +181,14 @@ pipeline {
               echo "Updated commit ${env.GIT_COMMIT} status to FAILURE."
             } catch (e) {
               echo "Error updating commit ${env.GIT_COMMIT} status to FAILURE: ${e}"
+              if (params.MAIL_ADDRESS){
+                emailext to: params.MAIL_ADDRESS, subject: subject, body: body
+              }
             }
           }
           // sending email
           if (params.MAIL_ADDRESS){
-            try {
-              def recipient = params.MAIL_ADDRESS
-              def subject = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."
-              def body = "Build URL: ${env.BUILD_URL}"
-              emailext to: recipient, subject: subject, body: body
-            } catch (e) {
-              echo "Error sending email: ${e}"
-            }
+            emailext to: params.MAIL_ADDRESS, subject: subject, body: body
           }
         }
       } // script

