blob: c113d28c6dd0c831ffba60300f792a3922428fbb [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
Jakub Josefaea1c632017-06-26 14:03:07 +020022 if(gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Code-Review", "+")){
Jakub Josef7ed33e42017-06-23 16:51:34 +020023 doSubmit = true
Ruslan Kamaldinov310ffc02017-08-08 16:07:42 +040024 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
25 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
26 def jobsNamespace = JOBS_NAMESPACE
27 def plural_namespaces = ['salt-formulas', 'salt-models']
28 // remove plural s on the end of job namespace
29 if (JOBS_NAMESPACE in plural_namespaces){
30 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
31 }
32 // salt-formulas tests have -latest on end of the name
33 if(JOBS_NAMESPACE.equals("salt-formulas")){
34 gerritProject=gerritProject+"-latest"
35 }
36 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
37 if (_jobExists(testJob)) {
38 common.infoMsg("Test job ${testJob} found, running")
39 def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet,"Verified", "+")
40 build job: testJob, parameters: [
41 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
42 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
43 ]
44 giveVerify = true
45 } else {
46 common.infoMsg("Test job ${testJob} not found")
Jakub Josef617e1162017-03-21 15:58:27 +010047 }
Jakub Josef7ed33e42017-06-23 16:51:34 +020048 } else {
49 common.errorMsg("Change don't have a CodeReview, skipping gate")
Jakub Josefa722d432017-03-10 12:43:00 +010050 }
Jakub Josef617e1162017-03-21 15:58:27 +010051 } else {
52 common.infoMsg("Test job skipped")
Jakub Josefa722d432017-03-10 12:43:00 +010053 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010054 }
55 stage("submit review"){
Jakub Josef841aac22017-04-05 14:00:35 +020056 if(gerritChange.status == "MERGED"){
Jakub Josef09d9d032017-04-04 17:51:03 +020057 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
Jakub Josef7ed33e42017-06-23 16:51:34 +020058 }else if(doSubmit){
59 if(giveVerify){
60 common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
61 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))
62 }else{
63 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
64 }
Jakub Josef09d9d032017-04-04 17:51:03 +020065 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
66 }
Jakub Joseffd91d1f2017-03-08 17:27:41 +010067 }
Jakub Joseff2a66e92017-03-08 17:21:07 +010068 } catch (Throwable e) {
69 // If there was an error or exception thrown, the build failed
70 currentBuild.result = "FAILURE"
71 throw e
Jakub Joseff2a66e92017-03-08 17:21:07 +010072 }
Jakub Josef4251feb2017-03-10 16:10:53 +010073}
74
75@NonCPS
76def _jobExists(jobName){
Jakub Josef253d4482017-03-13 14:04:46 +010077 return Jenkins.instance.items.find{it -> it.name.equals(jobName)}
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040078}