import hudson.model.Result
import hudson.model.*;
import jenkins.model.CauseOfInterruption
node {
}
def sync_source() {
    sh '''
        hostname
        date
        '''
    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 {
            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
    '''
    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 {
            sh '''
                cd ${WK}
                git checkout develop
            '''
        }
    }
    sh '''
        cd ${WK}
        git pull >/dev/null
        git clean -dfx
    '''
    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

                if [ ! -d src/connector/python/.github ]; then
                    rm -rf src/connector/python/* || :
                    rm -rf src/connector/python/.* || :
                    git clone --depth 1 https://github.com/taosdata/taos-connector-python src/connector/python || echo "failed to clone python connector"
                else
                    cd src/connector/python || echo "src/connector/python not exist"
                    git pull || :
                    cd ${WKC}
                fi
            '''
        } 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

                if [ ! -d community/src/connector/python/.github ]; then
                    rm -rf community/src/connector/python/* || :
                    rm -rf community/src/connector/python/.* || :
                    git clone --depth 1 https://github.com/taosdata/taos-connector-python community/src/connector/python || echo "failed to clone python connector"
                else
                    cd community/src/connector/python || echo "community/src/connector/python not exist"
                    git pull || :
                    cd ${WK}
                fi
            '''
        } 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 {label " dispatcher "}
    options { skipDefaultCheckout() }
    environment{
        WK = '/var/data/jenkins/workspace/TDinternal'
        WKC = '/var/data/jenkins/workspace/TDinternal/community'
        LOGDIR = '/var/data/jenkins/workspace/log'
    }
    stages {
    }
}
