#!groovy

def prepareDocker () {
    def img
    stage('Prepare environment') {
        dir('.imagebuild') {
            def uid = sh returnStdout: true, script: 'id -u'
            writeFile file: 'Dockerfile', text: """
            FROM python:3.8-buster
            RUN pip install pytest-cov PySocks && \\
            useradd -m -u ${uid.trim()} jenkins
            """
            img = docker.build('local:test-all-comics')
        }
    }
    return img
}

def runTests() {
    stage ('Install lib') {
        sh "pip install --user -e .[css,dev]"
    }
    stage ('Run tests') {
        withCredentials([string(credentialsId: 'proxymap', variable: 'PROXYMAP')]) {
            sh '''
                TESTALL=1 py.test -v --cov=dosagelib --cov-report xml \
                     --alluredir=allure-data \
                     --tb=short -n10 --junitxml=junit.xml \
                     tests/modules/check_comics.py || true
            '''
        }
    }
    stage('Report') {
        junit 'junit.xml'
        publishCoverage calculateDiffForChangeRequests: true,
            sourceFileResolver: sourceFiles('STORE_LAST_BUILD'),
            adapters: [
                coberturaAdapter('coverage.xml')
            ]
    }
}

ansiColor('xterm') {
    node {
        def img = prepareDocker()

        stage('Checkout') {
            checkout scm
        }

        img.inside {
            runTests()
        }

        stage('Allure Report') {
            copyArtifacts filter: 'allure-history.zip', optional: true, projectName: JOB_NAME, selector: lastWithArtifacts()
            if (fileExists('allure-history.zip')) {
                unzip dir: 'allure-data', quiet: true, zipFile: 'allure-history.zip'
                sh 'rm -f allure-history.zip'
            }
            sh 'docker run --rm -v $PWD:/work -u $(id -u) tobix/allure-cli generate allure-data'
            zip archive: true, dir: 'allure-report', glob: 'history/**', zipFile: 'allure-history.zip'
            publishHTML reportDir: 'allure-report', reportFiles: 'index.html', reportName: 'Allure Report'
        }
    }
}