String GITHUB_REPOSITORY = 'feed-status-monitor'
String SERVICE_NAME = 'monty-feed-status'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String QA_ACCOUNT_ID = '437795906767'
String QA_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String PROD_ACCOUNT_ID = '437795906767'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String SLACK_NOTIFICATIONS_CHANNEL = '#data-alerts'

List<String> VULNERABILITIES_TO_IGNORE = [
'CVE-2026-23949',
'CVE-2026-24049',
'CVE-2025-13836', //pkg:deb/debian/python3.11@3.11.2-6+deb12u1?arch=amd64
'CVE-2025-8194', //pkg:deb/debian/python3.11@3.11.2-6+deb12u1?arch=amd64
'CVE-2026-4224', //pkg:deb/debian/python3.11@3.11.2-6+deb12u1?arch=amd64
'CVE-2026-4519',
]

pipeline {
    agent any

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: false, description: 'Whether or not to deploy to prod.')
    }

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

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@master"
            }
        }
        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }
        stage('Validate Software Catalog Definition') {
            steps {
                datadogSoftwareCatalogValidate()
            }
        }
        stage('Unit Tests and Style Checks') {
            environment {
                // specifying unique project name to avoid collisions when multiple builds run on the same jenkins agent
                // project name must consist only of lowercase alphanumeric characters, hyphens,
                //   and underscores as well as start with a letter or number
                // will produce name like: jenkins-theorchard-feed-status-monitor-pr-4861-3
                COMPOSE_PROJECT_NAME="${env.BUILD_TAG.replaceAll('[^a-zA-Z0-9-]', '').toLowerCase()}"
            }
            steps {
                withEcr {
                    sh 'make docker_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)
                //}
                cleanup {
                    sh 'touch .env'
                    sh 'make docker_unit_lint_clean'
                }
            }
        }
// It was agreed by The Team to mute it as we are targeting to transition monitoring from Monty to Datadog
//    and upgrading vuetify to major version will take too much resources
//         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 {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                dockerToEcr awsRegions: AWS_REGIONS, ecrAccountId: ECR_ACCOUNT_ID, imageName: SERVICE_NAME, imageTag: env.GIT_COMMIT
            }
        }
        stage('Scan Docker Image') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                dockerScan awsRegion: AWS_REGIONS[0],
                    ecrAccountId: ECR_ACCOUNT_ID,
                    imageName: SERVICE_NAME,
                    imageTag: env.GIT_COMMIT,
                    vulnerabilitiesToIgnore: VULNERABILITIES_TO_IGNORE
            }
        }
        stage('Deploy to QA') {
            when {
                branch 'master'
            }
            steps {
                fargateDeploy environment: 'qa', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: SERVICE_NAME,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
            }
        }
        stage('Deploy to Prod') {
             when {
                 allOf {
                     branch 'master'
                     expression { params.DEPLOY_TO_PROD }
                 }
             }
             steps {
                 fargateDeploy environment: 'prod', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: SERVICE_NAME,
                     ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: PROD_ACCOUNT_ID, awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
                 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()
        }
    }
}
