def main() {

    stage("Checkout") {
        container("golang") { sh("chown -R 1000:1000 ./") }
        checkout(changelog: false, poll: false, scm: [
            $class           : "GitSCM",
            branches         : [[name: "*/workload"]],
            userRemoteConfigs: [[url: "https://github.com/tidbcloud/qa-PlatformAPIAuto.git",
                                credentialsId: "github-sre-bot"]],
            extensions       : [[$class: 'PruneStaleBranch'], [$class: 'CleanBeforeCheckout']],
        ])
    }
    stage("Test") {
        container("golang") {
            def username = "api-test"
            def access_token = "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"
            def projectDir = pwd()
            catchError(buildResult: 'FAILURE', stageResult: 'FAILURE'){
                sh("""
                    pwd
                    go env GOPRIVATE=github.com/tidbcloud
                    git config --list
                    git config --global url.'https://${username}:${access_token}@github.com'.insteadOf 'https://github.com'
                    go env -w GOSUMDB=off
                    go get -u github.com/tidbcloud/qa-Gourd
                    cd ${projectDir}
                    mkdir report
                    export ALLURE_RESULTS_PATH=${projectDir}/report
                    cd sceneCases
                    go test -requestUrl ${params.Url} -orgId ${params.OrgId} -projectId ${params.ProjectId} -tenantEmail ${params.TenantEmail}   -provider ${params.Provider} -awsRegion ${params.AwsRegion} -gcpRegion ${params.GcpRegion}  -cpu ${params.Cpu}  -delCluster ${params.DelCluster}  --timeout=180000s
                """)
            }
            sh("""
                chmod -R o+xw ${projectDir}/report
            """)
            script {
                allure([
                    includeProperties: false,
                    jdk: 'DBaaS',
                    properties: [],
                    reportBuildPolicy: 'ALWAYS',
                    report: 'report/allure-report',
                    results: [[path:'report/allure-results']]
                    ])
            }
        }

    }
    stage("Result") {
            container("python") {
               def projectDir = "/home/jenkins/agent"
               def url = "http://cicd.tidbcloud.com/view/Test/job/run-workload-staging"
               catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE'){
               sh("""
                    cd ${projectDir}
                    ls
                     """)
               }

            }
        }
    /*stage("Image") {
        build(job: "image-build", parameters: [
            string(name: "CONTEXT_ARTIFACT", value: "$JOB_NAME:$BUILD_NUMBER:platfromCases/scene.tar.gz"),
            string(name: "DESTINATION", value: "hub.pingcap.net/qa/platfromCases"),
        ])
    }*/
}

def run(label, image, Closure body) {
    podTemplate(name: label, label: label, instanceCap: 10, containers: [
        containerTemplate(name: 'golang', image: image, alwaysPullImage: true, ttyEnabled: true, command: 'cat'),
        containerTemplate(name: 'selenium', image: 'elgalu/selenium', alwaysPullImage: true, ttyEnabled: true, command: 'cat'),
        containerTemplate(name: 'python', image: 'python:3.8', alwaysPullImage: true, ttyEnabled: true, command: 'cat'),
    ]) { node(label) { body() } }
}

catchError {
    run("run-workload-staging-${BUILD_NUMBER}", 'golang:1.16.0') { main() }
}
