String GITHUB_REPOSITORY = 'python-vector-utils'
String SLACK_NOTIFICATIONS_CHANNEL = '#distro-build-alerts'

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('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }
        stage('Validate Software Catalog Definitions') {
            steps {
                datadogSoftwareCatalogValidate()
            }
        }
        stage('Unit Tests and Style Checks') {
            steps {
                withEcr{
                    sh 'mkdir -p output && chmod 777 output && docker compose run --rm --build lint-and-test'
                }
            }
            post {
                always {
                    xunit(
                        tools: [
                            JUnit(
                                pattern: 'output/pyunit.xml',
                                deleteOutputFiles: true,
                                failIfNotNew: true,
                                stopProcessingIfError: true
                            )
                        ]
                    )
                    recordCoverage(
                        tools: [
                            [
                                parser: 'COBERTURA',
                                pattern: 'output/coverage.xml'
                            ]
                        ],
                        enabledForFailure: true,
                        failOnError: true,
                        sourceDirectories: [[path: "vector_utils"]]
                    )

                }
                cleanup {
                    sh 'docker compose down --remove-orphans'
                }
            }
        }
        stage('Static Application Security Tests') {
            steps {
                sastTests()
            }
        }
        stage('Sonar Scan and Analysis') {
            when {
                branch 'master'
            }
            steps {
                sonarScan project: GITHUB_REPOSITORY, language: 'py'
            }
        }
        stage('Publish Software Catalog Definition') {
            when {
                branch 'master'
            }
            steps {
                datadogSoftwareCatalogPublish()
            }
        }
    }
    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()
        }
    }
}
