Added second checkout function for checkouting by hand

Change-Id: I01faf715fa52e7da63054e5a5de1a72f1cfbc72b
diff --git a/src/com/mirantis/mk/Gerrit.groovy b/src/com/mirantis/mk/Gerrit.groovy
index 6de6b1d..7cbd1fd 100644
--- a/src/com/mirantis/mk/Gerrit.groovy
+++ b/src/com/mirantis/mk/Gerrit.groovy
@@ -1,5 +1,6 @@
 package com.mirantis.mk
 
+import java.util.regex.Pattern
 /**
  * Gerrit functions
  *
@@ -13,6 +14,7 @@
  *          - credentialsId, id of user which should make checkout
  *          - withMerge, prevent detached mode in repo
  *          - withWipeOut, wipe repository and force clone
+ * @return boolean result
  *
  * Usage example:
  * //anonymous gerrit checkout
@@ -42,47 +44,38 @@
     def depth = config.get('depth', 0)
     def timeout = config.get('timeout', 20)
 
-    // default parameters
-    def scmExtensions = [
-        [$class: 'CleanCheckout'],
-        [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']],
-        [$class: 'CheckoutOption', timeout: timeout],
-        [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout]
-    ]
-    def scmUserRemoteConfigs = [
-        name: 'gerrit',
-        refspec: gerritRefSpec
-    ]
+    if(_validGerritConfig(config)){
+        // default parameters
+        def scmExtensions = [
+            [$class: 'CleanCheckout'],
+            [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']],
+            [$class: 'CheckoutOption', timeout: timeout],
+            [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout]
+        ]
+        def scmUserRemoteConfigs = [
+            name: 'gerrit',
+            refspec: gerritRefSpec
+        ]
 
-    if (credentials == '') {
-        // then try to checkout in anonymous mode
-        scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}")
-    } else {
-        // else use ssh checkout
-        scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git")
-        scmUserRemoteConfigs.put('credentialsId',credentials)
-    }
+        if (credentials == '') {
+            // then try to checkout in anonymous mode
+            scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}")
+        } else {
+            // else use ssh checkout
+            scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git")
+            scmUserRemoteConfigs.put('credentialsId',credentials)
+        }
 
-    // if we need to "merge" code from patchset to GERRIT_BRANCH branch
-    if (merge) {
-        scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"])
-    }
-    // we need wipe workspace before checkout
-    if (wipe) {
-        scmExtensions.add([$class: 'WipeWorkspace'])
-    }
+        // if we need to "merge" code from patchset to GERRIT_BRANCH branch
+        if (merge) {
+            scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"])
+        }
+        // we need wipe workspace before checkout
+        if (wipe) {
+            scmExtensions.add([$class: 'WipeWorkspace'])
+        }
 
-    if (path == "") {
-        checkout(
-            scm: [
-                $class: 'GitSCM',
-                branches: [[name: "${gerritBranch}"]],
-                extensions: scmExtensions,
-                userRemoteConfigs: [scmUserRemoteConfigs]
-            ]
-        )
-    } else {
-        dir(path) {
+        if (path == "") {
             checkout(
                 scm: [
                     $class: 'GitSCM',
@@ -91,6 +84,52 @@
                     userRemoteConfigs: [scmUserRemoteConfigs]
                 ]
             )
+        } else {
+            dir(path) {
+                checkout(
+                    scm: [
+                        $class: 'GitSCM',
+                        branches: [[name: "${gerritBranch}"]],
+                        extensions: scmExtensions,
+                        userRemoteConfigs: [scmUserRemoteConfigs]
+                    ]
+                )
+            }
         }
+        return true
     }
+    return false
+}
+/**
+ * Execute git clone and checkout stage from gerrit review
+ *
+ * @param gitUrl git url with scheme
+ *    "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git
+ * @param gitRef git ref spec
+ * @return boolean result
+ */
+def gerritPatchsetCheckout(gitUrl, gitRef) {
+    def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)")
+    def gitUrlMatcher = gitUrlPattern.matcher(gitUrl)
+    if(gitUrlMatcher.find()){
+        gerritPatchsetCheckout([
+          credentialsId : CREDENTIALS_ID,
+          gerritRefSpec: gitRef,
+          gerritScheme: gitUrlMatcher.group(1),
+          gerritName: gitUrlMatcher.group(2),
+          gerritHost: gitUrlMatcher.group(3),
+          gerritPort: gitUrlMatcher.group(4),
+          gerritProject: gitUrlMatcher.group(5)
+        ])
+        return true
+    }
+    return false
+}
+
+def _validGerritConfig(LinkedHashMap config){
+    return config.get("gerritScheme","") != "" &&
+           config.get("gerritName","") != "" &&
+           config.get("gerritHost","") != "" &&
+           config.get("gerritPort","") != "" &&
+           config.get("gerritProject","") != ""
 }
\ No newline at end of file