/**
 * Gerrit gating pipeline
 * CREDENTIALS_ID - Gerrit credentails ID
 * JOBS_NAMESPACE - Gerrit gating jobs namespace (mk, contrail, ...)
 *
 **/
import groovy.json.JsonOutput

def common = new com.mirantis.mk.Common()
def gerrit = new com.mirantis.mk.Gerrit()
def ssh = new com.mirantis.mk.Ssh()

slaveNode = env.SLAVE_NODE ?: 'docker'
giveVerify = false

@NonCPS
def isJobExists(jobName) {
    return Jenkins.instance.items.find { it -> it.name.equals(jobName) }
}

def callJobWithExtraVars(String jobName) {
    def gerritVars = JsonOutput.toJson(env.getEnvironment().findAll{ it.key.startsWith('GERRIT_') })
    testJob = build job: jobName, parameters: [
        [$class: 'TextParameterValue', name: 'EXTRA_VARIABLES_YAML', value: gerritVars]
    ]
    if (testJob.getResult() != 'SUCCESS') {
        error("Gate job ${testJob.getBuildUrl().toString()}  finished with ${testJob.getResult()} !")
    }
    giveVerify = true
}


timeout(time: 12, unit: 'HOURS') {
    node(slaveNode) {
        try {
            // test if change is not already merged
            ssh.prepareSshAgentKey(CREDENTIALS_ID)
            ssh.ensureKnownHosts(GERRIT_HOST)
            def gerritChange = gerrit.getGerritChange(GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, CREDENTIALS_ID, true)
            def doSubmit = false
            stage("test") {
                if (gerritChange.status != "MERGED" && !SKIP_TEST.equals("true")) {
                    // test max CodeReview
                    if (gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Code-Review", "+")) {
                        doSubmit = true
                        def gerritProjectArray = GERRIT_PROJECT.tokenize("/")
                        def gerritProject = gerritProjectArray[gerritProjectArray.size() - 1]
                        def jobsNamespace = JOBS_NAMESPACE
                        def plural_namespaces = ['salt-formulas', 'salt-models']
                        // remove plural s on the end of job namespace
                        if (JOBS_NAMESPACE in plural_namespaces) {
                            jobsNamespace = JOBS_NAMESPACE.substring(0, JOBS_NAMESPACE.length() - 1)
                        }
                        // salt-formulas tests have -latest on end of the name
                        if (JOBS_NAMESPACE.equals("salt-formulas")) {
                            gerritProject = gerritProject + "-latest"
                        }
                        def testJob = String.format("test-%s-%s", jobsNamespace, gerritProject)
                        if (env.GERRIT_PROJECT == 'mk/cookiecutter-templates') {
                            callJobWithExtraVars('test-mk-cookiecutter-templates')
                        } else if (env.GERRIT_PROJECT == 'salt-models/reclass-system') {
                            callJobWithExtraVars('test-salt-model-reclass-system')
                        } else {
                            if (isJobExists(testJob)) {
                                common.infoMsg("Test job ${testJob} found, running")
                                def patchsetVerified = gerrit.patchsetHasApproval(gerritChange.currentPatchSet, "Verified", "+")
                                build job: testJob, parameters: [
                                    [$class: 'StringParameterValue', name: 'DEFAULT_GIT_URL', value: "${GERRIT_SCHEME}://${GERRIT_NAME}@${GERRIT_HOST}:${GERRIT_PORT}/${GERRIT_PROJECT}"],
                                    [$class: 'StringParameterValue', name: 'DEFAULT_GIT_REF', value: GERRIT_REFSPEC]
                                ]
                                giveVerify = true
                            } else {
                                common.infoMsg("Test job ${testJob} not found")
                            }
                        }
                    } else {
                        common.errorMsg("Change don't have a CodeReview, skipping gate")
                    }
                } else {
                    common.infoMsg("Test job skipped")
                }
            }
            stage("submit review") {
                if (gerritChange.status == "MERGED") {
                    common.successMsg("Change ${GERRIT_CHANGE_NUMBER} is already merged, no need to gate them")
                } else if (doSubmit) {
                    if (giveVerify) {
                        common.warningMsg("Change ${GERRIT_CHANGE_NUMBER} don't have a Verified, but tests were successful, so adding Verified and submitting")
                        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))
                    } else {
                        ssh.agentSh(String.format("ssh -p 29418 %s@%s gerrit review --submit %s,%s", GERRIT_NAME, GERRIT_HOST, GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
                    }
                    common.infoMsg(String.format("Gerrit review %s,%s submitted", GERRIT_CHANGE_NUMBER, GERRIT_PATCHSET_NUMBER))
                }
            }
        } catch (Throwable e) {
            // If there was an error or exception thrown, the build failed
            currentBuild.result = "FAILURE"
            currentBuild.description = currentBuild.description ? e.message + " " + currentBuild.description : e.message
            throw e
        }
    }
}
