pipeline {

    environment {
        TOX_PARALLEL_NO_SPINNER = "1"
        PY_COLORS = "0"
    }

    agent any

    stages {
        stage('Prepare') {
            steps {
                sh 'poetry install --remove-untracked --no-root'
            }
        }
        stage('Tox') {
            matrix {
                axes {
                    axis {
                        name 'PYTHON_VERSION'
                        values 'py38', 'py39', 'py310', 'py311'
                    }
                }
                stages {
                    stage('Tox Test') {
                        options {
                            lock('single-tox-build')
                        }
                        steps {
                            sh 'poetry run tox -r -e ${PYTHON_VERSION}'
                            xunit checksName: "Tests ${PYTHON_VERSION}", tools: [JUnit(excludesPattern: '', pattern: 'test-reports/*.xml', stopProcessingIfError: true)]
                            cobertura autoUpdateStability: false, coberturaReportFile: 'coverage.xml', conditionalCoverageTargets: '70, 0, 0', failUnhealthy: false, failUnstable: false, lineCoverageTargets: '80, 0, 0', maxNumberOfBuilds: 0, methodCoverageTargets: '80, 0, 0', onlyStable: false
                        }
                    }
                }
            }
        }
    }

    post {
        success {
            deleteDir()
        }
        always {
            withCredentials([string(credentialsId: 'jed-notification-email', variable: 'EMAIL')]) {
                mail to: '$EMAIL',
                     subject: "[${currentBuild.fullDisplayName}] ${currentBuild.currentResult}",
                     body: "Duration: ${currentBuild.durationString} / Jenkins URL: ${env.BUILD_URL}"
            }
        }
    }

}