| package com.mirantis.mk |
| |
| import java.util.regex.Pattern |
| /** |
| * Gerrit functions |
| * |
| */ |
| |
| /** |
| * Execute git clone and checkout stage from gerrit review |
| * |
| * @param config LinkedHashMap |
| * config includes next parameters: |
| * - 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 |
| * def gitFunc = new com.mirantis.mcp.Git() |
| * gitFunc.gerritPatchsetCheckout([ |
| * withMerge : true |
| * ]) |
| * |
| * def gitFunc = new com.mirantis.mcp.Git() |
| * gitFunc.gerritPatchsetCheckout([ |
| * 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','') |
| def gerritScheme = config.get('gerritScheme', env["GERRIT_SCHEME"] ? env["GERRIT_SCHEME"] : "") |
| def gerritRefSpec = config.get('gerritRefSpec', env["GERRIT_REFSPEC"] ? env["GERRIT_REFSPEC"] : "") |
| def gerritName = config.get('gerritName', env["GERRIT_NAME"] ? env["GERRIT_NAME"] : "") |
| def gerritHost = config.get('gerritHost', env["GERRIT_HOST"] ? env["GERRIT_HOST"] : "") |
| def gerritPort = config.get('gerritPort', env["GERRIT_PORT"] ? env["GERRIT_PORT"] : "") |
| def gerritProject = config.get('gerritProject', env["GERRIT_PROJECT"] ? env["GERRIT_PROJECT"] : "") |
| def gerritBranch = config.get('gerritBranch', env["GERRIT_BRANCH"] ? env["GERRIT_BRANCH"] : "") |
| def path = config.get('path', "") |
| def depth = config.get('depth', 0) |
| def timeout = config.get('timeout', 20) |
| |
| 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 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) { |
| 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 gerritParams = _getGerritParamsFromUrl(gitUrl) |
| if(gerritParams.size() == 5){ |
| gerritPatchsetCheckout([ |
| credentialsId : CREDENTIALS_ID, |
| gerritRefSpec: gitRef, |
| gerritScheme: gerritParams[0], |
| gerritName: gerritParams[1], |
| gerritHost: gerritParams[2], |
| gerritPort: gerritParams[3], |
| gerritProject: gerritParams[4] |
| ]) |
| return true |
| } |
| return false |
| } |
| |
| @NonCPS |
| def _getGerritParamsFromUrl(gitUrl){ |
| def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)") |
| def gitUrlMatcher = gitUrlPattern.matcher(gitUrl) |
| if(gitUrlMatcher.find() && gitUrlMatcher.groupCount() == 5){ |
| return [gitUrlMatcher.group(1),gitUrlMatcher.group(2),gitUrlMatcher.group(3),gitUrlMatcher.group(4),gitUrlMatcher.group(5)] |
| } |
| return [] |
| } |
| |
| def _validGerritConfig(LinkedHashMap config){ |
| return config.get("gerritScheme","") != "" && |
| config.get("gerritName","") != "" && |
| config.get("gerritHost","") != "" && |
| config.get("gerritPort","") != "" && |
| config.get("gerritProject","") != "" |
| } |