Refactoring in test system reclass

Change-Id: I20f31eefba7b5093647ae6fa0edfff7c3ac7a4c0
diff --git a/test-system-reclass-pipeline.groovy b/test-system-reclass-pipeline.groovy
index a9dcba4..4c490da 100644
--- a/test-system-reclass-pipeline.groovy
+++ b/test-system-reclass-pipeline.groovy
@@ -8,29 +8,57 @@
     gerritCredentials = "gerrit"
 }
 
+def gerritRef
+try {
+  gerritRef = GERRIT_REFSPEC
+} catch (MissingPropertyException e) {
+  gerritRef = null
+}
+
+def defaultGitRef, defaultGitUrl
+try {
+    defaultGitRef = DEFAULT_GIT_REF
+    defaultGitUrl = DEFAULT_GIT_URL
+} catch (MissingPropertyException e) {
+    defaultGitRef = null
+    defaultGitUrl = null
+}
+def checkouted = false
+
 try {
   stage("Checkout") {
     node() {
-      gerrit.gerritPatchsetCheckout ([
-        credentialsId : gerritCredentials
-      ])
+      if (gerritRef) {
+        // job is triggered by Gerrit
+        checkouted = gerrit.gerritPatchsetCheckout ([
+          credentialsId : gerritCredentials
+        ])
+        // change defaultGit variables if job triggered from Gerrit
+        defaultGitRef = GERRIT_REFSPEC
+        defaultGitUrl = "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git"
+      } else if(defaultGitRef && defaultGitUrl) {
+          checkouted = gerrit.gerritPatchsetCheckout(defaultGitUrl, defaultGitRef, "HEAD", gerritCredentials)
+      }
     }
   }
 
   stage("Test") {
-    def branches = [:]
-    def testModels = TEST_MODELS.split(',')
-
-    for (int i = 0; i < testModels.size(); i++) {
-      def cluster = testModels[i]
-      branches["${cluster}"] = {
-        build job: "test-salt-model-${cluster}", parameters: [
-          [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git"],
-          [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
-        ]
-      }
+    if(checkouted){
+      def branches = [:]
+      def testModels = TEST_MODELS.split(',')
+        for (int i = 0; i < testModels.size(); i++) {
+          def cluster = testModels[i]
+          branches["${cluster}"] = {
+            build job: "test-salt-model-${cluster}", parameters: [
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: defaultGitUrl],
+              [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: defaultGitRef]
+            ]
+          }
+        }
+      parallel branches
+    }else{
+       common.errorMsg("Cannot checkout gerrit patchset, GERRIT_REFSPEC and DEFAULT_GIT_REF is null")
     }
-    parallel branches
   }
 } catch (Throwable e) {
     // If there was an error or exception thrown, the build failed