blob: 99c91b71714a67c54be7fd40133cd9250d463dd3 [file] [log] [blame]
Jakub Josef1b75ca82017-02-20 16:08:13 +01001package com.mirantis.mk
Jakub Josefc70c2a32017-03-29 16:38:30 +02002import java.util.regex.Pattern
Jakub Josefec5098f2017-06-15 18:15:32 +02003import com.cloudbees.groovy.cps.NonCPS
4import com.sonyericsson.hudson.plugins.gerrit.trigger.hudsontrigger.GerritCause
Jakub Josef1b75ca82017-02-20 16:08:13 +01005/**
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 Josefbccd7862017-05-30 14:27:15 +020016 * - withMerge, merge master before build
17 * - withLocalBranch, prevent detached mode in repo
Jakub Josef1b75ca82017-02-20 16:08:13 +010018 * - withWipeOut, wipe repository and force clone
Jakub Josefbccd7862017-05-30 14:27:15 +020019 * 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 Josefc70c2a32017-03-29 16:38:30 +020021 * @return boolean result
Jakub Josef1b75ca82017-02-20 16:08:13 +010022 *
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 Josefbccd7862017-05-30 14:27:15 +020036def gerritPatchsetCheckout(LinkedHashMap config, List extraScmExtensions = []) {
Jakub Josef1b75ca82017-02-20 16:08:13 +010037 def merge = config.get('withMerge', false)
38 def wipe = config.get('withWipeOut', false)
Jakub Josefbccd7862017-05-30 14:27:15 +020039 def localBranch = config.get('withLocalBranch', false)
Jakub Josef1b75ca82017-02-20 16:08:13 +010040 def credentials = config.get('credentialsId','')
Jakub Josef71c46a62017-03-29 14:55:33 +020041 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"] : "")
chnyda96a1e8a2017-03-28 16:02:13 +020048 def path = config.get('path', "")
chnyda7d25fc92017-03-29 10:51:59 +020049 def depth = config.get('depth', 0)
50 def timeout = config.get('timeout', 20)
Jakub Josef1b75ca82017-02-20 16:08:13 +010051
Dmitry Pyzhov169f8122017-12-06 14:44:41 +030052 def invalidParams = _getInvalidGerritParams(config)
53 if (invalidParams.isEmpty()) {
Jakub Josefc70c2a32017-03-29 16:38:30 +020054 // 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 Josefc70c2a32017-03-29 16:38:30 +020063 ]
Jakub Josef30fc9212017-04-04 11:47:19 +020064 if(gerritRefSpec && gerritRefSpec != ""){
65 scmUserRemoteConfigs.put('refspec', gerritRefSpec)
66 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010067
Jakub Josefc70c2a32017-03-29 16:38:30 +020068 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 Josef1b75ca82017-02-20 16:08:13 +010076
Jakub Josefc70c2a32017-03-29 16:38:30 +020077 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
78 if (merge) {
Jakub Josefbccd7862017-05-30 14:27:15 +020079 scmExtensions.add([$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: 'gerrit', mergeStrategy: 'default', mergeTarget: gerritBranch]])
Jakub Josefc70c2a32017-03-29 16:38:30 +020080 }
81 // we need wipe workspace before checkout
82 if (wipe) {
83 scmExtensions.add([$class: 'WipeWorkspace'])
84 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010085
Jakub Josefbccd7862017-05-30 14:27:15 +020086 if(localBranch){
87 scmExtensions.add([$class: 'LocalBranch', localBranch: gerritBranch])
88 }
89
90 if(!extraScmExtensions.isEmpty()){
91 scmExtensions.addAll(extraScmExtensions)
92 }
Jakub Josefc70c2a32017-03-29 16:38:30 +020093 if (path == "") {
chnyda96a1e8a2017-03-28 16:02:13 +020094 checkout(
95 scm: [
96 $class: 'GitSCM',
97 branches: [[name: "${gerritBranch}"]],
98 extensions: scmExtensions,
99 userRemoteConfigs: [scmUserRemoteConfigs]
100 ]
101 )
Jakub Josefc70c2a32017-03-29 16:38:30 +0200102 } else {
103 dir(path) {
104 checkout(
105 scm: [
106 $class: 'GitSCM',
107 branches: [[name: "${gerritBranch}"]],
108 extensions: scmExtensions,
109 userRemoteConfigs: [scmUserRemoteConfigs]
110 ]
111 )
112 }
chnyda96a1e8a2017-03-28 16:02:13 +0200113 }
Jakub Josefc70c2a32017-03-29 16:38:30 +0200114 return true
Jakub Josef73d62142017-03-29 17:07:18 +0200115 }else{
Dmitry Pyzhov169f8122017-12-06 14:44:41 +0300116 throw new Exception("Cannot perform gerrit checkout, missed config options: " + invalidParams)
chnyda96a1e8a2017-03-28 16:02:13 +0200117 }
Jakub Josef30fc9212017-04-04 11:47:19 +0200118 return false
Jakub Josefc70c2a32017-03-29 16:38:30 +0200119}
120/**
121 * Execute git clone and checkout stage from gerrit review
122 *
Jakub Josefad34dbf2017-03-29 17:52:31 +0200123 * @param gerritUrl gerrit url with scheme
Jakub Josefc70c2a32017-03-29 16:38:30 +0200124 * "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git
Jakub Josefad34dbf2017-03-29 17:52:31 +0200125 * @param gerritRef gerrit ref spec
126 * @param gerritBranch gerrit branch
127 * @param credentialsId jenkins credentials id
Dmitry Pyzhove832b0a2017-12-06 14:43:25 +0300128 * @param path checkout path, optional, default is empty string which means workspace root
Jakub Josefc70c2a32017-03-29 16:38:30 +0200129 * @return boolean result
130 */
Dmitry Pyzhove832b0a2017-12-06 14:43:25 +0300131def gerritPatchsetCheckout(gerritUrl, gerritRef, gerritBranch, credentialsId, path="") {
Jakub Josefad34dbf2017-03-29 17:52:31 +0200132 def gerritParams = _getGerritParamsFromUrl(gerritUrl)
Jakub Josefd383f392017-03-29 16:52:04 +0200133 if(gerritParams.size() == 5){
Dmitry Pyzhove832b0a2017-12-06 14:43:25 +0300134 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 Josefc70c2a32017-03-29 16:38:30 +0200161 }
162 return false
163}
164
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200165/**
166 * Return gerrit change object from gerrit API
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200167 * @param gerritName gerrit user name (usually GERRIT_NAME property)
Jakub Josefb735dd42017-04-10 15:31:19 +0200168 * @param gerritHost gerrit host (usually GERRIT_HOST property)
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200169 * @param gerritChangeNumber gerrit change number (usually GERRIT_CHANGE_NUMBER property)
170 * @param credentialsId jenkins credentials id for gerrit
Jakub Josef5aa33de2017-06-16 12:23:45 +0200171 * @param includeCurrentPatchset do you want to include current (last) patchset
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200172 * @return gerrit change object
173 */
Jakub Josef5aa33de2017-06-16 12:23:45 +0200174def getGerritChange(gerritName, gerritHost, gerritChangeNumber, credentialsId, includeCurrentPatchset = false){
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200175 def common = new com.mirantis.mk.Common()
176 def ssh = new com.mirantis.mk.Ssh()
177 ssh.prepareSshAgentKey(credentialsId)
178 ssh.ensureKnownHosts(gerritHost)
Jakub Josef5aa33de2017-06-16 12:23:45 +0200179 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 Josef50c9c3a2017-04-10 14:32:35 +0200184}
185
Jakub Josef4edd7432017-05-10 17:58:56 +0200186/**
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
193def getGerritTriggeredBuilds(allBuilds, gerritChange, excludePatchset = null){
194 return allBuilds.findAll{job ->
195 def cause = job.causes[0]
Jakub Josefec5098f2017-06-15 18:15:32 +0200196 if(cause instanceof GerritCause &&
Jakub Josef4edd7432017-05-10 17:58:56 +0200197 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 Josef62899fd2017-06-15 18:53:46 +0200207/**
Jakub Josef5aa33de2017-06-16 12:23:45 +0200208 * Returns boolean result of test given gerrit patchset for given approval type and value
209 * @param patchset gerrit patchset
Jakub Josef62899fd2017-06-15 18:53:46 +0200210 * @param approvalType type of tested approval (optional, default Verified)
Jakub Josef798bfc52017-06-16 12:44:23 +0200211 * @param approvalValue value of tested approval (optional, default empty string which means any value)
Jakub Josef62899fd2017-06-15 18:53:46 +0200212 * @return boolean result
Jakub Josef5aa33de2017-06-16 12:23:45 +0200213 * @example patchsetHasApproval(gerrit.getGerritChange(*,*,*,*, true).currentPatchSet)
Jakub Josef62899fd2017-06-15 18:53:46 +0200214 */
Jakub Josef5aa33de2017-06-16 12:23:45 +0200215@NonCPS
Jakub Josef798bfc52017-06-16 12:44:23 +0200216def patchsetHasApproval(patchSet, approvalType="Verified", approvalValue = ""){
Jakub Josef5aa33de2017-06-16 12:23:45 +0200217 if(patchSet && patchSet.approvals){
218 for(int i=0; i < patchSet.approvals.size();i++){
219 def approval = patchSet.approvals.get(i)
Jakub Josef798bfc52017-06-16 12:44:23 +0200220 if(approval.type.equals(approvalType)){
221 if(approvalValue.equals("") || approval.value.equals(approvalValue)){
222 return true
Jakub Josef996ada82017-06-16 12:59:43 +0200223 }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 Josef798bfc52017-06-16 12:44:23 +0200227 }
Jakub Josef5aa33de2017-06-16 12:23:45 +0200228 }
229 }
Jakub Josef62899fd2017-06-15 18:53:46 +0200230 }
231 return false
232}
Jakub Josef4edd7432017-05-10 17:58:56 +0200233
Jakub Josefd383f392017-03-29 16:52:04 +0200234@NonCPS
235def _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 Pyzhov169f8122017-12-06 14:44:41 +0300244def _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 Josef1b75ca82017-02-20 16:08:13 +0100249}