String GITHUB_REPOSITORY = 'python-kafka-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).')
    }

    stages {
        stage('Get Git Commit') {
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '577cbc72-7d9e-4eba-924c-ecbbe6de9805', url: 'git@github.com:theorchard/python-kafka-utils.git']]])
                    GIT_COMMIT = sh(returnStdout: true, script: 'git rev-parse HEAD').trim()
                }
            }
        }
        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: "kafka_utils"]])
                }
                cleanup {
                    sh 'make ci_unit_lint_clean'
                }
            }
        }
        stage('Static Application Security Tests') {
            steps {
                sastTests()
            }
        }
        stage('Sonar Scan and Analysis') {
            steps {
                sonarScan project: GITHUB_REPOSITORY, language: 'py'
            }
        }
        stage('Create a release') {
            steps {
                build job: 'publish-pypi-package-v2', parameters: [
                    [$class: 'StringParameterValue', name: 'GIT_COMMIT', value: GIT_COMMIT],
                    [$class: 'StringParameterValue', name: 'GITREPO', value: 'python-kafka-utils'],
                    [$class: 'StringParameterValue', name: 'VERSION', value: params.VERSION],
                    [$class: 'StringParameterValue', name: 'PYTHON_VERSION', value: '3.11']
                ]
            }
        }
        stage('Integration Tests') {
            agent { label 'can_reach_orchard_aws_dev' }
            steps {
                withEcr {
                    sh 'make ci_test_integration'
                }
            }
            post {
                cleanup {
                    sh 'make clean'
                }
            }
        }
    }
    post {
        regression {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        fixed {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        cleanup {
            cleanWs()
        }
    }
}
