String GITHUB_REPOSITORY = 'lambda-ddex-ingester'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#ddex-ingester'

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'
List<String> VULNERABILITIES_TO_IGNORE = [
    'CVE-2024-36039',
    'CVE-2025-66418', // urllib3
    'CVE-2025-66471', // urllib3
    'CVE-2026-21441', // urllib3
    'CVE-2026-24049',
    'CVE-2026-23949',
    'CVE-2026-4046',
    'CVE-2026-6100',
    'CVE-2025-45768',
    'CVE-2026-33811',
    'CVE-2026-33814',
    'CVE-2026-39820',
    'CVE-2026-39836',
    'CVE-2026-42499',
    'CVE-2026-44431',
    'CVE-2026-4786',
    'CVE-2026-6766',
    'CVE-2026-6772',
    'CVE-2026-7598',
    'CVE-2026-27145',
    'CVE-2026-42504',  // pkg:generic/package_dedupe?go_toolchain=1.26.3
]

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

pipeline {
    agent none

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

    parameters {
        booleanParam(name: 'DEPLOY_TO_PROD', defaultValue: false, 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 {
                script {
                    withModifiedFunctions(checkout: true) { functionName ->
                        echo "Running Compliance Checks for ${functionName}"
                        dir("lambda/${functionName}") {
                            complianceChecks()
                        }
                    }
                }
            }
        }

        stage('Validate Software Catalog Definitions') {
            steps {
                script {
                    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 ->
                    echo "Running Unit Tests and Style Checks for ${functionName}"
                    withEcr {
                        dir("lambda/${functionName}") {
                            sh 'docker compose run --rm --build lint-and-test'
                        }
                    }
                }
            }
        }

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

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

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

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

        stage('Integration Tests') {
            when {
                branch 'master'
            }
            steps {
                script {
                    parallel(getFunctionsToBuild().collectEntries { functionName ->
                        return [
                            (functionName): {
                                build job: 'lambda-ddex-ingester-integration-tests',
                                    parameters: [string(name: 'GIT_COMMIT', value: GIT_COMMIT),
                                    string(name: 'LAMBDA_DIR', value: functionName)]
                            }
                        ]
                    })
                }
            }
        }

        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',
                            functionName: getFullFunctionName("${functionName.replaceAll('_', '-')}", 'prod'),
                            awsRegions: AWS_REGIONS,
                            imageTag: env.GIT_COMMIT,
                            imageName: "${GITHUB_REPOSITORY}-${functionName.replaceAll('_', '-')}",
                            ecrRegistryAccountId: ECR_ACCOUNT_ID,
                            awsDeploymentTargetAccountId: PROD_ACCOUNT_ID,
                            awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE,
                            publishVersion: true
                    }
                    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
    )
}

def getFunctionsToBuild() {
    if (this.@functionsToBuild != null) {
        return this.@functionsToBuild
    }

    if (params.LAMBDA_FUNCTION_NAMES) {
        this.@functionsToBuild = params.LAMBDA_FUNCTION_NAMES.split(',').collect { it.trim() }
    } else {
        node {
            def scmVars = checkout scm
            def lambdaDirectories = findFiles(glob: '**/Dockerfile')
            this.@functionsToBuild = getModifiedFunctions branchName: scmVars.BRANCH_NAME,
                previousSuccessfulCommit: scmVars.GIT_PREVIOUS_SUCCESSFUL_COMMIT,
                lambdaDirectories: lambdaDirectories
        }
    }

    if (this.@functionsToBuild) {
        currentBuild.description = this.@functionsToBuild.join('<br>')
    }

    return this.@functionsToBuild
}

def getFullFunctionName(String sanitizedFunctionName, String env) {
    if (sanitizedFunctionName == 'awal-ingestion-flow-control') {
        return "${env}-ddex-ingester-${sanitizedFunctionName}"
    } else {
        return "ddex-ingester-${sanitizedFunctionName}-${env}"
    }
}
