blob: 932cb061b1ae9c21c9d34fd3725ca7c179c2cdbe [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 Josef0fcd75e2017-03-22 18:35:55 +010034 def gerritRefSpec = config.get('gerritRefSpec', GERRIT_REFSPEC)
35 def gerritName = config.get('gerritName', GERRIT_NAME)
36 def gerritHost = config.get('gerritHost', GERRIT_HOST)
37 def gerritPort = config.get('gerritPort', GERRIT_PORT)
38 def gerritProject = config.get('gerritProject', GERRIT_PROJECT)
39 def gerritBranch = config.get('gerritBranch', GERRIT_BRANCH)
chnyda96a1e8a2017-03-28 16:02:13 +020040 def path = config.get('path', "")
chnyda7d25fc92017-03-29 10:51:59 +020041 def depth = config.get('depth', 0)
42 def timeout = config.get('timeout', 20)
Jakub Josef1b75ca82017-02-20 16:08:13 +010043
44 // default parameters
45 def scmExtensions = [
46 [$class: 'CleanCheckout'],
chnyda7d25fc92017-03-29 10:51:59 +020047 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']],
48 [$class: 'CheckoutOption', timeout: timeout],
49 [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout]
Jakub Josef1b75ca82017-02-20 16:08:13 +010050 ]
51 def scmUserRemoteConfigs = [
52 name: 'gerrit',
Jakub Josef0fcd75e2017-03-22 18:35:55 +010053 refspec: gerritRefSpec
Jakub Josef1b75ca82017-02-20 16:08:13 +010054 ]
55
56 if (credentials == '') {
57 // then try to checkout in anonymous mode
Jakub Josef0fcd75e2017-03-22 18:35:55 +010058 scmUserRemoteConfigs.put('url',"https://${gerritHost}/${gerritProject}")
Jakub Josef1b75ca82017-02-20 16:08:13 +010059 } else {
60 // else use ssh checkout
Jakub Josef0fcd75e2017-03-22 18:35:55 +010061 scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git")
Jakub Josef1b75ca82017-02-20 16:08:13 +010062 scmUserRemoteConfigs.put('credentialsId',credentials)
63 }
64
65 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
66 if (merge) {
Jakub Josef0fcd75e2017-03-22 18:35:55 +010067 scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"])
Jakub Josef1b75ca82017-02-20 16:08:13 +010068 }
69 // we need wipe workspace before checkout
70 if (wipe) {
71 scmExtensions.add([$class: 'WipeWorkspace'])
72 }
73
chnyda96a1e8a2017-03-28 16:02:13 +020074 if (path == "") {
75 checkout(
76 scm: [
77 $class: 'GitSCM',
78 branches: [[name: "${gerritBranch}"]],
79 extensions: scmExtensions,
80 userRemoteConfigs: [scmUserRemoteConfigs]
81 ]
82 )
83 } else {
84 dir(path) {
85 checkout(
86 scm: [
87 $class: 'GitSCM',
88 branches: [[name: "${gerritBranch}"]],
89 extensions: scmExtensions,
90 userRemoteConfigs: [scmUserRemoteConfigs]
91 ]
92 )
93 }
94 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010095}