blob: 410fd69c40dc46995f2cd0f782b586b62659adf7 [file] [log] [blame]
Jakub Josef1b75ca82017-02-20 16:08:13 +01001package com.mirantis.mk
2
Jakub Josefc70c2a32017-03-29 16:38:30 +02003import java.util.regex.Pattern
Jakub Josef1b75ca82017-02-20 16:08:13 +01004/**
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 Josefc70c2a32017-03-29 16:38:30 +020017 * @return boolean result
Jakub Josef1b75ca82017-02-20 16:08:13 +010018 *
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 */
32def gerritPatchsetCheckout(LinkedHashMap config) {
33 def merge = config.get('withMerge', false)
34 def wipe = config.get('withWipeOut', false)
35 def credentials = config.get('credentialsId','')
Jakub Josef71c46a62017-03-29 14:55:33 +020036 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"] : "")
chnyda96a1e8a2017-03-28 16:02:13 +020043 def path = config.get('path', "")
chnyda7d25fc92017-03-29 10:51:59 +020044 def depth = config.get('depth', 0)
45 def timeout = config.get('timeout', 20)
Jakub Josef1b75ca82017-02-20 16:08:13 +010046
Jakub Josefc70c2a32017-03-29 16:38:30 +020047 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',
Jakub Josefc70c2a32017-03-29 16:38:30 +020057 ]
Jakub Josef30fc9212017-04-04 11:47:19 +020058 if(gerritRefSpec && gerritRefSpec != ""){
59 scmUserRemoteConfigs.put('refspec', gerritRefSpec)
60 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010061
Jakub Josefc70c2a32017-03-29 16:38:30 +020062 if (credentials == '') {
63 // then try to checkout in anonymous mode
64 scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}")
65 } else {
66 // else use ssh checkout
67 scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git")
68 scmUserRemoteConfigs.put('credentialsId',credentials)
69 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010070
Jakub Josefc70c2a32017-03-29 16:38:30 +020071 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
72 if (merge) {
73 scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"])
74 }
75 // we need wipe workspace before checkout
76 if (wipe) {
77 scmExtensions.add([$class: 'WipeWorkspace'])
78 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010079
Jakub Josefc70c2a32017-03-29 16:38:30 +020080 if (path == "") {
chnyda96a1e8a2017-03-28 16:02:13 +020081 checkout(
82 scm: [
83 $class: 'GitSCM',
84 branches: [[name: "${gerritBranch}"]],
85 extensions: scmExtensions,
86 userRemoteConfigs: [scmUserRemoteConfigs]
87 ]
88 )
Jakub Josefc70c2a32017-03-29 16:38:30 +020089 } else {
90 dir(path) {
91 checkout(
92 scm: [
93 $class: 'GitSCM',
94 branches: [[name: "${gerritBranch}"]],
95 extensions: scmExtensions,
96 userRemoteConfigs: [scmUserRemoteConfigs]
97 ]
98 )
99 }
chnyda96a1e8a2017-03-28 16:02:13 +0200100 }
Jakub Josefc70c2a32017-03-29 16:38:30 +0200101 return true
Jakub Josef73d62142017-03-29 17:07:18 +0200102 }else{
Jakub Josef30fc9212017-04-04 11:47:19 +0200103 throw new Exception("Cannot perform gerrit checkout, given config file is not valid")
chnyda96a1e8a2017-03-28 16:02:13 +0200104 }
Jakub Josef30fc9212017-04-04 11:47:19 +0200105 return false
Jakub Josefc70c2a32017-03-29 16:38:30 +0200106}
107/**
108 * Execute git clone and checkout stage from gerrit review
109 *
Jakub Josefad34dbf2017-03-29 17:52:31 +0200110 * @param gerritUrl gerrit url with scheme
Jakub Josefc70c2a32017-03-29 16:38:30 +0200111 * "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git
Jakub Josefad34dbf2017-03-29 17:52:31 +0200112 * @param gerritRef gerrit ref spec
113 * @param gerritBranch gerrit branch
114 * @param credentialsId jenkins credentials id
Jakub Josefc70c2a32017-03-29 16:38:30 +0200115 * @return boolean result
116 */
Jakub Josefad34dbf2017-03-29 17:52:31 +0200117def gerritPatchsetCheckout(gerritUrl, gerritRef, gerritBranch, credentialsId) {
118 def gerritParams = _getGerritParamsFromUrl(gerritUrl)
Jakub Josefd383f392017-03-29 16:52:04 +0200119 if(gerritParams.size() == 5){
Jakub Josefc70c2a32017-03-29 16:38:30 +0200120 gerritPatchsetCheckout([
Jakub Josefad34dbf2017-03-29 17:52:31 +0200121 credentialsId : credentialsId,
122 gerritBranch: gerritBranch,
123 gerritRefSpec: gerritRef,
Jakub Josefd383f392017-03-29 16:52:04 +0200124 gerritScheme: gerritParams[0],
125 gerritName: gerritParams[1],
126 gerritHost: gerritParams[2],
127 gerritPort: gerritParams[3],
128 gerritProject: gerritParams[4]
Jakub Josefc70c2a32017-03-29 16:38:30 +0200129 ])
130 return true
131 }
132 return false
133}
134
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200135/**
136 * Return gerrit change object from gerrit API
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200137 * @param gerritName gerrit user name (usually GERRIT_NAME property)
Jakub Josefb735dd42017-04-10 15:31:19 +0200138 * @param gerritHost gerrit host (usually GERRIT_HOST property)
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200139 * @param gerritChangeNumber gerrit change number (usually GERRIT_CHANGE_NUMBER property)
140 * @param credentialsId jenkins credentials id for gerrit
141 * @return gerrit change object
142 */
Jakub Josefb735dd42017-04-10 15:31:19 +0200143def getGerritChange(gerritName, gerritHost, gerritChangeNumber, credentialsId){
Jakub Josef50c9c3a2017-04-10 14:32:35 +0200144 def common = new com.mirantis.mk.Common()
145 def ssh = new com.mirantis.mk.Ssh()
146 ssh.prepareSshAgentKey(credentialsId)
147 ssh.ensureKnownHosts(gerritHost)
148 return common.parseJSON(ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --format=JSON change:%s", gerritName, gerritHost, gerritChangeNumber)))
149}
150
Jakub Josefd383f392017-03-29 16:52:04 +0200151@NonCPS
152def _getGerritParamsFromUrl(gitUrl){
153 def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)")
154 def gitUrlMatcher = gitUrlPattern.matcher(gitUrl)
155 if(gitUrlMatcher.find() && gitUrlMatcher.groupCount() == 5){
156 return [gitUrlMatcher.group(1),gitUrlMatcher.group(2),gitUrlMatcher.group(3),gitUrlMatcher.group(4),gitUrlMatcher.group(5)]
157 }
158 return []
159}
160
Jakub Josefc70c2a32017-03-29 16:38:30 +0200161def _validGerritConfig(LinkedHashMap config){
Jakub Josef73d62142017-03-29 17:07:18 +0200162 return config.get("gerritScheme","") != null && config.get("gerritScheme","") != "" &&
163 config.get("gerritName","") != null && config.get("gerritName","") != "" &&
164 config.get("gerritHost","") != null && config.get("gerritHost","") != "" &&
165 config.get("gerritPort","") != null && config.get("gerritPort","") != "" &&
166 config.get("gerritProject","") != null && config.get("gerritProject","") != "" &&
Jakub Josef30fc9212017-04-04 11:47:19 +0200167 config.get("gerritBranch","") != null && config.get("gerritBranch","") != ""
Jakub Josef1b75ca82017-02-20 16:08:13 +0100168}