pipeline {
    agent {
        label "rmc-vosl423-x8664-build01"
    }

    parameters {
        string(name: 'tox_args', defaultValue: '-v', description: 'Arguments passed to tox besides the environment name', )
        string(name: 'pytest_args', defaultValue: '-vx -p no:timeout -m "(core or gui or share_elements) and not unstable and not user_input"', description: 'Arguments passed to pytest')
    }

    environment {
        PATH = "/opt/python/osl42-x86_64/python2/default/bin:/opt/python/osl42-x86_64/python3/default/bin:$PATH"
        LD_LIBRARY_PATH = "/opt/python/osl42-x86_64/python2/default/lib:/opt/python/osl42-x86_64/python3/default/lib:$LD_LIBRARY_PATH"
        TOX_LIMITED_SHEBANG = 1
        // Allows 1st build of a project to succeed, workaround for https://issues.jenkins-ci.org/browse/JENKINS-41929
        tox_args = "${params.tox_args}"
        pytest_args = "${params.pytest_args}"
    }

    stages {

        stage("Run tests") {
            // Run tests in parallel:
            // * wrapped in Xvfb for having an X server
            // * specify tox environment (py27, py36, coverage)
            // * run only stable tests
            // * collect pytest results in XML file
            parallel {
                stage('Test Python 2.7') {
                    steps {
                        timeout(time: 10, unit: 'MINUTES') {
                            wrap([$class: 'Xvfb', autoDisplayName: true, installationName: 'default', parallelBuild: true, screen: '1920x1200x24', timeout: 3]) {
                                sh "tox -e py27 $tox_args -- $pytest_args --junitxml $WORKSPACE/pytest_py27_results.xml"
                            }
                        }
                    }
                }

                stage('Test Python 3.6') {
                    steps {
                        timeout(time: 10, unit: 'MINUTES') {
                            wrap([$class: 'Xvfb', autoDisplayName: true, installationName: 'default', parallelBuild: true, screen: '1920x1200x24', timeout: 3]) {
                                sh "tox -e py36 $tox_args -- $pytest_args --junitxml $WORKSPACE/pytest_py36_results.xml"
                            }
                        }
                    }
                }

                stage('Test Python 2.7 Coverage') {
                    when {
                        anyOf { branch 'master'; branch 'develop' }
                    }
                    steps {
                        timeout(time: 10, unit: 'MINUTES') {
                            wrap([$class: 'Xvfb', autoDisplayName: true, installationName: 'default', parallelBuild: true, screen: '1920x1200x24', timeout: 3]) {
                                sh "tox -e coverage $tox_args -- $pytest_args > pytestout.txt"
                            }
                        }
                    }
                }
            }
        }

        stage('Build Documentation') {
            steps {
                wrap([$class: 'Xvfb', autoDisplayName: true, installationName: 'default', parallelBuild: true, screen: '1920x1200x24', timeout: 3]) {
                    sh 'tox -e docs'
                }
                // sphinx linkcheck only generates relative files, which cannot be found by recordIssues
                // therefore, we need to make the path absolute
                sh 'sed -i "s#.*#$WORKSPACE/doc/&#" build_doc/output.txt'
                // Find warnings:
                // * in the sphinx build process
                // * in the sphinx linkcheck results
                // * in the pytest warnings section
                recordIssues filters: [excludeCategory('.*rafcon.*'), excludeCategory('redirected with Found')], tools: [sphinxBuild(), groovyScript(parserId: 'sphinx-linkcheck', pattern: 'build_doc/output.txt'), groovyScript(parserId: 'pytest', pattern: 'pytestout.txt')]
            }
        }

    }
    post {
        failure {
            rocketSend channel: 'rafcon-jenkins', avatar: 'https://rmc-jenkins.robotic.dlr.de/jenkins/static/ff676c77/images/headshot.png', message: ":sob: <$BUILD_URL|Build $BUILD_NUMBER> on branch '$BRANCH_NAME' *failed*! Commit: <https://rmc-github.robotic.dlr.de/common/rafcon/commit/$GIT_COMMIT|$GIT_COMMIT> :sob:", rawMessage: true
        }
        unstable {
            junit '**/pytest_*_results.xml'
            rocketSend channel: 'rafcon-jenkins', avatar: 'https://rmc-jenkins.robotic.dlr.de/jenkins/static/ff676c77/images/headshot.png', message: ":sob: <$BUILD_URL|Build $BUILD_NUMBER> on branch '$BRANCH_NAME' *failed* (unstable)! Commit: <https://rmc-github.robotic.dlr.de/common/rafcon/commit/$GIT_COMMIT|$GIT_COMMIT> :sob:", rawMessage: true
        }
        success {
            junit '**/pytest_*_results.xml'
            cobertura autoUpdateHealth: false, autoUpdateStability: false,
                coberturaReportFile: 'pytest-cov_results.xml',
                conditionalCoverageTargets: '70, 0, 0', lineCoverageTargets: '80, 0, 0', methodCoverageTargets: '80, 0, 0',
                maxNumberOfBuilds: 0,
                enableNewApi: true,
                failUnhealthy: false, failUnstable: false, failNoReports: false,
                sourceEncoding: 'ASCII', zoomCoverageChart: false
            publishHTML([allowMissing: false, alwaysLinkToLastBuild: false, keepAll: false, reportDir: 'build_doc', reportFiles: 'index.html', reportName: 'Documentation', reportTitles: 'RAFCON documentation'])
            rocketSend channel: 'rafcon-jenkins', avatar: 'https://rmc-jenkins.robotic.dlr.de/jenkins/static/ff676c77/images/headshot.png', message: ":tada: <$BUILD_URL|Build $BUILD_NUMBER> on branch '$BRANCH_NAME' *succeeded*! Commit: <https://rmc-github.robotic.dlr.de/common/rafcon/commit/$GIT_COMMIT|$GIT_COMMIT> :tada:", rawMessage: true
            archiveArtifacts artifacts: '.tox/dist/*', fingerprint: true
        }
    }
}
