Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 2 | import java.util.regex.Pattern |
Jakub Josef | ec5098f | 2017-06-15 18:15:32 +0200 | [diff] [blame] | 3 | import com.cloudbees.groovy.cps.NonCPS |
| 4 | import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 5 | /** |
| 6 | * Gerrit functions |
| 7 | * |
| 8 | */ |
| 9 | |
| 10 | /** |
| 11 | * Execute git clone and checkout stage from gerrit review |
| 12 | * |
| 13 | * @param config LinkedHashMap |
| 14 | * config includes next parameters: |
| 15 | * - credentialsId, id of user which should make checkout |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 16 | * - withMerge, merge master before build |
| 17 | * - withLocalBranch, prevent detached mode in repo |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 18 | * - withWipeOut, wipe repository and force clone |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 19 | * Gerrit properties like GERRIT_SCHEMA can be passed in config as gerritSchema or will be obtained from env |
| 20 | * @param extraScmExtensions list of extra scm extensions which will be used for checkout (optional) |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 21 | * @return boolean result |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 22 | * |
| 23 | * Usage example: |
| 24 | * //anonymous gerrit checkout |
| 25 | * def gitFunc = new com.mirantis.mcp.Git() |
| 26 | * gitFunc.gerritPatchsetCheckout([ |
| 27 | * withMerge : true |
| 28 | * ]) |
| 29 | * |
| 30 | * def gitFunc = new com.mirantis.mcp.Git() |
| 31 | * gitFunc.gerritPatchsetCheckout([ |
| 32 | * credentialsId : 'mcp-ci-gerrit', |
| 33 | * withMerge : true |
| 34 | * ]) |
| 35 | */ |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 36 | def gerritPatchsetCheckout(LinkedHashMap config, List extraScmExtensions = []) { |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 37 | def merge = config.get('withMerge', false) |
| 38 | def wipe = config.get('withWipeOut', false) |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 39 | def localBranch = config.get('withLocalBranch', false) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 40 | def credentials = config.get('credentialsId','') |
Jakub Josef | 71c46a6 | 2017-03-29 14:55:33 +0200 | [diff] [blame] | 41 | def gerritScheme = config.get('gerritScheme', env["GERRIT_SCHEME"] ? env["GERRIT_SCHEME"] : "") |
| 42 | def gerritRefSpec = config.get('gerritRefSpec', env["GERRIT_REFSPEC"] ? env["GERRIT_REFSPEC"] : "") |
| 43 | def gerritName = config.get('gerritName', env["GERRIT_NAME"] ? env["GERRIT_NAME"] : "") |
| 44 | def gerritHost = config.get('gerritHost', env["GERRIT_HOST"] ? env["GERRIT_HOST"] : "") |
| 45 | def gerritPort = config.get('gerritPort', env["GERRIT_PORT"] ? env["GERRIT_PORT"] : "") |
| 46 | def gerritProject = config.get('gerritProject', env["GERRIT_PROJECT"] ? env["GERRIT_PROJECT"] : "") |
| 47 | def gerritBranch = config.get('gerritBranch', env["GERRIT_BRANCH"] ? env["GERRIT_BRANCH"] : "") |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 48 | def path = config.get('path', "") |
chnyda | 7d25fc9 | 2017-03-29 10:51:59 +0200 | [diff] [blame] | 49 | def depth = config.get('depth', 0) |
| 50 | def timeout = config.get('timeout', 20) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 51 | |
Dmitry Pyzhov | 169f812 | 2017-12-06 14:44:41 +0300 | [diff] [blame^] | 52 | def invalidParams = _getInvalidGerritParams(config) |
| 53 | if (invalidParams.isEmpty()) { |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 54 | // default parameters |
| 55 | def scmExtensions = [ |
| 56 | [$class: 'CleanCheckout'], |
| 57 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']], |
| 58 | [$class: 'CheckoutOption', timeout: timeout], |
| 59 | [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout] |
| 60 | ] |
| 61 | def scmUserRemoteConfigs = [ |
| 62 | name: 'gerrit', |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 63 | ] |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 64 | if(gerritRefSpec && gerritRefSpec != ""){ |
| 65 | scmUserRemoteConfigs.put('refspec', gerritRefSpec) |
| 66 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 67 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 68 | if (credentials == '') { |
| 69 | // then try to checkout in anonymous mode |
| 70 | scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}") |
| 71 | } else { |
| 72 | // else use ssh checkout |
| 73 | scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git") |
| 74 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 75 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 76 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 77 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 78 | if (merge) { |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 79 | scmExtensions.add([$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: 'gerrit', mergeStrategy: 'default', mergeTarget: gerritBranch]]) |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 80 | } |
| 81 | // we need wipe workspace before checkout |
| 82 | if (wipe) { |
| 83 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 84 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 85 | |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 86 | if(localBranch){ |
| 87 | scmExtensions.add([$class: 'LocalBranch', localBranch: gerritBranch]) |
| 88 | } |
| 89 | |
| 90 | if(!extraScmExtensions.isEmpty()){ |
| 91 | scmExtensions.addAll(extraScmExtensions) |
| 92 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 93 | if (path == "") { |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 94 | checkout( |
| 95 | scm: [ |
| 96 | $class: 'GitSCM', |
| 97 | branches: [[name: "${gerritBranch}"]], |
| 98 | extensions: scmExtensions, |
| 99 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 100 | ] |
| 101 | ) |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 102 | } else { |
| 103 | dir(path) { |
| 104 | checkout( |
| 105 | scm: [ |
| 106 | $class: 'GitSCM', |
| 107 | branches: [[name: "${gerritBranch}"]], |
| 108 | extensions: scmExtensions, |
| 109 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 110 | ] |
| 111 | ) |
| 112 | } |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 113 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 114 | return true |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 115 | }else{ |
Dmitry Pyzhov | 169f812 | 2017-12-06 14:44:41 +0300 | [diff] [blame^] | 116 | throw new Exception("Cannot perform gerrit checkout, missed config options: " + invalidParams) |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 117 | } |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 118 | return false |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 119 | } |
| 120 | /** |
| 121 | * Execute git clone and checkout stage from gerrit review |
| 122 | * |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 123 | * @param gerritUrl gerrit url with scheme |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 124 | * "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 125 | * @param gerritRef gerrit ref spec |
| 126 | * @param gerritBranch gerrit branch |
| 127 | * @param credentialsId jenkins credentials id |
Dmitry Pyzhov | e832b0a | 2017-12-06 14:43:25 +0300 | [diff] [blame] | 128 | * @param path checkout path, optional, default is empty string which means workspace root |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 129 | * @return boolean result |
| 130 | */ |
Dmitry Pyzhov | e832b0a | 2017-12-06 14:43:25 +0300 | [diff] [blame] | 131 | def gerritPatchsetCheckout(gerritUrl, gerritRef, gerritBranch, credentialsId, path="") { |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 132 | def gerritParams = _getGerritParamsFromUrl(gerritUrl) |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 133 | if(gerritParams.size() == 5){ |
Dmitry Pyzhov | e832b0a | 2017-12-06 14:43:25 +0300 | [diff] [blame] | 134 | if (path==""){ |
| 135 | gerritPatchsetCheckout([ |
| 136 | credentialsId : credentialsId, |
| 137 | gerritBranch: gerritBranch, |
| 138 | gerritRefSpec: gerritRef, |
| 139 | gerritScheme: gerritParams[0], |
| 140 | gerritName: gerritParams[1], |
| 141 | gerritHost: gerritParams[2], |
| 142 | gerritPort: gerritParams[3], |
| 143 | gerritProject: gerritParams[4] |
| 144 | ]) |
| 145 | return true |
| 146 | } else { |
| 147 | dir(path) { |
| 148 | gerritPatchsetCheckout([ |
| 149 | credentialsId : credentialsId, |
| 150 | gerritBranch: gerritBranch, |
| 151 | gerritRefSpec: gerritRef, |
| 152 | gerritScheme: gerritParams[0], |
| 153 | gerritName: gerritParams[1], |
| 154 | gerritHost: gerritParams[2], |
| 155 | gerritPort: gerritParams[3], |
| 156 | gerritProject: gerritParams[4] |
| 157 | ]) |
| 158 | return true |
| 159 | } |
| 160 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 161 | } |
| 162 | return false |
| 163 | } |
| 164 | |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 165 | /** |
| 166 | * Return gerrit change object from gerrit API |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 167 | * @param gerritName gerrit user name (usually GERRIT_NAME property) |
Jakub Josef | b735dd4 | 2017-04-10 15:31:19 +0200 | [diff] [blame] | 168 | * @param gerritHost gerrit host (usually GERRIT_HOST property) |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 169 | * @param gerritChangeNumber gerrit change number (usually GERRIT_CHANGE_NUMBER property) |
| 170 | * @param credentialsId jenkins credentials id for gerrit |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 171 | * @param includeCurrentPatchset do you want to include current (last) patchset |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 172 | * @return gerrit change object |
| 173 | */ |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 174 | def getGerritChange(gerritName, gerritHost, gerritChangeNumber, credentialsId, includeCurrentPatchset = false){ |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 175 | def common = new com.mirantis.mk.Common() |
| 176 | def ssh = new com.mirantis.mk.Ssh() |
| 177 | ssh.prepareSshAgentKey(credentialsId) |
| 178 | ssh.ensureKnownHosts(gerritHost) |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 179 | def curPatchset = ""; |
| 180 | if(includeCurrentPatchset){ |
| 181 | curPatchset = "--current-patch-set" |
| 182 | } |
| 183 | return common.parseJSON(ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query ${curPatchset} --format=JSON change:%s", gerritName, gerritHost, gerritChangeNumber))) |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 184 | } |
| 185 | |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 186 | /** |
| 187 | * Returns list of Gerrit trigger requested builds |
| 188 | * @param allBuilds list of all builds of some job |
| 189 | * @param gerritChange gerrit change number |
| 190 | * @param excludePatchset gerrit patchset number which will be excluded from builds, optional null |
| 191 | */ |
| 192 | @NonCPS |
| 193 | def getGerritTriggeredBuilds(allBuilds, gerritChange, excludePatchset = null){ |
| 194 | return allBuilds.findAll{job -> |
| 195 | def cause = job.causes[0] |
Jakub Josef | ec5098f | 2017-06-15 18:15:32 +0200 | [diff] [blame] | 196 | if(cause instanceof GerritCause && |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 197 | cause.getEvent() instanceof com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated){ |
| 198 | if(excludePatchset == null || excludePatchset == 0){ |
| 199 | return cause.event.change.number.equals(String.valueOf(gerritChange)) |
| 200 | }else{ |
| 201 | return cause.event.change.number.equals(String.valueOf(gerritChange)) && !cause.event.patchSet.number.equals(String.valueOf(excludePatchset)) |
| 202 | } |
| 203 | } |
| 204 | return false |
| 205 | } |
| 206 | } |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 207 | /** |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 208 | * Returns boolean result of test given gerrit patchset for given approval type and value |
| 209 | * @param patchset gerrit patchset |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 210 | * @param approvalType type of tested approval (optional, default Verified) |
Jakub Josef | 798bfc5 | 2017-06-16 12:44:23 +0200 | [diff] [blame] | 211 | * @param approvalValue value of tested approval (optional, default empty string which means any value) |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 212 | * @return boolean result |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 213 | * @example patchsetHasApproval(gerrit.getGerritChange(*,*,*,*, true).currentPatchSet) |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 214 | */ |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 215 | @NonCPS |
Jakub Josef | 798bfc5 | 2017-06-16 12:44:23 +0200 | [diff] [blame] | 216 | def patchsetHasApproval(patchSet, approvalType="Verified", approvalValue = ""){ |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 217 | if(patchSet && patchSet.approvals){ |
| 218 | for(int i=0; i < patchSet.approvals.size();i++){ |
| 219 | def approval = patchSet.approvals.get(i) |
Jakub Josef | 798bfc5 | 2017-06-16 12:44:23 +0200 | [diff] [blame] | 220 | if(approval.type.equals(approvalType)){ |
| 221 | if(approvalValue.equals("") || approval.value.equals(approvalValue)){ |
| 222 | return true |
Jakub Josef | 996ada8 | 2017-06-16 12:59:43 +0200 | [diff] [blame] | 223 | }else if(approvalValue.equals("+") && Integer.parseInt(approval.value) > 0) { |
| 224 | return true |
| 225 | }else if(approvalValue.equals("-") && Integer.parseInt(approval.value) < 0) { |
| 226 | return true |
Jakub Josef | 798bfc5 | 2017-06-16 12:44:23 +0200 | [diff] [blame] | 227 | } |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame] | 228 | } |
| 229 | } |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 230 | } |
| 231 | return false |
| 232 | } |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 233 | |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 234 | @NonCPS |
| 235 | def _getGerritParamsFromUrl(gitUrl){ |
| 236 | def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)") |
| 237 | def gitUrlMatcher = gitUrlPattern.matcher(gitUrl) |
| 238 | if(gitUrlMatcher.find() && gitUrlMatcher.groupCount() == 5){ |
| 239 | return [gitUrlMatcher.group(1),gitUrlMatcher.group(2),gitUrlMatcher.group(3),gitUrlMatcher.group(4),gitUrlMatcher.group(5)] |
| 240 | } |
| 241 | return [] |
| 242 | } |
| 243 | |
Dmitry Pyzhov | 169f812 | 2017-12-06 14:44:41 +0300 | [diff] [blame^] | 244 | def _getInvalidGerritParams(LinkedHashMap config){ |
| 245 | def requiredParams = ["gerritScheme", "gerritName", "gerritHost", "gerritPort", "gerritProject", "gerritBranch"] |
| 246 | def missedParams = requiredParams - config.keySet() |
| 247 | def badParams = config.subMap(requiredParams).findAll{it.value in [null, '']}.keySet() |
| 248 | return badParams + missedParams |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 249 | } |