def main() {
    stage("Checkout") {
            container("python") { sh("chown -R 1000:1000 ./") }

            checkout(changelog: false, poll: false, scm: [
                $class           : "GitSCM",
                branches         : [[name: "*/master"]],
                userRemoteConfigs: [[url: "https://github.com/tidbcloud/test-automation.git",
                                    credentialsId: "github-sre-bot"]],
                extensions       : [[$class: 'PruneStaleBranch'], [$class: 'CleanBeforeCheckout']],
            ])
        }

    stage("Initialize environment"){
            echo "start to update pip"
            container('python'){
                sh("""
                    pip install --upgrade pip
                    mkdir  ~/.pip/
                    touch  ~/.pip/pip.conf
                    echo '[global]\nindex-url=http://mirrors.aliyun.com/pypi/simple/\n[install]\ntrusted-host=mirrors.aliyun.com' > ~/.pip/pip.conf
                """)
                echo "start to install dependencies"
                sh("""
                    pip3 install -r requirements.txt
                    pip3 install pytest-html
                    pip3 install allure-pytest
                """)
            }
        }

    stage("Test") {
        container("python") {
            def projectDir = pwd()
            sh("""
                mkdir ./result/allure
            """)
            catchError(buildResult: 'FAILURE', stageResult: 'FAILURE'){
                echo "start to execute test"
                sh 'python main.py --env=${environment}'
            }
        }
    }
    stage("Allure") {
            container("java") {
                //def projectDir = "/home/jenkins/agent"
                def projectDir = pwd()
                catchError(buildResult: 'FAILURE', stageResult: 'FAILURE'){
                    sh("""
                        mkdir report
                        chmod -R a+rw *
                        export ALLURE_RESULTS_PATH=${projectDir}/report
                    """)
                }
                script {
                    allure([
                        includeProperties: false,
                        jdk: 'openjdk:1.8',
                        properties: [],
                        reportBuildPolicy: 'ALWAYS',
                        report: 'report/allure-report',
                        results: [[path:'result/allure-results']]
                        ])
                }
            }

        }
    
    stage("Report") {
        container("python") {
            def url = "https://ci.pingcap.net/job/QE/job/platform-prod-patrol-UI"
            sh("""
                python notify.py QA-UI-patrol ${environment} ${currentBuild.result} ${BUILD_NUMBER} ${params.runBy} Y ${url}/${BUILD_NUMBER}/allure
                """)
        }

    }
}

def run(label, image, Closure body) {
    podTemplate(name: label, label: label, instanceCap: 10,  
    nodeSelector: "kubernetes.io/arch=amd64",
    containers: [
        containerTemplate(name: 'python', image: image, alwaysPullImage: true, ttyEnabled: true, command: 'cat'),
        containerTemplate(name: 'java', image: 'bincent/openjdk:1.8', alwaysPullImage: true, ttyEnabled: true, command: 'cat'),
    ]) { node(label) { body() } }
}

catchError {
    run("platform-prod-patrol-UI-${BUILD_NUMBER}", 'joyzoursky/python-chromedriver') { main() }
}
