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 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 52 | if(_validGerritConfig(config)){ |
| 53 | // default parameters |
| 54 | def scmExtensions = [ |
| 55 | [$class: 'CleanCheckout'], |
| 56 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']], |
| 57 | [$class: 'CheckoutOption', timeout: timeout], |
| 58 | [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout] |
| 59 | ] |
| 60 | def scmUserRemoteConfigs = [ |
| 61 | name: 'gerrit', |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 62 | ] |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 63 | if(gerritRefSpec && gerritRefSpec != ""){ |
| 64 | scmUserRemoteConfigs.put('refspec', gerritRefSpec) |
| 65 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 66 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 67 | if (credentials == '') { |
| 68 | // then try to checkout in anonymous mode |
| 69 | scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}") |
| 70 | } else { |
| 71 | // else use ssh checkout |
| 72 | scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git") |
| 73 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 74 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 75 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 76 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 77 | if (merge) { |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 78 | 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] | 79 | } |
| 80 | // we need wipe workspace before checkout |
| 81 | if (wipe) { |
| 82 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 83 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 84 | |
Jakub Josef | bccd786 | 2017-05-30 14:27:15 +0200 | [diff] [blame] | 85 | if(localBranch){ |
| 86 | scmExtensions.add([$class: 'LocalBranch', localBranch: gerritBranch]) |
| 87 | } |
| 88 | |
| 89 | if(!extraScmExtensions.isEmpty()){ |
| 90 | scmExtensions.addAll(extraScmExtensions) |
| 91 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 92 | if (path == "") { |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 93 | checkout( |
| 94 | scm: [ |
| 95 | $class: 'GitSCM', |
| 96 | branches: [[name: "${gerritBranch}"]], |
| 97 | extensions: scmExtensions, |
| 98 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 99 | ] |
| 100 | ) |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 101 | } else { |
| 102 | dir(path) { |
| 103 | checkout( |
| 104 | scm: [ |
| 105 | $class: 'GitSCM', |
| 106 | branches: [[name: "${gerritBranch}"]], |
| 107 | extensions: scmExtensions, |
| 108 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 109 | ] |
| 110 | ) |
| 111 | } |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 112 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 113 | return true |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 114 | }else{ |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 115 | throw new Exception("Cannot perform gerrit checkout, given config file is not valid") |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 116 | } |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 117 | return false |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 118 | } |
| 119 | /** |
| 120 | * Execute git clone and checkout stage from gerrit review |
| 121 | * |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 122 | * @param gerritUrl gerrit url with scheme |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 123 | * "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 124 | * @param gerritRef gerrit ref spec |
| 125 | * @param gerritBranch gerrit branch |
| 126 | * @param credentialsId jenkins credentials id |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 127 | * @return boolean result |
| 128 | */ |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 129 | def gerritPatchsetCheckout(gerritUrl, gerritRef, gerritBranch, credentialsId) { |
| 130 | def gerritParams = _getGerritParamsFromUrl(gerritUrl) |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 131 | if(gerritParams.size() == 5){ |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 132 | gerritPatchsetCheckout([ |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 133 | credentialsId : credentialsId, |
| 134 | gerritBranch: gerritBranch, |
| 135 | gerritRefSpec: gerritRef, |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 136 | gerritScheme: gerritParams[0], |
| 137 | gerritName: gerritParams[1], |
| 138 | gerritHost: gerritParams[2], |
| 139 | gerritPort: gerritParams[3], |
| 140 | gerritProject: gerritParams[4] |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 141 | ]) |
| 142 | return true |
| 143 | } |
| 144 | return false |
| 145 | } |
| 146 | |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 147 | /** |
| 148 | * Return gerrit change object from gerrit API |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 149 | * @param gerritName gerrit user name (usually GERRIT_NAME property) |
Jakub Josef | b735dd4 | 2017-04-10 15:31:19 +0200 | [diff] [blame] | 150 | * @param gerritHost gerrit host (usually GERRIT_HOST property) |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 151 | * @param gerritChangeNumber gerrit change number (usually GERRIT_CHANGE_NUMBER property) |
| 152 | * @param credentialsId jenkins credentials id for gerrit |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame^] | 153 | * @param includeCurrentPatchset do you want to include current (last) patchset |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 154 | * @return gerrit change object |
| 155 | */ |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame^] | 156 | def getGerritChange(gerritName, gerritHost, gerritChangeNumber, credentialsId, includeCurrentPatchset = false){ |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 157 | def common = new com.mirantis.mk.Common() |
| 158 | def ssh = new com.mirantis.mk.Ssh() |
| 159 | ssh.prepareSshAgentKey(credentialsId) |
| 160 | ssh.ensureKnownHosts(gerritHost) |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame^] | 161 | def curPatchset = ""; |
| 162 | if(includeCurrentPatchset){ |
| 163 | curPatchset = "--current-patch-set" |
| 164 | } |
| 165 | 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] | 166 | } |
| 167 | |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 168 | /** |
| 169 | * Returns list of Gerrit trigger requested builds |
| 170 | * @param allBuilds list of all builds of some job |
| 171 | * @param gerritChange gerrit change number |
| 172 | * @param excludePatchset gerrit patchset number which will be excluded from builds, optional null |
| 173 | */ |
| 174 | @NonCPS |
| 175 | def getGerritTriggeredBuilds(allBuilds, gerritChange, excludePatchset = null){ |
| 176 | return allBuilds.findAll{job -> |
| 177 | def cause = job.causes[0] |
Jakub Josef | ec5098f | 2017-06-15 18:15:32 +0200 | [diff] [blame] | 178 | if(cause instanceof GerritCause && |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 179 | cause.getEvent() instanceof com.sonymobile.tools.gerrit.gerritevents.dto.events.PatchsetCreated){ |
| 180 | if(excludePatchset == null || excludePatchset == 0){ |
| 181 | return cause.event.change.number.equals(String.valueOf(gerritChange)) |
| 182 | }else{ |
| 183 | return cause.event.change.number.equals(String.valueOf(gerritChange)) && !cause.event.patchSet.number.equals(String.valueOf(excludePatchset)) |
| 184 | } |
| 185 | } |
| 186 | return false |
| 187 | } |
| 188 | } |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 189 | /** |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame^] | 190 | * Returns boolean result of test given gerrit patchset for given approval type and value |
| 191 | * @param patchset gerrit patchset |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 192 | * @param approvalType type of tested approval (optional, default Verified) |
| 193 | * @param approvalValue value of tested approval (optional, default 1) |
| 194 | * @return boolean result |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame^] | 195 | * @example patchsetHasApproval(gerrit.getGerritChange(*,*,*,*, true).currentPatchSet) |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 196 | */ |
Jakub Josef | 5aa33de | 2017-06-16 12:23:45 +0200 | [diff] [blame^] | 197 | @NonCPS |
| 198 | def patchsetHasApproval(patchSet, approvalType="Verified", approvalValue="1"){ |
| 199 | if(patchSet && patchSet.approvals){ |
| 200 | for(int i=0; i < patchSet.approvals.size();i++){ |
| 201 | def approval = patchSet.approvals.get(i) |
| 202 | if(approval.type.equals(approvalType) && approval.value.equals(approvalValue)){ |
| 203 | return true |
| 204 | } |
| 205 | } |
Jakub Josef | 62899fd | 2017-06-15 18:53:46 +0200 | [diff] [blame] | 206 | } |
| 207 | return false |
| 208 | } |
Jakub Josef | 4edd743 | 2017-05-10 17:58:56 +0200 | [diff] [blame] | 209 | |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 210 | @NonCPS |
| 211 | def _getGerritParamsFromUrl(gitUrl){ |
| 212 | def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)") |
| 213 | def gitUrlMatcher = gitUrlPattern.matcher(gitUrl) |
| 214 | if(gitUrlMatcher.find() && gitUrlMatcher.groupCount() == 5){ |
| 215 | return [gitUrlMatcher.group(1),gitUrlMatcher.group(2),gitUrlMatcher.group(3),gitUrlMatcher.group(4),gitUrlMatcher.group(5)] |
| 216 | } |
| 217 | return [] |
| 218 | } |
| 219 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 220 | def _validGerritConfig(LinkedHashMap config){ |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 221 | return config.get("gerritScheme","") != null && config.get("gerritScheme","") != "" && |
| 222 | config.get("gerritName","") != null && config.get("gerritName","") != "" && |
| 223 | config.get("gerritHost","") != null && config.get("gerritHost","") != "" && |
| 224 | config.get("gerritPort","") != null && config.get("gerritPort","") != "" && |
| 225 | config.get("gerritProject","") != null && config.get("gerritProject","") != "" && |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 226 | config.get("gerritBranch","") != null && config.get("gerritBranch","") != "" |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 227 | } |