import hudson.model.Result
import hudson.model.*;
import jenkins.model.CauseOfInterruption
node {
}
def sync_source() {
    sh '''
        hostname
        date
        env
    '''
    sh '''
        cd ${WKC}
        [ -f src/connector/grafanaplugin/README.md ] && rm -f src/connector/grafanaplugin/README.md > /dev/null || echo "failed to remove grafanaplugin README.md"
        git reset --hard
        git fetch
        cd ${WK}
        git reset --hard
        git fetch
    '''
    script {
        if (env.CHANGE_TARGET == 'master') {
            sh '''
                cd ${WKC}
                git clean -fxd
                git checkout master
            '''
        } else if (env.CHANGE_TARGET == '2.0') {
            sh '''
                cd ${WKC}
                git clean -fxd
                git checkout 2.0
            '''
        } else if (env.CHANGE_TARGET == '2.4') {
            sh '''
                cd ${WKC}
                git clean -fxd
                git checkout 2.4
            '''
        } else if (env.CHANGE_TARGET == '2.6') {
            sh '''
                cd ${WKC}
                git clean -fxd
                git checkout 2.6
            '''
        } else {
            sh '''
                cd ${WKC}
                git clean -fxd
                git checkout develop
            '''
        }
    }
    sh '''
        export TZ=Asia/Harbin
        cd ${WKC}
        git remote prune origin
        [ -f src/connector/grafanaplugin/README.md ] && rm -f src/connector/grafanaplugin/README.md > /dev/null || echo "failed to remove grafanaplugin README.md"
        git pull >/dev/null
        git clean -dfx
        git log -5
    '''
    script {
        if (env.CHANGE_TARGET == 'master') {
            sh '''
                cd ${WK}
                git checkout master
            '''
        } else if (env.CHANGE_TARGET == '2.0') {
            sh '''
                cd ${WK}
                git checkout 2.0
            '''
        } else if (env.CHANGE_TARGET == '2.4') {
            sh '''
                cd ${WK}
                git checkout 2.4
            '''
        } else if (env.CHANGE_TARGET == '2.6') {
            sh '''
                cd ${WK}
                git checkout 2.6
            '''
        } else {
            sh '''
                cd ${WK}
                git checkout develop
            '''
        }
    }
    sh '''
        cd ${WK}
        git pull >/dev/null
        git clean -dfx
        git log -5
    '''
    script {
        if (env.CHANGE_URL =~ /\/TDengine\//) {
            sh '''
                echo "match /TDengine/ repository"
                cd ${WKC}
                git fetch origin +refs/pull/${CHANGE_ID}/merge
                git checkout -qf FETCH_HEAD
                git log -5
            '''
            sh '''
                cd ${WKC}
                rm -rf src/connector/python
                mkdir -p src/connector/python
                git clone --depth 1 https://github.com/taosdata/taos-connector-python src/connector/python || echo "failed to clone python connector"
            '''
        } else if (env.CHANGE_URL =~ /\/TDinternal\//) {
            sh '''
                echo "match /TDinternal/ repository"
                cd ${WK}
                git fetch origin +refs/pull/${CHANGE_ID}/merge
                git checkout -qf FETCH_HEAD
                git log -5
            '''
            sh '''
                cd ${WKC}
                rm -rf src/connector/python
                mkdir -p src/connector/python
                git clone --depth 1 https://github.com/taosdata/taos-connector-python src/connector/python || echo "failed to clone python connector"
            '''
        } else {
            sh '''
                echo "unmatched reposiotry ${CHANGE_URL}"
            '''
        }
    }
    sh '''
        cd ${WKC}
        git submodule update --init --recursive
    '''
}
def pre_test() {
    sync_source()
    sh '''
        cd ${WK}
        mkdir -p debug
        cd debug
        go env -w GOPROXY=https://goproxy.cn,direct
        go env -w GO111MODULE=on
        cmake .. -DBUILD_HTTP=false -DBUILD_TOOLS=true > /dev/null
        make -j8 >/dev/null
    '''
    return 1
}
def pre_test_mac() {
    sync_source()
    sh '''
        cd ${WK}
        mkdir -p debug
        cd debug
        go env -w GOPROXY=https://goproxy.cn,direct
        go env -w GO111MODULE=on
        cmake .. -DBUILD_TOOLS=false > /dev/null
        make -j8 >/dev/null
    '''
    return 1
}
pipeline {
    agent none
    options { skipDefaultCheckout() }
    environment{
        WK = '/var/data/jenkins/workspace/TDinternal'
        WKC = '/var/data/jenkins/workspace/TDinternal/community'
        LOGDIR = '/var/data/jenkins/workspace/log'
    }
    stages {
        stage('run test') {
            options { skipDefaultCheckout() }
            when {
                allOf {
                    changeRequest()
                    not { expression { env.CHANGE_BRANCH =~ /docs\// }}
                }
            }
            parallel {
                stage ('build arm64') {
                    agent {label " worker07_arm64 || worker09_arm64 "}
                    steps {
                        timeout(time: 20, unit: 'MINUTES') {
                            pre_test()
                            script {
                                sh '''
                                    echo "arm64 build done"
                                    date
                                '''
                            }
                        }
                    }
                }
                stage ('build Mac') {
                    agent {label " Mac_catalina "}
                    steps {
                        timeout(time: 20, unit: 'MINUTES') {
                            pre_test_mac()
                            script {
                                sh '''
                                    echo "Mac build done"
                                    date
                                '''
                            }
                        }
                    }
                }
                stage('run cases') {
                    agent {label " worker01 || worker02 "}
                    steps {
                        sh '''
                            date
                            pwd
                            hostname
                        '''
                        timeout(time: 15, unit: 'MINUTES') {
                            pre_test()
                            script {
                                sh '''
                                    echo "Linux build done"
                                    date
                                '''
                            }
                        }
                        catchError(buildResult: 'FAILURE', stageResult: 'FAILURE') {
                            timeout(time: 25, unit: 'MINUTES') {
                                sh '''
                                    date
                                    cd ${WKC}/tests/parallel_test
                                    time ./run.sh -m /home/m.json -t cases.task -l ${LOGDIR} -b ${BRANCH_NAME}
                                    date
                                    hostname
                                '''
                            }
                        }
                    }
                }
            }    
        }
    }
    post {
        success {
            emailext (
                subject: "PR-result: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' SUCCESS",
                body: """<!DOCTYPE html>
                    <html>
                        <head>
                            <meta charset="UTF-8">
                        </head>
                        <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                            <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 16pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                                <tr>
                                    <td>
                                        <br/>
                                        <b><font color="#0B610B"><font size="6">构建信息</font></font></b>
                                        <hr size="2" width="100%" align="center" />
                                     </td>
                                </tr>
                                <tr>
                                    <td>
                                        <ul>
                                            <div style="font-size:18px">
                                                <li>构建名称>>分支：${env.BRANCH_NAME}</li>
                                                <li>构建结果：<span style="color:green"> Successful </span></li>
                                                <li>构建编号：${BUILD_NUMBER}</li>
                                                <li>触发用户：${env.CHANGE_AUTHOR}</li>
                                                <li>提交信息：${env.CHANGE_TITLE}</li>
                                                <li>构建地址：<a href=${BUILD_URL}>${BUILD_URL}</a></li>
                                                <li>构建日志：<a href=${BUILD_URL}console>${BUILD_URL}console</a></li>
                                            </div>
                                        </ul>
                                    </td>
                                </tr>
                            </table>
                        </body>
                    </html>""",
                to: "${env.CHANGE_AUTHOR_EMAIL}",
                from: "support@taosdata.com"
            )
        }
        failure {
            emailext (
                subject: "PR-result: Job '${env.JOB_NAME} [${env.BUILD_NUMBER}]' FAIL",
                body: """<!DOCTYPE html>
                    <html>
                        <head>
                            <meta charset="UTF-8">
                        </head>
                        <body leftmargin="8" marginwidth="0" topmargin="8" marginheight="4" offset="0">
                            <table width="95%" cellpadding="0" cellspacing="0" style="font-size: 16pt; font-family: Tahoma, Arial, Helvetica, sans-serif">
                                <tr>
                                    <td>
                                        <br/>
                                        <b><font color="#0B610B"><font size="6">构建信息</font></font></b>
                                        <hr size="2" width="100%" align="center" />
                                    </td>
                                </tr>
                                <tr>
                                    <td>
                                        <ul>
                                            <div style="font-size:18px">
                                                <li>构建名称>>分支：${env.BRANCH_NAME}</li>
                                                <li>构建结果：<span style="color:red"> Failure </span></li>
                                                <li>构建编号：${BUILD_NUMBER}</li>
                                                <li>触发用户：${env.CHANGE_AUTHOR}</li>
                                                <li>提交信息：${env.CHANGE_TITLE}</li>
                                                <li>构建地址：<a href=${BUILD_URL}>${BUILD_URL}</a></li>
                                                <li>构建日志：<a href=${BUILD_URL}console>${BUILD_URL}console</a></li>
                                            </div>
                                        </ul>
                                    </td>
                                </tr>
                            </table>
                        </body>
                    </html>""",
                to: "${env.CHANGE_AUTHOR_EMAIL}",
                from: "support@taosdata.com"
            )
        }
    }
}
