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', |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 57 | ] |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 58 | if(gerritRefSpec && gerritRefSpec != ""){ |
| 59 | scmUserRemoteConfigs.put('refspec', gerritRefSpec) |
| 60 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 61 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 62 | if (credentials == '') { |
| 63 | // then try to checkout in anonymous mode |
| 64 | scmUserRemoteConfigs.put('url',"${gerritScheme}://${gerritHost}/${gerritProject}") |
| 65 | } else { |
| 66 | // else use ssh checkout |
| 67 | scmUserRemoteConfigs.put('url',"ssh://${gerritName}@${gerritHost}:${gerritPort}/${gerritProject}.git") |
| 68 | scmUserRemoteConfigs.put('credentialsId',credentials) |
| 69 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 70 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 71 | // if we need to "merge" code from patchset to GERRIT_BRANCH branch |
| 72 | if (merge) { |
| 73 | scmExtensions.add([$class: 'LocalBranch', localBranch: "${gerritBranch}"]) |
| 74 | } |
| 75 | // we need wipe workspace before checkout |
| 76 | if (wipe) { |
| 77 | scmExtensions.add([$class: 'WipeWorkspace']) |
| 78 | } |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 79 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 80 | if (path == "") { |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 81 | checkout( |
| 82 | scm: [ |
| 83 | $class: 'GitSCM', |
| 84 | branches: [[name: "${gerritBranch}"]], |
| 85 | extensions: scmExtensions, |
| 86 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 87 | ] |
| 88 | ) |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 89 | } else { |
| 90 | dir(path) { |
| 91 | checkout( |
| 92 | scm: [ |
| 93 | $class: 'GitSCM', |
| 94 | branches: [[name: "${gerritBranch}"]], |
| 95 | extensions: scmExtensions, |
| 96 | userRemoteConfigs: [scmUserRemoteConfigs] |
| 97 | ] |
| 98 | ) |
| 99 | } |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 100 | } |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 101 | return true |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 102 | }else{ |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 103 | throw new Exception("Cannot perform gerrit checkout, given config file is not valid") |
chnyda | 96a1e8a | 2017-03-28 16:02:13 +0200 | [diff] [blame] | 104 | } |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 105 | return false |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 106 | } |
| 107 | /** |
| 108 | * Execute git clone and checkout stage from gerrit review |
| 109 | * |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 110 | * @param gerritUrl gerrit url with scheme |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 111 | * "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}.git |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 112 | * @param gerritRef gerrit ref spec |
| 113 | * @param gerritBranch gerrit branch |
| 114 | * @param credentialsId jenkins credentials id |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 115 | * @return boolean result |
| 116 | */ |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 117 | def gerritPatchsetCheckout(gerritUrl, gerritRef, gerritBranch, credentialsId) { |
| 118 | def gerritParams = _getGerritParamsFromUrl(gerritUrl) |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 119 | if(gerritParams.size() == 5){ |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 120 | gerritPatchsetCheckout([ |
Jakub Josef | ad34dbf | 2017-03-29 17:52:31 +0200 | [diff] [blame] | 121 | credentialsId : credentialsId, |
| 122 | gerritBranch: gerritBranch, |
| 123 | gerritRefSpec: gerritRef, |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 124 | gerritScheme: gerritParams[0], |
| 125 | gerritName: gerritParams[1], |
| 126 | gerritHost: gerritParams[2], |
| 127 | gerritPort: gerritParams[3], |
| 128 | gerritProject: gerritParams[4] |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 129 | ]) |
| 130 | return true |
| 131 | } |
| 132 | return false |
| 133 | } |
| 134 | |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 135 | /** |
| 136 | * Return gerrit change object from gerrit API |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 137 | * @param gerritName gerrit user name (usually GERRIT_NAME property) |
Jakub Josef | b735dd4 | 2017-04-10 15:31:19 +0200 | [diff] [blame^] | 138 | * @param gerritHost gerrit host (usually GERRIT_HOST property) |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 139 | * @param gerritChangeNumber gerrit change number (usually GERRIT_CHANGE_NUMBER property) |
| 140 | * @param credentialsId jenkins credentials id for gerrit |
| 141 | * @return gerrit change object |
| 142 | */ |
Jakub Josef | b735dd4 | 2017-04-10 15:31:19 +0200 | [diff] [blame^] | 143 | def getGerritChange(gerritName, gerritHost, gerritChangeNumber, credentialsId){ |
Jakub Josef | 50c9c3a | 2017-04-10 14:32:35 +0200 | [diff] [blame] | 144 | def common = new com.mirantis.mk.Common() |
| 145 | def ssh = new com.mirantis.mk.Ssh() |
| 146 | ssh.prepareSshAgentKey(credentialsId) |
| 147 | ssh.ensureKnownHosts(gerritHost) |
| 148 | return common.parseJSON(ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --format=JSON change:%s", gerritName, gerritHost, gerritChangeNumber))) |
| 149 | } |
| 150 | |
Jakub Josef | d383f39 | 2017-03-29 16:52:04 +0200 | [diff] [blame] | 151 | @NonCPS |
| 152 | def _getGerritParamsFromUrl(gitUrl){ |
| 153 | def gitUrlPattern = Pattern.compile("(.+):\\/\\/(.+)@(.+):(.+)\\/(.+)") |
| 154 | def gitUrlMatcher = gitUrlPattern.matcher(gitUrl) |
| 155 | if(gitUrlMatcher.find() && gitUrlMatcher.groupCount() == 5){ |
| 156 | return [gitUrlMatcher.group(1),gitUrlMatcher.group(2),gitUrlMatcher.group(3),gitUrlMatcher.group(4),gitUrlMatcher.group(5)] |
| 157 | } |
| 158 | return [] |
| 159 | } |
| 160 | |
Jakub Josef | c70c2a3 | 2017-03-29 16:38:30 +0200 | [diff] [blame] | 161 | def _validGerritConfig(LinkedHashMap config){ |
Jakub Josef | 73d6214 | 2017-03-29 17:07:18 +0200 | [diff] [blame] | 162 | return config.get("gerritScheme","") != null && config.get("gerritScheme","") != "" && |
| 163 | config.get("gerritName","") != null && config.get("gerritName","") != "" && |
| 164 | config.get("gerritHost","") != null && config.get("gerritHost","") != "" && |
| 165 | config.get("gerritPort","") != null && config.get("gerritPort","") != "" && |
| 166 | config.get("gerritProject","") != null && config.get("gerritProject","") != "" && |
Jakub Josef | 30fc921 | 2017-04-04 11:47:19 +0200 | [diff] [blame] | 167 | config.get("gerritBranch","") != null && config.get("gerritBranch","") != "" |
Jakub Josef | 1b75ca8 | 2017-02-20 16:08:13 +0100 | [diff] [blame] | 168 | } |