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 | **/ |
| 7 | |
| 8 | def common = new com.mirantis.mk.Common() |
| 9 | def gerrit = new com.mirantis.mk.Gerrit() |
Jakub Josef | eda0768 | 2017-03-09 14:50:54 +0100 | [diff] [blame] | 10 | def ssh = new com.mirantis.mk.Ssh() |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 11 | timeout(time: 12, unit: 'HOURS') { |
| 12 | node("python") { |
| 13 | try{ |
| 14 | // test if change is not already merged |
| 15 | ssh.prepareSshAgentKey(CREDENTIALS_ID) |
| 16 | ssh.ensureKnownHosts(GERRIT_HOST) |
| 17 | def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true) |
| 18 | def doSubmit = false |
| 19 | def giveVerify = false |
| 20 | stage("test") { |
| 21 | if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")){ |
| 22 | // test max CodeReview |
| 23 | if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Code-Review", "+")){ |
| 24 | doSubmit = true |
| 25 | def gerritProjectArray = GERRIT_PROJECT.tokenize("/") |
| 26 | def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1] |
| 27 | def jobsNamespace = JOBS_NAMESPACE |
| 28 | def plural_namespaces = ['salt-formulas', 'salt-models'] |
| 29 | // remove plural s on the end of job namespace |
| 30 | if (JOBS_NAMESPACE in plural_namespaces){ |
| 31 | jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1) |
| 32 | } |
| 33 | // salt-formulas tests have -latest on end of the name |
| 34 | if(JOBS_NAMESPACE.equals("salt-formulas")){ |
| 35 | gerritProject=gerritProject+"-latest" |
| 36 | } |
| 37 | def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject) |
| 38 | if (_jobExists(testJob)) { |
| 39 | common.infoMsg("Test job ${testJob} found, running") |
| 40 | def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+") |
| 41 | build job: testJob, parameters: [ |
| 42 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"], |
| 43 | [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC] |
| 44 | ] |
| 45 | giveVerify = true |
| 46 | } else { |
| 47 | common.infoMsg("Test job ${testJob} not found") |
| 48 | } |
Ruslan Kamaldinov | 310ffc0 | 2017-08-08 16:07:42 +0400 | [diff] [blame] | 49 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 50 | common.errorMsg("Change don't have a CodeReview, skipping gate") |
Jakub Josef | 617e116 | 2017-03-21 15:58:27 +0100 | [diff] [blame] | 51 | } |
Jakub Josef | 7ed33e4 | 2017-06-23 16:51:34 +0200 | [diff] [blame] | 52 | } else { |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 53 | common.infoMsg("Test job skipped") |
Jakub Josef | a722d43 | 2017-03-10 12:43:00 +0100 | [diff] [blame] | 54 | } |
| 55 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 56 | stage("submit review"){ |
| 57 | if(gerritChange.status == "MERGED"){ |
| 58 | common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them") |
| 59 | }else if(doSubmit){ |
| 60 | if(giveVerify){ |
| 61 | common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting") |
| 62 | ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --verified +1 --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 63 | }else{ |
| 64 | ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
| 65 | } |
| 66 | common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER)) |
Jakub Josef | 7ed33e4 | 2017-06-23 16:51:34 +0200 | [diff] [blame] | 67 | } |
Jakub Josef | 09d9d03 | 2017-04-04 17:51:03 +0200 | [diff] [blame] | 68 | } |
Jakub Josef | a63f986 | 2018-01-11 17:58:38 +0100 | [diff] [blame] | 69 | } catch (Throwable e) { |
| 70 | // If there was an error or exception thrown, the build failed |
| 71 | currentBuild.result = "FAILURE" |
| 72 | currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message |
| 73 | throw e |
Jakub Josef | fd91d1f | 2017-03-08 17:27:41 +0100 | [diff] [blame] | 74 | } |
Jakub Josef | f2a66e9 | 2017-03-08 17:21:07 +0100 | [diff] [blame] | 75 | } |
Jakub Josef | 4251feb | 2017-03-10 16:10:53 +0100 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | @NonCPS |
| 79 | def _jobExists(jobName){ |
Jakub Josef | 253d448 | 2017-03-13 14:04:46 +0100 | [diff] [blame] | 80 | return Jenkins.instance.items.find{it -> it.name.equals(jobName)} |
Mikhail Ivanov | f5cd07f | 2017-04-11 17:09:54 +0400 | [diff] [blame] | 81 | } |