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', "") |
chnyda | 7d25fc9 | 2017-03-29 10:51:59 +0200 | [diff] [blame^] | 41 | def depth = config.get('depth', 0) |
| 42 | def timeout = config.get('timeout', 20) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 43 | |
| 44 | // default parameters |
| 45 | def scmExtensions = [ |
| 46 | [$class: 'CleanCheckout'], |
chnyda | 7d25fc9 | 2017-03-29 10:51:59 +0200 | [diff] [blame^] | 47 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']], |
| 48 | [$class: 'CheckoutOption', timeout: timeout], |
| 49 | [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout] |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 50 | ] |
| 51 | def scmUserRemoteConfigs = [ |
| 52 | name: 'gerrit', |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 53 | refspec: gerritRefSpec |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 54 | ] |
| 55 | |
| 56 | if (credentials == '') { |
| 57 | // then try to checkout in anonymous mode |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 58 | scmUserRemoteConfigs.put('url',"https://${gerritHost}/${gerritProject}") |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 59 | } else { |
| 60 | // else use ssh checkout |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 61 | scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git") |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 62 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 63 | } |
| 64 | |
| 65 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 66 | if (merge) { |
Jakub Josef | 0fcd75e | 2017-03-22 18:35:55 +0100 | [diff] [blame] | 67 | scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"]) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 68 | } |
| 69 | // we need wipe workspace before checkout |
| 70 | if (wipe) { |
| 71 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 72 | } |
| 73 | |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 74 | if (path == "") { |
| 75 | checkout( |
| 76 | scm: [ |
| 77 | $class: 'GitSCM', |
| 78 | branches: [[name: "${gerritBranch}"]], |
| 79 | extensions: scmExtensions, |
| 80 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 81 | ] |
| 82 | ) |
| 83 | } else { |
| 84 | dir(path) { |
| 85 | checkout( |
| 86 | scm: [ |
| 87 | $class: 'GitSCM', |
| 88 | branches: [[name: "${gerritBranch}"]], |
| 89 | extensions: scmExtensions, |
| 90 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 91 | ] |
| 92 | ) |
| 93 | } |
| 94 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 95 | } |