String GITHUB_REPOSITORY = 'python-abacus-common-logic'
String SLACK_NOTIFICATIONS_CHANNEL = '#abacus-devs'

pipeline {
    agent any

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        timestamps()
    }

    parameters {
        choice(name: 'VERSION_BUMP_LEVEL', choices: ['', 'patch', 'minor', 'major'], description: 'Creates a release candidate version of the bumped version type: <strong>{major}</strong>.<strong>{minor}</strong>.<strong>{patch}</strong>')
        string(name: 'SHARED_LIBRARIES_VERSION', defaultValue: 'master', description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag or Git revision.')
    }

    triggers {
        issueCommentTrigger('.*retest this please.*')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }
        stage('Unit Tests and Style Checks') {
            steps {
                withEcr{
                    sh 'make ci_unit_lint'
                }
            }
            post {
                always {
                    xunit tools: [JUnit(pattern: 'build/pyunit.xml', deleteOutputFiles: true, failIfNotNew: true, stopProcessingIfError: true)]
                    recordCoverage(tools: [[parser: 'COBERTURA', pattern: 'build/coverage.xml']], enabledForFailure: true, failOnError: true, sourceDirectories: [[path: "abacus_common_logic"]])
                }
                cleanup {
                    sh 'make ci_unit_lint_clean'
                }
            }
        }
        stage('Static Application Security Tests') {
            steps {
                sastTests()
            }
        }
        stage('Sonar Scan and Analysis') {
            when {
                branch 'master'
            }
            steps {
                sonarScan project: GITHUB_REPOSITORY, language: 'py'
            }
        }
        stage('Create a release') {
            when {
                allOf {
                    branch 'master'
                    expression { params.VERSION_BUMP_LEVEL != '' }
                }
            }
            steps {
                build job: 'publish-pypi-package-v2', parameters: [
                    [$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'GITREPO', value: GITHUB_REPOSITORY],
                    [$class: 'StringParameterValue', name: 'VERSION', value: params.VERSION_BUMP_LEVEL],
                    [$class: 'StringParameterValue', name: 'PYTHON_VERSION', value: '3.13']
                ]
            }
        }
        stage('Integration Tests') {
            when {
                allOf {
                    branch 'master'
                    expression { params.VERSION_BUMP_LEVEL != '' }
                }
            }
            steps {
                withEcr {
                        withCredentials([aws(accessKeyVariable: 'AWS_ACCESS_KEY_ID', credentialsId: 'cucumber-tests', secretKeyVariable: 'AWS_SECRET_ACCESS_KEY')]) {
                        sh 'make ci_test_integration'
                    }
                }
            }
            post {
                cleanup {
                    sh 'make ci_test_integration_clean'
                }
            }
        }
    }
    post {
        regression {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
        fixed {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
        cleanup {
            cleanWs()
        }
    }
}
