Add ability for anonymous gerrit code checkout

Change-Id: I80eb5c651ff8a481b60616cb7f3170125e22387a
diff --git a/src/com/mirantis/mcp/Git.groovy b/src/com/mirantis/mcp/Git.groovy
index 0e861fa..b18be10 100644
--- a/src/com/mirantis/mcp/Git.groovy
+++ b/src/com/mirantis/mcp/Git.groovy
@@ -104,22 +104,42 @@
  *          - withWipeOut, wipe repository and force clone
  *
  * Usage example:
+ * //anonymous gerrit checkout
+ * def gitFunc = new com.mirantis.mcp.Git()
+ * gitFunc.gerritPatchsetCheckout([
+ *   withMerge : true
+ * ])
  *
  * def gitFunc = new com.mirantis.mcp.Git()
  * gitFunc.gerritPatchsetCheckout([
- *   credentialsId : 'mcp-ci-gerrit'
+ *   credentialsId : 'mcp-ci-gerrit',
  *   withMerge : true
  * ])
  */
 def gerritPatchsetCheckout(LinkedHashMap config) {
   def merge = config.get('withMerge', false)
   def wipe = config.get('withWipeOut', false)
+  def credentials = config.get('credentialsId','')
 
   // default parameters
   def scmExtensions = [
     [$class: 'CleanCheckout'],
     [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
   ]
+  def scmUserRemoteConfigs = [
+        name: 'gerrit',
+        refspec: "${GERRIT_REFSPEC}"
+  ]
+
+  if (credentials == '') {
+    // then try to checkout in anonymous mode
+    scmUserRemoteConfigs.put('url',"https://${GERRIT_HOST}/${GERRIT_PROJECT}")
+  } else {
+    // else use ssh checkout
+    scmUserRemoteConfigs.put('url',"ssh://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git")
+    scmUserRemoteConfigs.put('credentialsId',credentials)
+  }
+
   // if we need to "merge" code from patchset to GERRIT_BRANCH branch
   if (merge) {
     scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
@@ -134,12 +154,7 @@
       $class: 'GitSCM',
       branches: [[name: "${GERRIT_BRANCH}"]],
       extensions: scmExtensions,
-      userRemoteConfigs: [[
-        credentialsId: "${config.credentialsId}",
-        name: 'gerrit',
-        url: "ssh://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git",
-        refspec: "${GERRIT_REFSPEC}"
-      ]]
+      userRemoteConfigs: [scmUserRemoteConfigs]
     ]
   )
 }