Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 1 | package com.mirantis.mk |
| 2 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 3 | import java.util.regex.Pattern |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 4 | /** |
| 5 | * Gerrit functions |
| 6 | * |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Execute git clone and checkout stage from gerrit review |
| 11 | * |
| 12 | * @param config LinkedHashMap |
| 13 | * config includes next parameters: |
| 14 | * - credentialsId, id of user which should make checkout |
| 15 | * - withMerge, prevent detached mode in repo |
| 16 | * - withWipeOut, wipe repository and force clone |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 17 | * @return boolean result |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 18 | * |
| 19 | * Usage example: |
| 20 | * //anonymous gerrit checkout |
| 21 | * def gitFunc = new com.mirantis.mcp.Git() |
| 22 | * gitFunc.gerritPatchsetCheckout([ |
| 23 | * withMerge : true |
| 24 | * ]) |
| 25 | * |
| 26 | * def gitFunc = new com.mirantis.mcp.Git() |
| 27 | * gitFunc.gerritPatchsetCheckout([ |
| 28 | * credentialsId : 'mcp-ci-gerrit', |
| 29 | * withMerge : true |
| 30 | * ]) |
| 31 | */ |
| 32 | def gerritPatchsetCheckout(LinkedHashMap config) { |
| 33 | def merge = config.get('withMerge', false) |
| 34 | def wipe = config.get('withWipeOut', false) |
| 35 | def credentials = config.get('credentialsId','') |
Jakub Josef | 71c46a6 | 2017-03-29 14:55:33 +0200 | [diff] [blame] | 36 | def gerritScheme = config.get('gerritScheme', env["GERRIT_SCHEME"] ? env["GERRIT_SCHEME"] : "") |
| 37 | def gerritRefSpec = config.get('gerritRefSpec', env["GERRIT_REFSPEC"] ? env["GERRIT_REFSPEC"] : "") |
| 38 | def gerritName = config.get('gerritName', env["GERRIT_NAME"] ? env["GERRIT_NAME"] : "") |
| 39 | def gerritHost = config.get('gerritHost', env["GERRIT_HOST"] ? env["GERRIT_HOST"] : "") |
| 40 | def gerritPort = config.get('gerritPort', env["GERRIT_PORT"] ? env["GERRIT_PORT"] : "") |
| 41 | def gerritProject = config.get('gerritProject', env["GERRIT_PROJECT"] ? env["GERRIT_PROJECT"] : "") |
| 42 | def gerritBranch = config.get('gerritBranch', env["GERRIT_BRANCH"] ? env["GERRIT_BRANCH"] : "") |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 43 | def path = config.get('path', "") |
chnyda | 7d25fc9 | 2017-03-29 10:51:59 +0200 | [diff] [blame] | 44 | def depth = config.get('depth', 0) |
| 45 | def timeout = config.get('timeout', 20) |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 46 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 47 | if(_validGerritConfig(config)){ |
| 48 | // default parameters |
| 49 | def scmExtensions = [ |
| 50 | [$class: 'CleanCheckout'], |
| 51 | [$class: 'BuildChooserSetting', buildChooser: [$class: 'GerritTriggerBuildChooser']], |
| 52 | [$class: 'CheckoutOption', timeout: timeout], |
| 53 | [$class: 'CloneOption', depth: depth, noTags: false, reference: '', shallow: depth > 0, timeout: timeout] |
| 54 | ] |
| 55 | def scmUserRemoteConfigs = [ |
| 56 | name: 'gerrit', |
| 57 | refspec: gerritRefSpec |
| 58 | ] |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 59 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 60 | if (credentials == '') { |
| 61 | // then try to checkout in anonymous mode |
| 62 | scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}") |
| 63 | } else { |
| 64 | // else use ssh checkout |
| 65 | scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git") |
| 66 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 67 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 68 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 69 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 70 | if (merge) { |
| 71 | scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"]) |
| 72 | } |
| 73 | // we need wipe workspace before checkout |
| 74 | if (wipe) { |
| 75 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 76 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 77 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 78 | if (path == "") { |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 79 | checkout( |
| 80 | scm: [ |
| 81 | $class: 'GitSCM', |
| 82 | branches: [[name: "${gerritBranch}"]], |
| 83 | extensions: scmExtensions, |
| 84 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 85 | ] |
| 86 | ) |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 87 | } else { |
| 88 | dir(path) { |
| 89 | checkout( |
| 90 | scm: [ |
| 91 | $class: 'GitSCM', |
| 92 | branches: [[name: "${gerritBranch}"]], |
| 93 | extensions: scmExtensions, |
| 94 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 95 | ] |
| 96 | ) |
| 97 | } |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 98 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 99 | return true |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 100 | }else{ |
| 101 | def common = new com.mirantis.mk.Common() |
| 102 | common.errorMsg("Cannot perform gerrit checkout, given config file is not valid") |
| 103 | return false |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 104 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 105 | } |
| 106 | /** |
| 107 | * Execute git clone and checkout stage from gerrit review |
| 108 | * |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame^] | 109 | * @param gerritUrl gerrit url with scheme |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 110 | * "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame^] | 111 | * @param gerritRef gerrit ref spec |
| 112 | * @param gerritBranch gerrit branch |
| 113 | * @param credentialsId jenkins credentials id |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 114 | * @return boolean result |
| 115 | */ |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame^] | 116 | def gerritPatchsetCheckout(gerritUrl, gerritRef, gerritBranch, credentialsId) { |
| 117 | def gerritParams = _getGerritParamsFromUrl(gerritUrl) |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 118 | if(gerritParams.size() == 5){ |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 119 | gerritPatchsetCheckout([ |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame^] | 120 | credentialsId : credentialsId, |
| 121 | gerritBranch: gerritBranch, |
| 122 | gerritRefSpec: gerritRef, |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 123 | gerritScheme: gerritParams[0], |
| 124 | gerritName: gerritParams[1], |
| 125 | gerritHost: gerritParams[2], |
| 126 | gerritPort: gerritParams[3], |
| 127 | gerritProject: gerritParams[4] |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 128 | ]) |
| 129 | return true |
| 130 | } |
| 131 | return false |
| 132 | } |
| 133 | |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 134 | @NonCPS |
| 135 | def _getGerritParamsFromUrl(gitUrl){ |
| 136 | def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)") |
| 137 | def gitUrlMatcher = gitUrlPattern.matcher(gitUrl) |
| 138 | if(gitUrlMatcher.find() && gitUrlMatcher.groupCount() == 5){ |
| 139 | return [gitUrlMatcher.group(1),gitUrlMatcher.group(2),gitUrlMatcher.group(3),gitUrlMatcher.group(4),gitUrlMatcher.group(5)] |
| 140 | } |
| 141 | return [] |
| 142 | } |
| 143 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 144 | def _validGerritConfig(LinkedHashMap config){ |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 145 | return config.get("gerritScheme","") != null && config.get("gerritScheme","") != "" && |
| 146 | config.get("gerritName","") != null && config.get("gerritName","") != "" && |
| 147 | config.get("gerritHost","") != null && config.get("gerritHost","") != "" && |
| 148 | config.get("gerritPort","") != null && config.get("gerritPort","") != "" && |
| 149 | config.get("gerritProject","") != null && config.get("gerritProject","") != "" && |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame^] | 150 | config.get("gerritBranch","") != null && config.get("gerritBranch","") != "" && |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 151 | config.get("gerritRefSpec","") != null && config.get("gerritRefSpec","") != "" |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 152 | } |