blob: dc3de1b5c4c16fcc67c8e57912f8d058aa1bee0a [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)
Jakub Josef1b75ca82017-02-20 16:08:13 +010040
41 // default parameters
42 def scmExtensions = [
43 [$class: 'CleanCheckout'],
44 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
45 ]
46 def scmUserRemoteConfigs = [
47 name: 'gerrit',
Jakub Josef0fcd75e2017-03-22 18:35:55 +010048 refspec: gerritRefSpec
Jakub Josef1b75ca82017-02-20 16:08:13 +010049 ]
50
51 if (credentials == '') {
52 // then try to checkout in anonymous mode
Jakub Josef0fcd75e2017-03-22 18:35:55 +010053 scmUserRemoteConfigs.put('url',"https://${gerritHost}/${gerritProject}")
Jakub Josef1b75ca82017-02-20 16:08:13 +010054 } else {
55 // else use ssh checkout
Jakub Josef0fcd75e2017-03-22 18:35:55 +010056 scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git")
Jakub Josef1b75ca82017-02-20 16:08:13 +010057 scmUserRemoteConfigs.put('credentialsId',credentials)
58 }
59
60 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
61 if (merge) {
Jakub Josef0fcd75e2017-03-22 18:35:55 +010062 scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"])
Jakub Josef1b75ca82017-02-20 16:08:13 +010063 }
64 // we need wipe workspace before checkout
65 if (wipe) {
66 scmExtensions.add([$class: 'WipeWorkspace'])
67 }
68
69 checkout(
70 scm: [
71 $class: 'GitSCM',
Jakub Josef0fcd75e2017-03-22 18:35:55 +010072 branches: [[name: "${gerritBranch}"]],
Jakub Josef1b75ca82017-02-20 16:08:13 +010073 extensions: scmExtensions,
74 userRemoteConfigs: [scmUserRemoteConfigs]
75 ]
76 )
77}