String GITHUB_REPOSITORY = 'python-content-utils'
String SLACK_NOTIFICATIONS_CHANNEL = '#content-review-team'

pipeline {
    agent any

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

    parameters {
        string(name: 'SHARED_LIBRARIES_VERSION', defaultValue: 'master', description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag, Git revision or PR ref (e.g. pull/PR_NUMBER/merge).')
        choice(
            name: 'VERSION',
            choices: ['', 'patch', 'minor', 'major', 'release', 'rc_version'],
            description: 'Creates a release candidate version of the bumped version type: {major}.{minor}.{patch}'
        )
    }

    environment {
        GRAPHQL_GATEWAY_URL = 'https://qa-graphql-router.theorchard.io/graphql'
        OWS_PRODUCT_REVIEW_RESET_URL = 'https://qa-ows-product-review.theorchard.io/qa-only-reset-test-product/'
    }
    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: 'pyunit.xml', deleteOutputFiles: true, failIfNotNew: true, stopProcessingIfError: true)]
                    recordCoverage(tools: [[parser: 'COBERTURA', pattern: 'coverage.xml']], enabledForFailure: true, failOnError: true, sourceDirectories: [[path: "content_utils"]])
                }
                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 != '' }
                }
            }
            steps {
                build job: 'publish-pypi-package-v2', parameters: [
                    [$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'GITREPO', value: 'python-content-utils'],
                    [$class: 'StringParameterValue', name: 'VERSION', value: params.VERSION],
                    [$class: 'StringParameterValue', name: 'PYTHON_VERSION', value: '3.13']
                ]
            }
        }
        stage('Integration Tests') {
            when {
                allOf {
                    branch 'master'
                    expression { params.VERSION != '' }
                }
            }
            steps {
                withEcr {
                    sh 'make ci_test_integration'
                }
            }
            post {
                cleanup {
                    sh 'make ci_unit_lint_clean'
                }
            }
        }
    }
    post {
        regression {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        fixed {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        cleanup {
            cleanWs()
        }
    }
}
