@Library('eo-jenkins-lib@main') import eo.Utils

pipeline {
    agent any
    options { disableConcurrentBuilds() }
    environment {
        TMPDIR = "/tmp/${JOB_NAME.take(32)}-${BUILD_NUMBER}"
    }
    stages {
        stage('Unit Tests') {
            steps {
                sh "mkdir -p ${env.TMPDIR}"
                sh """
python3 -m venv ${env.TMPDIR}/venv/
${env.TMPDIR}/venv/bin/pip install tox
PGPORT=`python3 -c 'import struct; import socket; s=socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'` pg_virtualenv -o fsync=off ${env.TMPDIR}/venv/bin/tox -rv"""
                script {
                    if (currentBuild.getBuildCauses('hudson.triggers.TimerTrigger$TimerTriggerCause')) {
                        sh """
        PYTESTOPTIONS=--slow \
        PGPORT=`python3 -c 'import struct; import socket; s=socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'` pg_virtualenv -o fsync=off ${env.TMPDIR}/venv/bin/tox -rv"""
                        sh """
        PYTESTOPTIONS=--slow \
        PGPORT=`python3 -c 'import struct; import socket; s=socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'` pg_virtualenv -o fsync=off ${env.TMPDIR}/venv/bin/tox -rv -e rbac-py3-dj22"""
                        if (env.JOB_NAME == 'authentic' && env.GIT_BRANCH == 'origin/main') {
                                sh """
        PGPORT=`python3 -c 'import struct; import socket; s=socket.socket(); s.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack("ii", 1, 0)); s.bind(("", 0)); print(s.getsockname()[1]); s.close()'` pg_virtualenv -o fsync=off ${env.TMPDIR}/venv/bin/tox -v -e authentic-py3-dj22-drf312 -- tests/test_api.py"""
                        }
                    }
                }
            }
            post {
                always {
                    script {
                        utils = new Utils()
                        utils.publish_coverage('coverage.xml')
                        utils.publish_coverage_native('index.html')
                        utils.publish_pylint('pylint.out')
                    }
                    mergeJunitResults()
                }
            }
        }
        stage('Packaging') {
            steps {
                script {
                    if (env.JOB_NAME == 'authentic' && env.GIT_BRANCH == 'origin/main') {
                        sh 'sudo -H -u eobuilder /usr/local/bin/eobuilder -d buster,bullseye authentic'
                    } else if (env.GIT_BRANCH.startsWith('hotfix/')) {
                        sh "sudo -H -u eobuilder /usr/local/bin/eobuilder -d buster,bullseye --branch ${env.GIT_BRANCH} --hotfix authentic"
                    }
                }
            }
        }
    }
    post {
        always {
            script {
                utils = new Utils()
                utils.mail_notify(currentBuild, env, 'ci+jenkins-authentic@entrouvert.org')
                utils.jabber_notify(currentBuild, env, 'entrouvert-tech@im.libre-entreprise.com')
            }
        }
        cleanup {
            sh "rm -rf ${env.TMPDIR}"
            cleanWs()
        }
    }
}
