Fixed gating pipeline

Change-Id: I8e2d84b1fee9b8e6dba875a9eebe7b43878a1cd8
diff --git a/gating-pipeline.groovy b/gating-pipeline.groovy
index bd94f63..4184687 100644
--- a/gating-pipeline.groovy
+++ b/gating-pipeline.groovy
@@ -14,11 +14,9 @@
     // test if change is not already merged
     ssh.prepareSshAgentKey(CREDENTIALS_ID)
     ssh.ensureKnownHosts(GERRIT_HOST)
-    def output = ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --format=JSON change:%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER))
-    def jsonSlurper = new JsonSlurper()
-    def gerritChange = jsonSlurper.parseText(output)
+    def gerritChangeStatus = _getGerritChangeStatus(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER)
     stage("test") {
-      if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")){
+      if (gerritChangeStatus != "MERGED" && !SKIP_TEST.equals("true")){
         wrap([$class: 'AnsiColorBuildWrapper']) {
           def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
           def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
@@ -43,7 +41,7 @@
       }
     }
     stage("submit review"){
-      if(gerritChange.status == "MERGED"){
+      if(gerritChangeStatus == "MERGED"){
         common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
       }else{
         ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
@@ -63,3 +61,15 @@
 def _jobExists(jobName){
   return Jenkins.instance.items.find{it -> it.name.equals(jobName)}
 }
+
+@NonCPS
+def _getGerritChangeStatus(gerritName, gerritHost, gerritChange){
+   def output = ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --format=JSON change:%s", gerritName, gerritHost, gerritChange))
+   def jsonSlurper = new JsonSlurper()
+   def gerritChange = jsonSlurper.parseText(output)
+   if(gerritChange["status"]){
+     return gerritChange["status"]
+   }else{
+     return "ERROR"
+   }
+}
\ No newline at end of file