blob: 99f487dd97af305ea503af758822572255399ea6 [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 *
azvyagintsev5173bc52018-09-21 13:24:59 +03006 **/
Jakub Joseff2a66e92017-03-08 17:21:07 +01007
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()
azvyagintsev5173bc52018-09-21 13:24:59 +030011
Jakub Josef4251feb2017-03-10 16:10:53 +010012
13@NonCPS
azvyagintsev5173bc52018-09-21 13:24:59 +030014def isJobExists(jobName) {
15 return Jenkins.instance.items.find { it -> it.name.equals(jobName) }
Mikhail Ivanovf5cd07f2017-04-11 17:09:54 +040016}
azvyagintsev5173bc52018-09-21 13:24:59 +030017
18slaveNode = env.SLAVE_NODE ?: 'docker'
19
20timeout(time: 12, unit: 'HOURS') {
21 node(slaveNode) {
22 try {
23 // test if change is not already merged
24 ssh.prepareSshAgentKey(CREDENTIALS_ID)
25 ssh.ensureKnownHosts(GERRIT_HOST)
26 def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
27 def doSubmit = false
28 def giveVerify = false
29 stage("test") {
30 if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")) {
31 // test max CodeReview
32 if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) {
33 doSubmit = true
34 def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
35 def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
36 def jobsNamespace = JOBS_NAMESPACE
37 def plural_namespaces = ['salt-formulas', 'salt-models']
38 // remove plural s on the end of job namespace
39 if (JOBS_NAMESPACE in plural_namespaces) {
40 jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
41 }
42 // salt-formulas tests have -latest on end of the name
43 if (JOBS_NAMESPACE.equals("salt-formulas")) {
44 gerritProject = gerritProject + "-latest"
45 }
46 def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
47 if (isJobExists(testJob)) {
48 common.infoMsg("Test job ${testJob} found, running")
49 def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")
50 build job: testJob, parameters: [
51 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
52 [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
53 ]
54 giveVerify = true
55 } else {
56 common.infoMsg("Test job ${testJob} not found")
57 }
58 } else {
59 common.errorMsg("Change don't have a CodeReview, skipping gate")
60 }
61 } else {
62 common.infoMsg("Test job skipped")
63 }
64 }
65 stage("submit review") {
66 if (gerritChange.status == "MERGED") {
67 common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
68 } else if (doSubmit) {
69 if (giveVerify) {
70 common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
71 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))
72 } else {
73 ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
74 }
75 common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
76 }
77 }
78 } catch (Throwable e) {
79 // If there was an error or exception thrown, the build failed
80 currentBuild.result = "FAILURE"
81 currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
82 throw e
83 }
84 }
85}