blob: 81e165ba5f4409acebb1b93627bb436b13981ba0 [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',
Jakub Josef46243702017-02-28 16:14:10 +010042 refspec: "${GERRIT_REFSPEC}"
Jakub Josef1b75ca82017-02-20 16:08:13 +010043 ]
44
45 if (credentials == '') {
46 // then try to checkout in anonymous mode
Jakub Josef46243702017-02-28 16:14:10 +010047 scmUserRemoteConfigs.put('url',"https://${GERRIT_HOST}/${GERRIT_PROJECT}")
Jakub Josef1b75ca82017-02-20 16:08:13 +010048 } else {
49 // else use ssh checkout
Jakub Josef46243702017-02-28 16:14:10 +010050 scmUserRemoteConfigs.put('url',"ssh://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git")
Jakub Josef1b75ca82017-02-20 16:08:13 +010051 scmUserRemoteConfigs.put('credentialsId',credentials)
52 }
53
54 // if we need to "merge" code from patchset to GERRIT_BRANCH branch
55 if (merge) {
Jakub Josef46243702017-02-28 16:14:10 +010056 scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"])
Jakub Josef1b75ca82017-02-20 16:08:13 +010057 }
58 // we need wipe workspace before checkout
59 if (wipe) {
60 scmExtensions.add([$class: 'WipeWorkspace'])
61 }
62
63 checkout(
64 scm: [
65 $class: 'GitSCM',
Jakub Josef46243702017-02-28 16:14:10 +010066 branches: [[name: "${GERRIT_BRANCH}"]],
Jakub Josef1b75ca82017-02-20 16:08:13 +010067 extensions: scmExtensions,
68 userRemoteConfigs: [scmUserRemoteConfigs]
69 ]
70 )
71}