String GITHUB_REPOSITORY = 'ows-product'
String ECR_ACCOUNT_ID = '437795906767'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#content-creation-mgmt-alerts'
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 BUILD_ROLE = 'qa-ows-product-integration-test-role'
// CVE-2026-33164: memory-safety flaw in libde265 (HEVC decoder) pulled in
// transitively via the parent image. This service does no image/video decoding,
// so the vulnerable code path is unreachable. No fixed Debian package available;
// remove this entry once bookworm ships a patched libde265.
List<String> VULNERABILITIES_TO_IGNORE = ['CVE-2026-33164']

pipeline {
    agent any

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: true, description: 'Whether or not to deploy to prod.')
        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('Build Test and Scan') {
            parallel {
                stage('Validate Software Catalog Definition') {
                    steps {
                        datadogSoftwareCatalogValidate()
                    }
                }
                stage('Unit Tests and Style Checks') {
                    environment {
                        COMPOSE_PROJECT_NAME = "${env.BUILD_TAG.toLowerCase()}"
                    }
                    steps {
                        withEcr {
                            sh 'make docker_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: "product"]])
                        }
                    }
                }
                stage('Static Application Security Tests') {
                    steps {
                        sastTests()
                    }
                }
                stage('Sonar Scan and Analysis') {
                    when {
                        branch 'master'
                    }
                    steps {
                        sonarScan project: GITHUB_REPOSITORY, language: 'py'
                    }
                }
                stage('Create and Scan a Release') {
                    steps {
                        dockerToEcr awsRegions: AWS_REGIONS, ecrAccountId: ECR_ACCOUNT_ID, imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT,
                            dockerBuildTarget: 'deploy'
                        dockerScan awsRegion: AWS_REGIONS[0], ecrAccountId: ECR_ACCOUNT_ID, imageName: GITHUB_REPOSITORY, 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: GITHUB_REPOSITORY,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
            }
        }
        stage('Integration Tests') {
            when {
                branch 'master'
            }
            environment {
                BASE_QA_OA_URL = 'https://oa.qaorch.com/grass'
                QA_GRASS_HOST = 'https://api-dev.theorchard.io/auth/session'
                QA_DB_HOST = 'qadb01.qaorch.com'
                QA_DB_USER = 'automation_qa'
                BASE_QA_URL = 'https://qa-ows-features.theorchard.io'
                BASE_QA_GRASS_URL = 'https://qa-ows-grass.theorchard.io'
                QA_DB_DATABASE = 'art_relations'
            }
            steps {
                withEcr {
                    withSecrets(awsAccountId: QA_ACCOUNT_ID, awsRole: BUILD_ROLE, secrets: [
                        [id: 'qa/ows-product-integration-test/AUTOMATION_QA_DB_PASS', environmentVariable: 'QA_DB_PASS'],
                    ]) {
                        withAWS(role: BUILD_ROLE, roleAccount: QA_ACCOUNT_ID, roleSessionName: 'integration-test', useNode: true) {
                            sh 'make docker_test_integration'
                        }
                    }
                }
            }
        }
        stage('E2E Tests') {
            when {
                branch 'master'
            }
            environment {
                TAGS = "@ows_product"
            }
            steps {
                sleep(10)
                playwrightTests tags: env.TAGS
            }
        }
        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: GITHUB_REPOSITORY,
                    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()
        }
    }
}
