Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 1 | /** |
| 2 | * Gerrit gating pipeline |
| 3 | * CREDENTIALS_ID - Gerrit credentails ID |
Jakub Josef | b62d00a | 2017-03-13 12:21:40 +0100 | [diff] [blame] | 4 | * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...) |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 5 | * |
| 6 | **/ |
Jakub Josef | 09d9d03 | 2017-04-04 17:51:03 +0200 | [diff] [blame] | 7 | import groovy.json.JsonSlurper |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 8 | |
| 9 | def common = new com.mirantis.mk.Common() |
| 10 | def gerrit = new com.mirantis.mk.Gerrit() |
Jakub Josef | eda0768 | 2017-03-09 14:50:54 +0100 | [diff] [blame] | 11 | def ssh = new com.mirantis.mk.Ssh() |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 12 | node("python") { |
| 13 | try{ |
Jakub Josef | 09d9d03 | 2017-04-04 17:51:03 +0200 | [diff] [blame] | 14 | // test if change is not already merged |
| 15 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
| 16 | ssh.ensureKnownHosts(GERRIT_HOST) |
Jakub Josef | dac4f11 | 2017-04-05 11:59:40 +0200 | [diff] [blame] | 17 | def gerritChangeStatus = _getGerritChangeStatus(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER) |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 18 | stage("test") { |
Jakub Josef | dac4f11 | 2017-04-05 11:59:40 +0200 | [diff] [blame] | 19 | if (gerritChangeStatus != "MERGED" && !SKIP_TEST.equals("true")){ |
Jakub Josef | 617e116 | 2017-03-21 15:58:27 +0100 | [diff] [blame] | 20 | wrap([$class: 'AnsiColorBuildWrapper']) { |
| 21 | def gerritProjectArray = GERRIT_PROJECT.tokenize("/") |
| 22 | def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1] |
| 23 | def jobsNamespace = JOBS_NAMESPACE |
| 24 | // remove plural s on the end of job namespace |
| 25 | if (JOBS_NAMESPACE[JOBS_NAMESPACE.length() - 1].equals("s")){ |
| 26 | jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1) |
| 27 | } |
Jakub Josef | f64c340 | 2017-03-21 18:58:40 +0100 | [diff] [blame] | 28 | def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject) |
Jakub Josef | 617e116 | 2017-03-21 15:58:27 +0100 | [diff] [blame] | 29 | if (_jobExists(testJob)) { |
| 30 | common.infoMsg("Test job ${testJob} found, running") |
| 31 | build job: testJob, parameters: [ |
Jakub Josef | c1c2ec2 | 2017-03-31 18:59:03 +0200 | [diff] [blame] | 32 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"], |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 33 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC] |
Jakub Josef | 617e116 | 2017-03-21 15:58:27 +0100 | [diff] [blame] | 34 | ] |
| 35 | } else { |
| 36 | common.infoMsg("Test job ${testJob} not found") |
| 37 | } |
Jakub Josef | a722d43 | 2017-03-10 12:43:00 +0100 | [diff] [blame] | 38 | } |
Jakub Josef | 617e116 | 2017-03-21 15:58:27 +0100 | [diff] [blame] | 39 | } else { |
| 40 | common.infoMsg("Test job skipped") |
Jakub Josef | a722d43 | 2017-03-10 12:43:00 +0100 | [diff] [blame] | 41 | } |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 42 | } |
| 43 | stage("submit review"){ |
Jakub Josef | dac4f11 | 2017-04-05 11:59:40 +0200 | [diff] [blame] | 44 | if(gerritChangeStatus == "MERGED"){ |
Jakub Josef | 09d9d03 | 2017-04-04 17:51:03 +0200 | [diff] [blame] | 45 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| 46 | }else{ |
| 47 | ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 48 | common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 49 | } |
Jakub Josef | fd91d1f | 2017-03-08 17:27:41 +0100 | [diff] [blame] | 50 | } |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 51 | } catch (Throwable e) { |
| 52 | // If there was an error or exception thrown, the build failed |
| 53 | currentBuild.result = "FAILURE" |
| 54 | throw e |
| 55 | } finally { |
Jakub Josef | 8337931 | 2017-03-29 18:12:34 +0200 | [diff] [blame] | 56 | //common.sendNotification(currentBuild.result,"",["slack"]) |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 57 | } |
Jakub Josef | 4251feb | 2017-03-10 16:10:53 +0100 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | @NonCPS |
| 61 | def _jobExists(jobName){ |
Jakub Josef | 253d448 | 2017-03-13 14:04:46 +0100 | [diff] [blame] | 62 | return Jenkins.instance.items.find{it -> it.name.equals(jobName)} |
| 63 | } |
Jakub Josef | dac4f11 | 2017-04-05 11:59:40 +0200 | [diff] [blame] | 64 | |
| 65 | @NonCPS |
| 66 | def _getGerritChangeStatus(gerritName, gerritHost, gerritChange){ |
Jakub Josef | 4830c1a | 2017-04-05 12:16:53 +0200 | [diff] [blame] | 67 | def ssh = new com.mirantis.mk.Ssh() |
Jakub Josef | dac4f11 | 2017-04-05 11:59:40 +0200 | [diff] [blame] | 68 | def output = ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit query --format=JSON change:%s", gerritName, gerritHost, gerritChange)) |
| 69 | def jsonSlurper = new JsonSlurper() |
Jakub Josef | f8ca0dc | 2017-04-05 12:13:29 +0200 | [diff] [blame] | 70 | def gerritChangeObject = jsonSlurper.parseText(output) |
| 71 | if(gerritChangeObject["status"]){ |
| 72 | return gerritChangeObject["status"] |
Jakub Josef | dac4f11 | 2017-04-05 11:59:40 +0200 | [diff] [blame] | 73 | }else{ |
| 74 | return "ERROR" |
| 75 | } |
| 76 | } |