blob: 6de6b1d4ef11fd3b3885ccfca80b21ea22e0d841 [file] [log] [blame]
Jakub Josef1b75ca82017-02-20 16:08:13 +01001package com.mirantis.mk
2
3/**
4 * Gerrit functions
5 *
6 */
7
8/**
9 * Execute git clone and checkout stage from gerrit review
10 *
11 * @param config LinkedHashMap
12 * config includes next parameters:
13 * - credentialsId, id of user which should make checkout
14 * - withMerge, prevent detached mode in repo
15 * - withWipeOut, wipe repository and force clone
16 *
17 * Usage example:
18 * //anonymous gerrit checkout
19 * def gitFunc = new com.mirantis.mcp.Git()
20 * gitFunc.gerritPatchsetCheckout([
21 * withMerge : true
22 * ])
23 *
24 * def gitFunc = new com.mirantis.mcp.Git()
25 * gitFunc.gerritPatchsetCheckout([
26 * credentialsId : 'mcp-ci-gerrit',
27 * withMerge : true
28 * ])
29 */
30def gerritPatchsetCheckout(LinkedHashMap config) {
31 def merge = config.get('withMerge', false)
32 def wipe = config.get('withWipeOut', false)
33 def credentials = config.get('credentialsId','')
Jakub Josef71c46a62017-03-29 14:55:33 +020034 def gerritScheme = config.get('gerritScheme', env["GERRIT_SCHEME"] ? env["GERRIT_SCHEME"] : "")
35 def gerritRefSpec = config.get('gerritRefSpec', env["GERRIT_REFSPEC"] ? env["GERRIT_REFSPEC"] : "")
36 def gerritName = config.get('gerritName', env["GERRIT_NAME"] ? env["GERRIT_NAME"] : "")
37 def gerritHost = config.get('gerritHost', env["GERRIT_HOST"] ? env["GERRIT_HOST"] : "")
38 def gerritPort = config.get('gerritPort', env["GERRIT_PORT"] ? env["GERRIT_PORT"] : "")
39 def gerritProject = config.get('gerritProject', env["GERRIT_PROJECT"] ? env["GERRIT_PROJECT"] : "")
40 def gerritBranch = config.get('gerritBranch', env["GERRIT_BRANCH"] ? env["GERRIT_BRANCH"] : "")
chnyda96a1e8a2017-03-28 16:02:13 +020041 def path = config.get('path', "")
chnyda7d25fc92017-03-29 10:51:59 +020042 def depth = config.get('depth', 0)
43 def timeout = config.get('timeout', 20)
Jakub Josef1b75ca82017-02-20 16:08:13 +010044
45 // default parameters
46 def scmExtensions = [
47 [$class: 'CleanCheckout'],
chnyda7d25fc92017-03-29 10:51:59 +020048 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']],
49 [$class: 'CheckoutOption', timeout: timeout],
50 [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout]
Jakub Josef1b75ca82017-02-20 16:08:13 +010051 ]
52 def scmUserRemoteConfigs = [
53 name: 'gerrit',
Jakub Josef0fcd75e2017-03-22 18:35:55 +010054 refspec: gerritRefSpec
Jakub Josef1b75ca82017-02-20 16:08:13 +010055 ]
56
57 if (credentials == '') {
58 // then try to checkout in anonymous mode
Jakub Josef71c46a62017-03-29 14:55:33 +020059 scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}")
Jakub Josef1b75ca82017-02-20 16:08:13 +010060 } else {
61 // else use ssh checkout
Jakub Josef0fcd75e2017-03-22 18:35:55 +010062 scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git")
Jakub Josef1b75ca82017-02-20 16:08:13 +010063 scmUserRemoteConfigs.put('credentialsId',credentials)
64 }
65
66 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
67 if (merge) {
Jakub Josef0fcd75e2017-03-22 18:35:55 +010068 scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"])
Jakub Josef1b75ca82017-02-20 16:08:13 +010069 }
70 // we need wipe workspace before checkout
71 if (wipe) {
72 scmExtensions.add([$class: 'WipeWorkspace'])
73 }
74
chnyda96a1e8a2017-03-28 16:02:13 +020075 if (path == "") {
76 checkout(
77 scm: [
78 $class: 'GitSCM',
79 branches: [[name: "${gerritBranch}"]],
80 extensions: scmExtensions,
81 userRemoteConfigs: [scmUserRemoteConfigs]
82 ]
83 )
84 } else {
85 dir(path) {
86 checkout(
87 scm: [
88 $class: 'GitSCM',
89 branches: [[name: "${gerritBranch}"]],
90 extensions: scmExtensions,
91 userRemoteConfigs: [scmUserRemoteConfigs]
92 ]
93 )
94 }
95 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010096}