blob: e7340a38df8a47ca8ca87f7338cc92b4458036a9 [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', "")
Jakub Josef1b75ca82017-02-20 16:08:13 +010041
42 // default parameters
43 def scmExtensions = [
44 [$class: 'CleanCheckout'],
45 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
46 ]
47 def scmUserRemoteConfigs = [
48 name: 'gerrit',
Jakub Josef0fcd75e2017-03-22 18:35:55 +010049 refspec: gerritRefSpec
Jakub Josef1b75ca82017-02-20 16:08:13 +010050 ]
51
52 if (credentials == '') {
53 // then try to checkout in anonymous mode
Jakub Josef0fcd75e2017-03-22 18:35:55 +010054 scmUserRemoteConfigs.put('url',"https://${gerritHost}/${gerritProject}")
Jakub Josef1b75ca82017-02-20 16:08:13 +010055 } else {
56 // else use ssh checkout
Jakub Josef0fcd75e2017-03-22 18:35:55 +010057 scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git")
Jakub Josef1b75ca82017-02-20 16:08:13 +010058 scmUserRemoteConfigs.put('credentialsId',credentials)
59 }
60
61 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
62 if (merge) {
Jakub Josef0fcd75e2017-03-22 18:35:55 +010063 scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"])
Jakub Josef1b75ca82017-02-20 16:08:13 +010064 }
65 // we need wipe workspace before checkout
66 if (wipe) {
67 scmExtensions.add([$class: 'WipeWorkspace'])
68 }
69
chnyda96a1e8a2017-03-28 16:02:13 +020070 if (path == "") {
71 checkout(
72 scm: [
73 $class: 'GitSCM',
74 branches: [[name: "${gerritBranch}"]],
75 extensions: scmExtensions,
76 userRemoteConfigs: [scmUserRemoteConfigs]
77 ]
78 )
79 } else {
80 dir(path) {
81 checkout(
82 scm: [
83 $class: 'GitSCM',
84 branches: [[name: "${gerritBranch}"]],
85 extensions: scmExtensions,
86 userRemoteConfigs: [scmUserRemoteConfigs]
87 ]
88 )
89 }
90 }
Jakub Josef1b75ca82017-02-20 16:08:13 +010091}