blob: f389df57c2f4140ea9c2bcd82c2f0d1e11938cfe [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','')
34
35 // default parameters
36 def scmExtensions = [
37 [$class: 'CleanCheckout'],
38 [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']]
39 ]
40 def scmUserRemoteConfigs = [
41 name: 'gerrit',
42 refspec: config.get('gerritRefSpec','')
43 ]
44
45 if (credentials == '') {
46 // then try to checkout in anonymous mode
47 scmUserRemoteConfigs.put('url', config.get('gerritUrl',''))
48 } else {
49 // else use ssh checkout
50 scmUserRemoteConfigs.put('url',config.get('gerritUrl',''))
51 scmUserRemoteConfigs.put('credentialsId',credentials)
52 }
53
54 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
55 if (merge) {
56 scmExtensions.add([$class: 'LocalBranch', localBranch: config.get('gerritBranch','master')])
57 }
58 // we need wipe workspace before checkout
59 if (wipe) {
60 scmExtensions.add([$class: 'WipeWorkspace'])
61 }
62
63 checkout(
64 scm: [
65 $class: 'GitSCM',
66 branches: [[name: config.get('gerritBranch','master')]],
67 extensions: scmExtensions,
68 userRemoteConfigs: [scmUserRemoteConfigs]
69 ]
70 )
71}