Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 3 | import java.util.regex.Pattern |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 4 | /** |
| 5 | * Gerrit functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Execute git clone and checkout stage from gerrit review |
| 11 | * |
| 12 | * @param config LinkedHashMap |
| 13 | * config includes next parameters: |
| 14 | * - credentialsId, id of user which should make checkout |
| 15 | * - withMerge, prevent detached mode in repo |
| 16 | * - withWipeOut, wipe repository and force clone |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 17 | * @return boolean result |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 18 | * |
| 19 | * Usage example: |
| 20 | * //anonymous gerrit checkout |
| 21 | * def gitFunc = new com.mirantis.mcp.Git() |
| 22 | * gitFunc.gerritPatchsetCheckout([ |
| 23 | * withMerge : true |
| 24 | * ]) |
| 25 | * |
| 26 | * def gitFunc = new com.mirantis.mcp.Git() |
| 27 | * gitFunc.gerritPatchsetCheckout([ |
| 28 | * credentialsId : 'mcp-ci-gerrit', |
| 29 | * withMerge : true |
| 30 | * ]) |
| 31 | */ |
| 32 | def gerritPatchsetCheckout(LinkedHashMap config) { |
| 33 | def merge = config.get('withMerge', false) |
| 34 | def wipe = config.get('withWipeOut', false) |
| 35 | def credentials = config.get('credentialsId','') |
Jakub Josef | 71c46a6 | 2017-03-29 14:55:33 +0200 | [diff] [blame] | 36 | def gerritScheme = config.get('gerritScheme', env["GERRIT_SCHEME"] ? env["GERRIT_SCHEME"] : "") |
| 37 | def gerritRefSpec = config.get('gerritRefSpec', env["GERRIT_REFSPEC"] ? env["GERRIT_REFSPEC"] : "") |
| 38 | def gerritName = config.get('gerritName', env["GERRIT_NAME"] ? env["GERRIT_NAME"] : "") |
| 39 | def gerritHost = config.get('gerritHost', env["GERRIT_HOST"] ? env["GERRIT_HOST"] : "") |
| 40 | def gerritPort = config.get('gerritPort', env["GERRIT_PORT"] ? env["GERRIT_PORT"] : "") |
| 41 | def gerritProject = config.get('gerritProject', env["GERRIT_PROJECT"] ? env["GERRIT_PROJECT"] : "") |
| 42 | def gerritBranch = config.get('gerritBranch', env["GERRIT_BRANCH"] ? env["GERRIT_BRANCH"] : "") |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 43 | def path = config.get('path', "") |
chnyda | 7d25fc9 | 2017-03-29 10:51:59 +0200 | [diff] [blame] | 44 | def depth = config.get('depth', 0) |
| 45 | def timeout = config.get('timeout', 20) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 46 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 47 | if(_validGerritConfig(config)){ |
| 48 | // default parameters |
| 49 | def scmExtensions = [ |
| 50 | [$class: 'CleanCheckout'], |
| 51 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']], |
| 52 | [$class: 'CheckoutOption', timeout: timeout], |
| 53 | [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout] |
| 54 | ] |
| 55 | def scmUserRemoteConfigs = [ |
| 56 | name: 'gerrit', |
| 57 | refspec: gerritRefSpec |
| 58 | ] |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 59 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 60 | if (credentials == '') { |
| 61 | // then try to checkout in anonymous mode |
| 62 | scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}") |
| 63 | } else { |
| 64 | // else use ssh checkout |
| 65 | scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git") |
| 66 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 67 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 68 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 69 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 70 | if (merge) { |
| 71 | scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"]) |
| 72 | } |
| 73 | // we need wipe workspace before checkout |
| 74 | if (wipe) { |
| 75 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 76 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 77 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 78 | if (path == "") { |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 79 | checkout( |
| 80 | scm: [ |
| 81 | $class: 'GitSCM', |
| 82 | branches: [[name: "${gerritBranch}"]], |
| 83 | extensions: scmExtensions, |
| 84 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 85 | ] |
| 86 | ) |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 87 | } else { |
| 88 | dir(path) { |
| 89 | checkout( |
| 90 | scm: [ |
| 91 | $class: 'GitSCM', |
| 92 | branches: [[name: "${gerritBranch}"]], |
| 93 | extensions: scmExtensions, |
| 94 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 95 | ] |
| 96 | ) |
| 97 | } |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 98 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 99 | return true |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 100 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame^] | 101 | return false |
| 102 | } |
| 103 | /** |
| 104 | * Execute git clone and checkout stage from gerrit review |
| 105 | * |
| 106 | * @param gitUrl git url with scheme |
| 107 | * "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git |
| 108 | * @param gitRef git ref spec |
| 109 | * @return boolean result |
| 110 | */ |
| 111 | def gerritPatchsetCheckout(gitUrl, gitRef) { |
| 112 | def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)") |
| 113 | def gitUrlMatcher = gitUrlPattern.matcher(gitUrl) |
| 114 | if(gitUrlMatcher.find()){ |
| 115 | gerritPatchsetCheckout([ |
| 116 | credentialsId : CREDENTIALS_ID, |
| 117 | gerritRefSpec: gitRef, |
| 118 | gerritScheme: gitUrlMatcher.group(1), |
| 119 | gerritName: gitUrlMatcher.group(2), |
| 120 | gerritHost: gitUrlMatcher.group(3), |
| 121 | gerritPort: gitUrlMatcher.group(4), |
| 122 | gerritProject: gitUrlMatcher.group(5) |
| 123 | ]) |
| 124 | return true |
| 125 | } |
| 126 | return false |
| 127 | } |
| 128 | |
| 129 | def _validGerritConfig(LinkedHashMap config){ |
| 130 | return config.get("gerritScheme","") != "" && |
| 131 | config.get("gerritName","") != "" && |
| 132 | config.get("gerritHost","") != "" && |
| 133 | config.get("gerritPort","") != "" && |
| 134 | config.get("gerritProject","") != "" |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 135 | } |