String GITHUB_REPOSITORY = 'lambda-collaborator-report'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#collaborators-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 E2E_ROLE = 'qa-lambda-collaborator-e2e-role'
String E2E_ACCOUNT_ID = '437795906767'

List<String> VULNERABILITIES_TO_IGNORE = [
    // Lambda watchdog is reporting many CVEs for the base image: 
    // https://github.com/aws/aws-lambda-base-images/issues
    // Remove the following CVEs when they are resolved
    // <<
    "CVE-2025-14087",
    "CVE-2025-61729",
    "CVE-2025-66418",
    "CVE-2025-66471",
    "CVE-2025-9086",
    'CVE-2026-4046', // pkg:deb/debian/glibc@2.36-9+deb12u13
    'CVE-2026-3805', // pkg:rpm/amazon/libcurl-minimal@8.5.0
    // >>
]

@groovy.transform.Field
List<String> functionsToBuild = null

pipeline {
    agent none

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: true, description: 'Whether or not to deploy to prod.')
        string(name: 'LAMBDA_FUNCTION_NAMES', defaultValue: '', description: 'Comma-separated list of Lambda function directory names to build and deploy')
        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 {
                withModifiedFunctions(checkout: true) { functionName ->
                    echo "Running Compliance Checks for ${functionName}"
                    dir("lambda/${functionName}") {
                        complianceChecks()
                    }
                }
            }
        }

        stage('Validate Software Catalog Definition') {
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    echo "Validating Software Catalog Definition for ${functionName}"
                    datadogSoftwareCatalogValidate(servicePath: "lambda/${functionName}")
                }
            }
        }

        stage('Unit Tests and Style Checks') {
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    script {
                        echo "Running Unit Tests and Style Checks for ${functionName}"
                        dir("lambda/${functionName}") {
                            try{
                                withEcr {
                                    sh "make ci_unit_lint"
                                }
                            }
                            finally {
                                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)
                                sh 'make ci_unit_lint_clean'
                            }
                        }
                    }
                }
            }
        }

        stage('Static Application Security Tests') {
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    echo "Running Static App Security Tests for ${functionName}"
                    sastTests(projectDir: "lambda/${functionName}")
                }
            }
        }

        stage('Sonar Scan and Analysis') {
            agent any
            when {
                branch 'master'
            }
            steps {
                script {
                    echo "Running Sonar scan for ${GITHUB_REPOSITORY}"
                    sonarScan project: GITHUB_REPOSITORY, language: 'py', exclusions: 'lambda/**/tests/**'
                }
            }
        }

        stage('Create a Release') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    echo "Building for ${functionName}"
                    dockerToEcr awsRegions: AWS_REGIONS,
                        ecrAccountId: ECR_ACCOUNT_ID,
                        imageName: "lambda-${functionName.replaceAll('_', '-')}",
                        imageTag: env.GIT_COMMIT,
                        dockerBuildContext: "lambda/${functionName}",
                        dockerBuildFile: "lambda/${functionName}/Dockerfile",
                        dockerBuildTarget: "deploy"
                }
            }
        }

        stage('Deploy to QA') {
            when {
                branch 'master'
            }
            steps {
                withModifiedFunctions { functionName ->
                    echo "Deploying to QA for ${functionName}"
                    dir("lambda/${functionName}") {
                        lambdaDeploy environment: 'qa',
                            awsRegions: AWS_REGIONS,
                            imageTag: env.GIT_COMMIT,
                            imageName: "lambda-${functionName.replaceAll('_', '-')}",
                            ecrRegistryAccountId: ECR_ACCOUNT_ID,
                            awsDeploymentTargetAccountId: QA_ACCOUNT_ID,
                            awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
                    }
                }
            }
        }

        stage('Scan Docker Images') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    echo "Scanning Docker Images for ${functionName}"
                    dir("lambda/${functionName}") {
                        dockerScan awsRegion: AWS_REGIONS[0],
                            ecrAccountId: ECR_ACCOUNT_ID,
                            imageName: "lambda-${functionName.replaceAll('_', '-')}",
                            imageTag: env.GIT_COMMIT,
                            vulnerabilitiesToIgnore: VULNERABILITIES_TO_IGNORE
                    }
                }
            }
        }

        stage('Report Generation E2E Tests') {
            agent any
            when {
                allOf {
                    branch 'master'
                }
            }
            environment {
                VENDOR_ID = 65228
                VENDOR_CURRENCY = 'USD'
                STATEMENT_PERIOD_ID = 313

                REPORT_GENERATION_QUEUE_URL = 'https://sqs.us-east-1.amazonaws.com/437795906767/qa-collaborator-report-trigger-queue'

                DB_COLLABORATORS_HOST = 'qa-ows-collaborator.cluster-cb22xqmk0y0q.us-east-1.rds.amazonaws.com'
                DB_COLLABORATORS_USER = 'lambda-collaborator-report-e2e'
                DB_COLLABORATORS_DB = 'ows_collaborator'

                DB_ART_RELATIONS_HOST = 'qa.db.qaorch.com'
                DB_ART_RELATIONS_USER = 'lambda-collaborator-report-e2e'
                DB_ART_RELATIONS_DB = 'art_relations'

                SNOWFLAKE_ACCOUNT = 'sme-delphi'
                SNOWFLAKE_USER = 'qa_lambda_collaborator_e2e_service_user'
                SNOWFLAKE_ROLE = 'qa_lambda_collaborator_e2e_service_user_role'
                SNOWFLAKE_WAREHOUSE = 'qa_ows_warehouse'

                AWS_DEFAULT_REGION = 'us-east-1'
            }
            steps {
                checkout scm
                withEcr {
                    withSecrets(
                        awsAccountId: E2E_ACCOUNT_ID,
                        awsRole: E2E_ROLE,
                        secrets: [
                            [id: 'qa/lambda-collaborator-e2e/DB_COLLABORATORS_PASSWORD', environmentVariable: 'DB_COLLABORATORS_PASSWORD'],
                            [id: 'qa/lambda-collaborator-e2e/DB_ART_RELATIONS_PASSWORD', environmentVariable: 'DB_ART_RELATIONS_PASSWORD'],
                            [id: 'qa/lambda-collaborator-e2e/SNOWFLAKE_PRIVATE_KEY', environmentVariable: 'SNOWFLAKE_PRIVATE_KEY'],
                            [id: 'qa/lambda-collaborator-e2e/SNOWFLAKE_KEY_PASSPHRASE', environmentVariable: 'SNOWFLAKE_KEY_PASSPHRASE'],
                        ],
                    ) {
                        withAWS(
                            role: E2E_ROLE,
                            roleAccount: E2E_ACCOUNT_ID,
                            useNode: true,
                        ) {
                            dir("e2e") {
                                sh 'make ci_test'
                            }
                        }
                    }
                }
            }
        }

        stage('Frontend E2E Tests') {
            when {
                branch 'master'
            }
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    script {
                            // Skip E2E tests for new functions for now
                            if ([
                                'collaborator-report-trigger',
                                'collaborator-report-process',
                                'collaborator-auto-report-run',
                            ].contains(functionName)) {
                                return [:]
                            }

                            return [
                                (functionName): {
                                    playwrightTests tags: "@collaborators_report_generation"
                                }
                            ]
                    }
                }
            }
        }

        stage('Deploy to PROD') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD }
                }
            }
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    echo "Deploying to PROD for ${functionName}"
                    dir("lambda/${functionName}") {
                        lambdaDeploy environment: 'prod',
                            awsRegions: AWS_REGIONS,
                            imageTag: env.GIT_COMMIT,
                            imageName: "lambda-${functionName.replaceAll('_', '-')}",
                            ecrRegistryAccountId: ECR_ACCOUNT_ID,
                            awsDeploymentTargetAccountId: PROD_ACCOUNT_ID,
                            awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
                    }
                    datadogSoftwareCatalogPublish(servicePath: "lambda/${functionName}")
                }
            }
        }
    }

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

def withModifiedFunctions(Map args = [:], Closure steps) {
    getMonorepoUtils().withModifiedProjects(args, steps)
}

def getMonorepoUtils() {
    return library("jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}").com.sonymusic.MonorepoUtils.getInstance(
        steps: this,
        projectBasePath: 'lambda',
        projectsToBuild: params.LAMBDA_FUNCTION_NAMES ? params.LAMBDA_FUNCTION_NAMES.split(',') : null
    )
}
