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','') |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 34 | 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) |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame^] | 40 | def path = config.get('path', "") |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 41 | |
| 42 | // default parameters |
| 43 | def scmExtensions = [ |
| 44 | [$class: 'CleanCheckout'], |
| 45 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']] |
| 46 | ] |
| 47 | def scmUserRemoteConfigs = [ |
| 48 | name: 'gerrit', |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 49 | refspec: gerritRefSpec |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 50 | ] |
| 51 | |
| 52 | if (credentials == '') { |
| 53 | // then try to checkout in anonymous mode |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 54 | scmUserRemoteConfigs.put('url',"https://${gerritHost}/${gerritProject}") |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 55 | } else { |
| 56 | // else use ssh checkout |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 57 | scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git") |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 58 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 59 | } |
| 60 | |
| 61 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 62 | if (merge) { |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 63 | scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"]) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 64 | } |
| 65 | // we need wipe workspace before checkout |
| 66 | if (wipe) { |
| 67 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 68 | } |
| 69 | |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame^] | 70 | 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 Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 91 | } |