pipeline {
    agent { label "windows && amd64" }

    environment {
        BUILD_OPTIONS = ''
    }

    options {
        buildDiscarder(logRotator(numToKeepStr: '135', daysToKeepStr: '21'))
        gitLabConnection('GitLabConnectionJenkins')
        timestamps() // Add timestamps to all console output
    }

    stages {
        stage('clean previous runs and update gitlab commit status'){
            steps{
                deleteDir()
                updateGitlabCommitStatus(name: 'Build windows', state: 'running')
            }
        }

        stage('Get build parameters'){
            parallel{
                stage('Get build options'){
                    when {
                        allOf {
                            expression { env.gitlabTriggerPhrase != null }
                            expression { env.gitlabTriggerPhrase.contains('BUILD_OPTIONS') }
                        }
                    }
                    steps {
                        script{
                            env.BUILD_OPTIONS = sh(script: 'echo "$gitlabTriggerPhrase" | grep BUILD_OPTIONS | awk -F "BUILD_OPTIONS="  \'{print \$2}\' | cut -d"\"" -f2', returnStdout: true).trim()
                            println BUILD_OPTIONS
                        }
                    }
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the additional build parameters :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }

                stage('Get SDK branch'){
                    steps {
                        script{
                            env.SDK_BRANCH = sh(script: 'echo "$gitlabMergeRequestDescription" | grep SDK_SUBMODULE_TEST | awk -F "SDK_SUBMODULE_TEST="  \'{print \$2}\' | cut -d" " -f1', returnStdout: true).trim()
                            if (SDK_BRANCH == ""){
                                echo "SDK_BRANCH was not found on description so develop will be used by default"
                                env.SDK_BRANCH = "develop"
                            }
                            println SDK_BRANCH
                        }
                    }
                    post{
                        always {
                            script{
                                if (currentBuild.currentResult == 'FAILURE'){
                                    addGitLabMRComment(comment: ":red_circle: ${env.JOB_NAME} FAILURE when getting the SDK branch :worried:<br/>Build results: [Jenkins [${env.JOB_NAME} ${env.BUILD_DISPLAY_NAME}]](${env.RUN_DISPLAY_URL})<br/>Commit: ${env.GIT_COMMIT}" )
                                }
                            }
                        }
                    }
                }
            }
        }
        stage('Checkout sources'){
            parallel{
                stage('Checkout MEGAcmd with prebuildmerge'){
                    steps{
                        checkout([
                            $class: 'GitSCM',
                            branches: [[name: "origin/${env.gitlabSourceBranch}"]],
                            userRemoteConfigs: [[ url: "${env.GIT_URL_MEGACMD}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]],
                            extensions: [
                                [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"],
                                [$class: 'PreBuildMerge', options: [fastForwardMode: 'FF', mergeRemote: "origin", mergeStrategy: 'DEFAULT', mergeTarget: "${env.gitlabTargetBranch}"]]
                            ]
                        ])
                        script{
                            megacmd_sources_workspace = WORKSPACE
                        }
                    }
                }
                stage('Checkout VCPKG'){
                    steps{
                        dir('vcpkg'){
                            checkout([
                            $class: 'GitSCM',
                            branches: [[name: "origin/master"]],
                            userRemoteConfigs: [[ url: "https://github.com/microsoft/vcpkg" ]],
                            extensions: [
                                [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"],
                                [$class: "CloneOption", noTags: false, reference: '']
                            ]
                            ])
                        }
                    }
                }
                stage('Checkout SDK'){
                    steps{
                        dir('sdk'){
                            checkout([
                                $class: 'GitSCM',
                                branches: [[name: "origin/${SDK_BRANCH}"]],
                                userRemoteConfigs: [[ url: "${env.GIT_URL_SDK}", credentialsId: "12492eb8-0278-4402-98f0-4412abfb65c1" ]],
                                extensions: [
                                    [$class: "UserIdentity",name: "jenkins", email: "jenkins@jenkins"],
                                    [$class: "CloneOption", depth: 1, shallow: true, noTags: false, reference: '']
                                ]
                            ])
                        }
                    }
                }
            }
        }

        stage('Build MEGAcmd'){
            environment{
                VCPKGPATH  = "${megacmd_sources_workspace}\\..\\vcpkg"
                BUILD_DIR = "cmdbuild"
                _MSPDBSRV_ENDPOINT_ = "${BUILD_TAG}"
                TMP       = "${megacmd_sources_workspace}\\tmp"
                TEMP      = "${megacmd_sources_workspace}\\tmp"
                TMPDIR    = "${megacmd_sources_workspace}\\tmp"
            }
            options{
                timeout(time: 3, unit: 'HOURS')
            }
            steps{
                sh "rm -rf ${BUILD_DIR}; mkdir ${BUILD_DIR}"
                sh "mkdir tmp"
                sh "cmake -DCMAKE_VERBOSE_MAKEFILE=1 -DENABLE_MEGACMD_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DVCPKG_ROOT='${VCPKGPATH}' -DCMAKE_INSTALL_PREFIX=cmdinstall -S '${megacmd_sources_workspace}' -B ${BUILD_DIR} '${env.BUILD_OPTIONS}'"
                sh "cmake --build ${BUILD_DIR} -j 2"
                sh "cmake --install ${BUILD_DIR} --config Debug"
            }
        }
        stage('Run unit tests') {
            environment {
                INSTALL_DIR = "${megacmd_sources_workspace}\\cmdbuild\\Debug"
                MEGACMD_PIPE_SUFFIX = "${env.BUILD_ID}"
            }
            options {
                timeout(time: 3, unit: 'HOURS')
            }
            steps {
                dir('unit-test-dir') {
                    echo "Running tests"
                    bat "${INSTALL_DIR}\\mega-cmd-tests-unit.exe --gtest_shuffle --gtest_output=xml:${megacmd_sources_workspace}\\unit-test-dir\\mega-cmd-tests.xml"
                }
            }
            post {
                always {
                    archiveArtifacts artifacts: "cmdbuild\\Debug\\.megaCmd\\megacmdserver.log*", allowEmptyArchive: true
                    junit "unit-test-dir\\mega-cmd-tests.xml"
                    bat "rmdir ${INSTALL_DIR}\\.megaCmd /s /q"
                }
            }
        }
        stage('Run integration tests') {
            environment {
                INSTALL_DIR = "${megacmd_sources_workspace}\\cmdbuild\\Debug"
                MEGACMD_PIPE_SUFFIX = "${env.BUILD_ID}"
                MEGACMD_TEST_USER = ''
                MEGACMD_TEST_PASS = credentials('MEGACMD_TESTS_PASSWORD')
            }
            options {
                timeout(time: 3, unit: 'HOURS')
            }
            steps {
                lock(label: 'testing_accounts_megacmd', variable: 'MEGACMD_TEST_USER', quantity: 1, resource: null) {
                    dir('integration-test-dir') {
                        echo "Running tests"
                        script {
                            def status = bat script: "${INSTALL_DIR}\\mega-cmd-tests-integration.exe --gtest_shuffle --gtest_output=xml:${megacmd_sources_workspace}\\integration-test-dir\\mega-cmd-tests.xml", returnStatus: true

                            // There is an unresolved issue on Windows that results in an incorrect status code being returned
                            // from the process above; so we use a file to get the actual value
                            if (fileExists("exit_code.txt")) {
                                def exitCode = readFile("exit_code.txt").trim().toInteger()
                                if (status != exitCode) {
                                    echo "Incorrect status code detected (expected: ${exitCode} actual: ${status})"
                                }
                                if (exitCode != 0) {
                                    error "Integration tests failed with exit code ${exitCode}"
                                }
                            } else {
                                unstable "Exit code file does not exist"
                            }
                        }
                    }
                }
            }
            post {
                always {
                    archiveArtifacts "cmdbuild\\Debug\\.megaCmd\\megacmdserver.log*"
                    junit "integration-test-dir\\mega-cmd-tests.xml"
                    bat "rmdir ${INSTALL_DIR}\\.megaCmd /s /q"
                }
            }
        }
    }

    post{
        always{
            script{
                if (currentBuild.currentResult == 'SUCCESS'){
                    addGitLabMRComment(comment: ":white_check_mark: ${currentBuild.projectName} <b>Windows</b> SUCCEEDED :muscle:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build windows', state: 'success')
                }
                if (currentBuild.currentResult == 'FAILURE'){
                    addGitLabMRComment(comment: ":red_circle: ${currentBuild.projectName} <b>Windows</b> FAILURE  :worried:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build windows', state: 'failed')
                }
                if (currentBuild.currentResult == 'ABORTED'){
                    addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} <b>Windows</b> ABORTED  :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build windows', state: 'canceled')
                }
                if (currentBuild.currentResult == 'UNSTABLE'){
                    addGitLabMRComment(comment: ":interrobang: ${currentBuild.projectName} <b>Windows</b> UNSTABLE  :confused:<br/>Build results: [Jenkins [${currentBuild.displayName}]](${currentBuild.absoluteUrl})<br/>Commit: ${env.GIT_COMMIT}" )
                    updateGitlabCommitStatus(name: 'Build windows', state: 'failed')
                }
            }
            deleteDir()
        }
    }
}
