Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 1 | package 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 | */ |
| 30 | def 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 Josef | 4624370 | 2017-02-28 16:14:10 +0100 | [diff] [blame] | 42 | refspec: "${GERRIT_REFSPEC}" |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 43 | ] |
| 44 | |
| 45 | if (credentials == '') { |
| 46 | // then try to checkout in anonymous mode |
Jakub Josef | 4624370 | 2017-02-28 16:14:10 +0100 | [diff] [blame] | 47 | scmUserRemoteConfigs.put('url',"https://${GERRIT_HOST}/${GERRIT_PROJECT}") |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 48 | } else { |
| 49 | // else use ssh checkout |
Jakub Josef | 4624370 | 2017-02-28 16:14:10 +0100 | [diff] [blame] | 50 | scmUserRemoteConfigs.put('url',"ssh://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git") |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 51 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 52 | } |
| 53 | |
| 54 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 55 | if (merge) { |
Jakub Josef | 4624370 | 2017-02-28 16:14:10 +0100 | [diff] [blame] | 56 | scmExtensions.add([$class: 'LocalBranch', localBranch: "${GERRIT_BRANCH}"]) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 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', |
Jakub Josef | 4624370 | 2017-02-28 16:14:10 +0100 | [diff] [blame] | 66 | branches: [[name: "${GERRIT_BRANCH}"]], |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 67 | extensions: scmExtensions, |
| 68 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 69 | ] |
| 70 | ) |
| 71 | } |