String GITHUB_REPOSITORY = 'python-datadog-utils'
String ECR_REPO = 'datadog-tools'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#devops-internal-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).')
    }

    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: "{{cookiecutter.app_name}}"]])
                }
                cleanup {
                    sh 'make ci_unit_lint_clean'
                }
            }
        }
        stage('Integration Tests') {
            steps {
                withEcr {
                    withAWS(roleAccount: '086679231553', role: 'shared-datadog-software-catalog-role', roleSessionName: env.BUILD_TAG, useNode: true) {
                        sh 'make ci_test_integration'
                    }
                }
            }
        }
        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') {
            steps {
                dockerToEcr awsRegions: AWS_REGIONS,
                    ecrAccountId: ECR_ACCOUNT_ID,
                    imageName: ECR_REPO,
                    imageTag: env.GIT_COMMIT,
                    pushLatest: false,
                    dockerBuildTarget: 'deploy'
            }
        }
        stage('Scan Docker Image') {
            steps {
                dockerScan awsRegion: AWS_REGIONS[0],
                    ecrAccountId: ECR_ACCOUNT_ID,
                    imageName: ECR_REPO,
                    imageTag: env.GIT_COMMIT,
                    slackNotificationChannel: (env.BRANCH_NAME == 'master' ? SLACK_NOTIFICATIONS_CHANNEL : null),
                    vulnerabilitiesToIgnore: [
                        // These are all vulnerabilities in Go/Go libraries used by syft and require that to be updated upstream
                        'CVE-2026-39821',
                        'CVE-2026-39832',
                        'CVE-2026-39833',
                        'CVE-2026-39834',
                        'CVE-2026-42508',
                        'CVE-2026-46595',
                        'CVE-2026-33811',
                        'CVE-2026-33814',
                        'CVE-2026-39820',
                        'CVE-2026-39836',
                        'CVE-2026-42499',
                    ]
            }
        }
        stage('Promote to Latest') {
            when {
                branch 'master'
            }
            steps {
                retagEcrImage awsRegions: AWS_REGIONS,
                    ecrAccountId: ECR_ACCOUNT_ID,
                    imageName: ECR_REPO,
                    imageTag: env.GIT_COMMIT,
                    newTag: 'latest'
            }
        }
    }

    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()
        }
    }
}
