Improved gerrit change verifing before tests will be started

Change-Id: Ie00f987e4f297b00709ac2673fc679c795296ac1
diff --git a/build.gradle b/build.gradle
index 3847c12..4eb83d0 100644
--- a/build.gradle
+++ b/build.gradle
@@ -6,12 +6,12 @@
 sourceSets {
   main {
     groovy {
-      srcDirs = ['src', 'vars']
+      srcDirs = ['.']
     }
   }
 }
 
-compileGroovy.enabled = true
+compileGroovy.enabled = false
 
 repositories {
   maven {
diff --git a/change-config.groovy b/change-config.groovy
index 69f7519..805e230 100644
--- a/change-config.groovy
+++ b/change-config.groovy
@@ -86,8 +86,5 @@
     } catch (Throwable e) {
         currentBuild.result = 'FAILURE'
         throw e
-    } finally {
-        // send notification
-        //common.sendNotification(currentBuild.result,HEAT_STACK_NAME,["slack"])
     }
 }
diff --git a/gating-pipeline.groovy b/gating-pipeline.groovy
index 3acc574..c056f38 100644
--- a/gating-pipeline.groovy
+++ b/gating-pipeline.groovy
@@ -13,7 +13,7 @@
     // test if change is not already merged
     ssh.prepareSshAgentKey(CREDENTIALS_ID)
     ssh.ensureKnownHosts(GERRIT_HOST)
-    def gerritChange = common.parseJSON(ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --format=JSON change:%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER)))
+    def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
     stage("test") {
       if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")){
         wrap([$class: 'AnsiColorBuildWrapper']) {
@@ -51,8 +51,6 @@
      // If there was an error or exception thrown, the build failed
      currentBuild.result = "FAILURE"
      throw e
-  } finally {
-     //common.sendNotification(currentBuild.result,"",["slack"])
   }
 }
 
diff --git a/git-mirror-2way-pipeline.groovy b/git-mirror-2way-pipeline.groovy
index e70e9e7..1466e88 100644
--- a/git-mirror-2way-pipeline.groovy
+++ b/git-mirror-2way-pipeline.groovy
@@ -10,7 +10,7 @@
       }
       dir("target") {
         checkout changelog: true, poll: true,
-          scm: [$class: 'GitSCM', branches: pollBranches, doGenerateSubmoduleConfigurations: false, submoduleCfg: [],
+          scm: [$class: 'GitSCM', branches: pollBranches, doGenerateSubmoduleConfigurations: false,
           extensions: [[$class: 'CleanCheckout']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: CREDENTIALS_ID, url: TARGET_URL]]]
       }
       dir("source") {
diff --git a/git-mirror-pipeline.groovy b/git-mirror-pipeline.groovy
index 3a0f892..10fa9a0 100644
--- a/git-mirror-pipeline.groovy
+++ b/git-mirror-pipeline.groovy
@@ -10,7 +10,7 @@
       }
       dir("source") {
         checkout changelog: true, poll: true,
-          scm: [$class: 'GitSCM', branches: pollBranches, doGenerateSubmoduleConfigurations: false, submoduleCfg: [], 
+          scm: [$class: 'GitSCM', branches: pollBranches, doGenerateSubmoduleConfigurations: false,
           extensions: [[$class: 'CleanCheckout']], submoduleCfg: [], userRemoteConfigs: [[credentialsId: CREDENTIALS_ID, url: SOURCE_URL]]]
         git.mirrorGit(SOURCE_URL, TARGET_URL, CREDENTIALS_ID, BRANCHES)
       }
diff --git a/test-groovy-pipeline.groovy b/test-groovy-pipeline.groovy
index edd549a..918fc98 100644
--- a/test-groovy-pipeline.groovy
+++ b/test-groovy-pipeline.groovy
@@ -63,7 +63,9 @@
             if(errLog){
                 common.errorMsg("Error log: ${errLog}")
             }
-        }catch(ex){}
+        }catch(ex){
+          common.errorMsg("Exception occured while reading codenarc output file", ex)
+        }
         throw e
     } finally {
         // send notification
diff --git a/test-salt-models-pipeline.groovy b/test-salt-models-pipeline.groovy
index d43c217..feed26f 100644
--- a/test-salt-models-pipeline.groovy
+++ b/test-salt-models-pipeline.groovy
@@ -27,15 +27,21 @@
     defaultGitUrl = null
 }
 def checkouted = false
+def merged = false
 
 node("python") {
   try{
     stage("checkout") {
       if (gerritRef) {
         // job is triggered by Gerrit
-        checkouted = gerrit.gerritPatchsetCheckout ([
-          credentialsId : CREDENTIALS_ID
-        ])
+        // test if change aren't already merged
+        def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
+        merged = gerritChange.status == "MERGED"
+        if(!merged){
+          checkouted = gerrit.gerritPatchsetCheckout ([
+            credentialsId : CREDENTIALS_ID
+          ])
+        }
       } else if(defaultGitRef && defaultGitUrl) {
           checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", CREDENTIALS_ID)
       }
@@ -48,15 +54,19 @@
           }
           ssh.agentSh("git submodule init; git submodule sync; git submodule update --recursive")
         }
-      }else{
+      }else if(!merged){
         throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
       }
     }
     stage("test") {
-      if(checkouted){
-        timeout(1440) {
-          wrap([$class: 'AnsiColorBuildWrapper']) {
-            sh("make test")
+      if(merged){
+        common.successMsg("Gerrit change is already merged, no need to test them")
+      }else{
+        if(checkouted){
+          timeout(1440) {
+            wrap([$class: 'AnsiColorBuildWrapper']) {
+              sh("make test")
+            }
           }
         }
       }
diff --git a/test-system-reclass-pipeline.groovy b/test-system-reclass-pipeline.groovy
index 65fce27..4382204 100644
--- a/test-system-reclass-pipeline.groovy
+++ b/test-system-reclass-pipeline.groovy
@@ -24,15 +24,20 @@
     defaultGitUrl = null
 }
 def checkouted = false
-
+def merged = false
 try {
   stage("Checkout") {
     node() {
       if (gerritRef) {
         // job is triggered by Gerrit
-        checkouted = gerrit.gerritPatchsetCheckout ([
-          credentialsId : gerritCredentials
-        ])
+        // test if change aren't already merged
+        def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID)
+        merged = gerritChange.status == "MERGED"
+        if(!merged){
+          checkouted = gerrit.gerritPatchsetCheckout ([
+            credentialsId : gerritCredentials
+          ])
+        }
         // change defaultGit variables if job triggered from Gerrit
         defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"
       } else if(defaultGitRef && defaultGitUrl) {
@@ -42,22 +47,26 @@
   }
 
   stage("Test") {
-    if(checkouted){
-      def branches = [:]
-      def testModels = TEST_MODELS.split(',')
-        for (int i = 0; i < testModels.size(); i++) {
-          def cluster = testModels[i]
-          def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
-          branches["${cluster}"] = {
-            build job: "test-salt-model-${cluster}", parameters: [
-              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
-              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"]
-            ]
-          }
-        }
-      parallel branches
+    if(merged){
+      common.successMsg("Gerrit change is already merged, no need to test them")
     }else{
-       throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
+      if(checkouted){
+        def branches = [:]
+        def testModels = TEST_MODELS.split(',')
+          for (int i = 0; i < testModels.size(); i++) {
+            def cluster = testModels[i]
+            def clusterGitUrl = defaultGitUrl.substring(0, defaultGitUrl.lastIndexOf("/") + 1) + cluster
+            branches["${cluster}"] = {
+              build job: "test-salt-model-${cluster}", parameters: [
+                [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: clusterGitUrl],
+                [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: "HEAD"]
+              ]
+            }
+          }
+        parallel branches
+      }else{
+         throw new Exception("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
+      }
     }
   }
 } catch (Throwable e) {
diff --git a/update-package.groovy b/update-package.groovy
index b37fe22..9441751 100644
--- a/update-package.groovy
+++ b/update-package.groovy
@@ -99,7 +99,5 @@
         // If there was an error or exception thrown, the build failed
         currentBuild.result = "FAILURE"
         throw e
-    } finally {
-        // common.sendNotification(currentBuild.result,"",["slack"])
     }
 }