From df02f2bdeadee5b5d184fa9cb2e7300dbf0dee9a Mon Sep 17 00:00:00 2001
From: James Molet <jmolet@redhat.com>
Date: Aug 20 2018 16:33:17 +0000
Subject: [PATCH 1/3] new emailer


---

diff --git a/Jenkinsfile b/Jenkinsfile
index dd6c8a1..3baa4a2 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -4,6 +4,10 @@ node('master'){
 }
 
 try {
+    scmVars.GIT_AUTHOR_EMAIL = sh (
+        script: 'git --no-pager show -s --format=\'%ae\'',
+        returnStdout: true
+    ).trim()
     echo "${scmVars}"
     echo 'GIT BRANCH:'
     echo scmVars.GIT_BRANCH
@@ -17,22 +21,52 @@ try {
   currentBuild.result = 'FAILURE'
   throw e
 } finally {
+    // if result hasn't been set to failure by this point, its a success.
     def currentResult = currentBuild.result ?: 'SUCCESS'
-    def resultPercent = (currentResult == 'SUCCESS') ? '100' : '0'
-    def resultComment = (currentResult == 'SUCCESS') ? 'Build passed.' : 'Build failed.'
-    def pagurePR = scmVars.GIT_BRANCH.split('/')[-1]  // origin/pr/1234 -> 1234
-    def pagureRepo = new URL(scmVars.GIT_URL).getPath() - ~/^\// - ~/.git$/  // https://pagure.io/my-repo.git -> my-repo
-    if (pagurePR != 'master') {
-      build job: 'pr-updater',
-          parameters: [
-                    //    [$class: 'StringParameterValue', name: 'PAGURE_REPO', value: 'https://pagure.io'],  // not needed if https://pagure.io
-                       [$class: 'StringParameterValue', name: 'PAGURE_PR', value: pagurePR],
-                       [$class: 'StringParameterValue', name: 'PAGURE_REPO', value: pagureRepo],
-                       [$class: 'StringParameterValue', name: 'PERCENT_PASSED', value: resultPercent],
-                       [$class: 'StringParameterValue', name: 'COMMENT', value: resultComment],
-                       [$class: 'StringParameterValue', name: 'REFERENCE_URL', value: "${env.BUILD_URL}"],
-                       [$class: 'StringParameterValue', name: 'REFERENCE_JOB_NAME', value: "${env.JOB_NAME}"]
-                      ]
+    def branch = scmVars.GIT_BRANCH.split('/')[-1]  // origin/pr/1234 -> 1234
+
+    // send pass/fail email
+    def previousResult = currentBuild.previousBuild?.result
+    def SUBJECT = ''
+    def RECIEPENT = (branch == 'master') ? ownership.job.primaryOwnerEmail : scmVars.GIT_AUTHOR_EMAIL
+    def BODY = "Build URL: ${env.BUILD_URL}"
+    if (branch ==~ /[0-9]+/){
+        BODY = BODY + "\nPull Request: https://pagure.io/greenwave/pull-request/${branch}"
+    }
+
+    if (previousResult == 'FAILURE' && currentResult == 'SUCCESS') {
+        SUBJECT = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} fixed."
+    }
+    else if (previousResult == 'SUCCESS' && currentResult == 'FAILURE' ) {
+        SUBJECT = "Jenkins job ${env.JOB_NAME} #${env.BUILD_NUMBER} failed."
+    }
+
+    if (SUBJECT != '') {
+        emailext to: RECIEPENT,
+                 subject: SUBJECT,
+                 body: BODY
+    }
+
+    // update Pagure PR status
+    if (branch ==~ /[0-9]+/) {  // PR's will only be numbers on pagure
+        def resultPercent = (currentResult == 'SUCCESS') ? '100' : '0'
+        def resultComment = (currentResult == 'SUCCESS') ? 'Build passed.' : 'Build failed.'
+        def pagureRepo = new URL(scmVars.GIT_URL).getPath() - ~/^\// - ~/.git$/  // https://pagure.io/my-repo.git -> my-repo
+
+        withCredentials([string(credentialsId: "${env.PAGURE_API_TOKEN}", variable: 'TOKEN')]) {
+        build job: 'pagure-PR-status-updater',
+            propagate: false,
+            parameters: [
+                // [$class: 'StringParameterValue', name: 'PAGURE_REPO', value: 'https://pagure.io'],  // not needed if https://pagure.io
+                [$class: 'StringParameterValue', name: 'PAGURE_PR', value: branch],
+                [$class: 'StringParameterValue', name: 'PAGURE_REPO', value: pagureRepo],
+                [$class: 'StringParameterValue', name: 'PERCENT_PASSED', value: resultPercent],
+                [$class: 'StringParameterValue', name: 'COMMENT', value: resultComment],
+                [$class: 'StringParameterValue', name: 'REFERENCE_URL', value: "${env.BUILD_URL}"],
+                [$class: 'StringParameterValue', name: 'REFERENCE_JOB_NAME', value: "${env.JOB_NAME}"],
+                [$class: 'hudson.model.PasswordParameterValue', name: 'TOKEN', value: "${env.TOKEN}"]
+                        ]
+        }
     }
 }
 

From fb43abdbbe903d04e222a213b7fc606a9b22471d Mon Sep 17 00:00:00 2001
From: James Molet <jmolet@redhat.com>
Date: Aug 20 2018 16:37:10 +0000
Subject: [PATCH 2/3] new emailer


---

diff --git a/Jenkinsfile b/Jenkinsfile
index 3baa4a2..54f0fba 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -4,19 +4,18 @@ node('master'){
 }
 
 try {
-    scmVars.GIT_AUTHOR_EMAIL = sh (
-        script: 'git --no-pager show -s --format=\'%ae\'',
-        returnStdout: true
-    ).trim()
-    echo "${scmVars}"
-    echo 'GIT BRANCH:'
-    echo scmVars.GIT_BRANCH
     node('master') {
         checkout scm
+        scmVars.GIT_AUTHOR_EMAIL = sh (
+            script: 'git --no-pager show -s --format=\'%ae\'',
+            returnStdout: true
+        ).trim()
         stage('Cat File') {
             sh "cat somefile"
         }
     }
+    echo "SCM VARS: ${scmVars}"
+
 } catch(e) {
   currentBuild.result = 'FAILURE'
   throw e

From f20dd15a419fa752454aa863eff84ad61a34e38f Mon Sep 17 00:00:00 2001
From: James Molet <jmolet@redhat.com>
Date: Aug 20 2018 18:10:34 +0000
Subject: [PATCH 3/3] new emailer


---

diff --git a/Jenkinsfile b/Jenkinsfile
index 54f0fba..eeb585f 100644
--- a/Jenkinsfile
+++ b/Jenkinsfile
@@ -53,7 +53,7 @@ try {
         def pagureRepo = new URL(scmVars.GIT_URL).getPath() - ~/^\// - ~/.git$/  // https://pagure.io/my-repo.git -> my-repo
 
         withCredentials([string(credentialsId: "${env.PAGURE_API_TOKEN}", variable: 'TOKEN')]) {
-        build job: 'pagure-PR-status-updater',
+        build job: 'pr-updater',
             propagate: false,
             parameters: [
                 // [$class: 'StringParameterValue', name: 'PAGURE_REPO', value: 'https://pagure.io'],  // not needed if https://pagure.io

