blob: a870ad494885d6b6df6b747e32ca64baa2ffc434 [file] [log] [blame]
Jakub Joseff2a66e92017-03-08 17:21:07 +01001/**
2 * Gerrit gating pipeline
3 * CREDENTIALS_ID - Gerrit credentails ID
Jakub Josefb62d00a2017-03-13 12:21:40 +01004 * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...)
Jakub Joseff2a66e92017-03-08 17:21:07 +01005 *
6**/
7
8def common = new com.mirantis.mk.Common()
9def gerrit = new com.mirantis.mk.Gerrit()
Jakub Josefeda07682017-03-09 14:50:54 +010010def ssh = new com.mirantis.mk.Ssh()
Jakub Joseff2a66e92017-03-08 17:21:07 +010011node("python") {
12 try{
Jakub Josef09d9d032017-04-04 17:51:03 +020013 // test if change is not already merged
14 ssh.prepareSshAgentKey(CREDENTIALS_ID)
15 ssh.ensureKnownHosts(GERRIT_HOST)
Jakub Josef7ed33e42017-06-23 16:51:34 +020016 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
17 def doSubmit = false
18 def giveVerify = false
Jakub Joseff2a66e92017-03-08 17:21:07 +010019 stage("test") {
Jakub Josef841aac22017-04-05 14:00:35 +020020 if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")){
Jakub Josef7ed33e42017-06-23 16:51:34 +020021 // test max CodeReview
22 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"CodeReview", "+")){
23 doSubmit = true
24 wrap([$class: 'AnsiColorBuildWrapper']) {
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 }
Jakub Josef617e1162017-03-21 15:58:27 +010049 }
Jakub Josef7ed33e42017-06-23 16:51:34 +020050 } else {
51 common.errorMsg("Change don't have a CodeReview, skipping gate")
Jakub Josefa722d432017-03-10 12:43:00 +010052 }
Jakub Josef617e1162017-03-21 15:58:27 +010053 } else {
54 common.infoMsg("Test job skipped")
Jakub Josefa722d432017-03-10 12:43:00 +010055 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010056 }
57 stage("submit review"){
Jakub Josef841aac22017-04-05 14:00:35 +020058 if(gerritChange.status == "MERGED"){
Jakub Josef09d9d032017-04-04 17:51:03 +020059 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
Jakub Josef7ed33e42017-06-23 16:51:34 +020060 }else if(doSubmit){
61 if(giveVerify){
62 common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
63 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))
64 }else{
65 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
66 }
Jakub Josef09d9d032017-04-04 17:51:03 +020067 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
68 }
Jakub Joseffd91d1f2017-03-08 17:27:41 +010069 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010070 } catch (Throwable e) {
71 // If there was an error or exception thrown, the build failed
72 currentBuild.result = "FAILURE"
73 throw e
Jakub Joseff2a66e92017-03-08 17:21:07 +010074 }
Jakub Josef4251feb2017-03-10 16:10:53 +010075}
76
77@NonCPS
78def _jobExists(jobName){
Jakub Josef253d4482017-03-13 14:04:46 +010079 return Jenkins.instance.items.find{it -> it.name.equals(jobName)}
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040080}