blob: 7cbd1fd1ea98cb80543adb78f91cdadfefe7586b [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',
57 refspec: gerritRefSpec
58 ]
Jakub Josef1b75ca82017-02-20 16:08:13 +010059
Jakub Josefc70c2a32017-03-29 16:38:30 +020060 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 Josef1b75ca82017-02-20 16:08:13 +010068
Jakub Josefc70c2a32017-03-29 16:38:30 +020069 // 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 Josef1b75ca82017-02-20 16:08:13 +010077
Jakub Josefc70c2a32017-03-29 16:38:30 +020078 if (path == "") {
chnyda96a1e8a2017-03-28 16:02:13 +020079 checkout(
80 scm: [
81 $class: 'GitSCM',
82 branches: [[name: "${gerritBranch}"]],
83 extensions: scmExtensions,
84 userRemoteConfigs: [scmUserRemoteConfigs]
85 ]
86 )
Jakub Josefc70c2a32017-03-29 16:38:30 +020087 } else {
88 dir(path) {
89 checkout(
90 scm: [
91 $class: 'GitSCM',
92 branches: [[name: "${gerritBranch}"]],
93 extensions: scmExtensions,
94 userRemoteConfigs: [scmUserRemoteConfigs]
95 ]
96 )
97 }
chnyda96a1e8a2017-03-28 16:02:13 +020098 }
Jakub Josefc70c2a32017-03-29 16:38:30 +020099 return true
chnyda96a1e8a2017-03-28 16:02:13 +0200100 }
Jakub Josefc70c2a32017-03-29 16:38:30 +0200101 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 */
111def 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
129def _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 Josef1b75ca82017-02-20 16:08:13 +0100135}