String GITHUB_REPOSITORY = 'lambda-product-transfer'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#transfer-product-ownership-alerts'
String QA_ACCOUNT_ID = '437795906767'
String QA_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'
String INTEGRATION_TEST_ROLE = 'qa-lambda-product-transfer-integration-tests-role'
String PROD_ACCOUNT_ID = '437795906767'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-aws-pipeline-agent'

FUNCTION_CONFIG = [
    'edit-attachments': [
        vulnerabilitiesToIgnore: [],
    ],
    'ensure-artists': [
        vulnerabilitiesToIgnore: [],
    ],
    'execute-content-transfer': [
        vulnerabilitiesToIgnore: [],
    ],
    'finalize-job': [
        vulnerabilitiesToIgnore: [],
    ],
    'handle-error': [
        vulnerabilitiesToIgnore: [],
    ],
    'update-dim-tables': [
        vulnerabilitiesToIgnore: [],
    ],
]

pipeline {
    agent none

    options {
        ansiColor('xterm')
        disableConcurrentBuilds()
        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).'
        )
        booleanParam(
            name: 'VERIFY_BUILD',
            defaultValue: false,
            description: 'Force build and test step to verify health.'
        )
    }

    triggers {
        issueCommentTrigger('.*(retest this please|run( sfn)? integration tests).*')
        parameterizedCron(env.BRANCH_NAME == 'master' ? '@weekly %DEPLOY_TO_PROD=false;VERIFY_BUILD=true;FUNCTION_NAME=*' : '')
    }

    stages {
        stage('Load Shared Libraries') {
            agent any
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Build Test and Scan') {
            when {
                expression { isPullRequest() || params.VERIFY_BUILD }
            }
            parallel {
                stage('Sonar Scan and Analysis') {
                    agent any
                    steps {
                        withModifiedFunctions(checkout: true) { functionName ->
                            echo "Running Sonar scan for ${functionName}"
                            sonarScan(
                                project: "${GITHUB_REPOSITORY}-${functionName.replaceAll('_', '-')}",
                                language: 'py',
                                projectBaseDir: "lambda/${functionName}",
                                qualityGateTimeout: 300
                            )
                        }
                    }
                }
                stage('Compliance Checks') {
                    agent any
                    steps {
                        complianceChecks()
                    }
                }
                stage('Validate Software Catalog Definition') {
                    agent any
                    steps {
                        withModifiedFunctions(checkout: true) { functionName ->
                            echo "Validating Software Catalog definition for ${functionName}"
                            datadogSoftwareCatalogValidate(servicePath: "lambda/${functionName}")
                        }
                    }
                }
                stage('Unit Tests and Style Checks') {
                    agent any
                    steps {
                        withModifiedFunctions(checkout: true) { functionName ->
                            script {
                                echo "Running Unit Tests and Style Checks for ${functionName}"
                                dir("lambda/${functionName}") {
                                    withEcr {
                                        withEnv(["COMPOSE_PROJECT_NAME=${env.BUILD_TAG.toLowerCase()}-${functionName}"]) {
                                            try {
                                                sh 'mkdir -p build && chmod a+w build && docker compose run --rm --build unit-lint'
                                            } finally {
                                                sh 'docker compose down --remove-orphans'
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                stage('Lint Integration Tests') {
                    agent any
                    when {
                        expression { getIntegrationTestsModified() }
                    }
                    steps {
                        script {
                            checkout scm
                            dir('integration_tests') {
                                withEcr {
                                    withEnv(["COMPOSE_PROJECT_NAME=${env.BUILD_TAG.toLowerCase()}-integration-tests"]) {
                                        try {
                                            sh 'docker compose --profile lint run --rm --build lint'
                                        } finally {
                                            sh 'docker compose down --remove-orphans'
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
                stage('Static Application Security Tests') {
                    agent any
                    steps {
                        withModifiedFunctions(checkout: true) { functionName ->
                            echo "Running Static App Security Tests for ${functionName}"
                            sastTests(projectDir: "lambda/${functionName}")
                        }
                    }
                }
                stage('Create and Docker Scan a Release') {
                    agent any
                    steps {
                        withModifiedFunctions(checkout: true) { functionName, config ->
                            script {
                                def sanitizedFunctionName = sanitizeFunctionName(functionName)
                                def imageName = "${GITHUB_REPOSITORY}-${sanitizedFunctionName}"
                                echo "Building for ${functionName}"
                                dockerToEcr(
                                    awsRegions: AWS_REGIONS,
                                    ecrAccountId: ECR_ACCOUNT_ID,
                                    imageName: imageName,
                                    imageTag: isPullRequest() ? env.BRANCH_NAME : "VERIFY-BUILD-${env.BUILD_NUMBER}",
                                    dockerBuildContext: "lambda/${functionName}",
                                    dockerBuildFile: "lambda/${functionName}/Dockerfile",
                                    dockerBuildTarget: 'deploy'
                                )
                                dockerScan(
                                    awsRegion: AWS_REGIONS[0],
                                    ecrAccountId: ECR_ACCOUNT_ID,
                                    imageName: imageName,
                                    imageTag: isPullRequest() ? env.BRANCH_NAME : "VERIFY-BUILD-${env.BUILD_NUMBER}",
                                    vulnerabilitiesToIgnore: config.vulnerabilitiesToIgnore
                                )
                            }
                        }
                    }
                }
            }
        }
        stage('Local Integration Tests') {
            agent any
            when {
                expression { isPullRequest() }
            }
            steps {
                script {
                    lock('product-transfer-integration-tests') {
                        withModifiedFunctions(checkout: true) { functionName ->
                            withEcr {
                                withAWS(roleAccount: QA_ACCOUNT_ID, role: "qa-lambda-pt-${functionName}-integration-tests-role", roleSessionName: env.BUILD_TAG, useNode: true) {
                                    withDatadogTestOptimizationEnv {
                                        dir('integration_tests') {
                                            withEnv([
                                                "COMPOSE_PROJECT_NAME=${env.BUILD_TAG.toLowerCase()}-${functionName}",
                                                'SNOWFLAKE_USER_NAME=QA_LAMBDA_PRODUCT_TRANSFER_UPDATE_DIM_TABLES',
                                                'SNOWFLAKE_ACCOUNT=delphi.us-east-1',
                                                'SNOWFLAKE_ROLE=QA_LAMBDA_PRODUCT_TRANSFER_UPDATE_DIM_TABLES',
                                                'SNOWFLAKE_SCHEMA=QA',
                                            ]) {
                                                sh "./scripts/run_tests.sh ${functionName} local"
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        stage('Retag and Docker Scan a Release') {
            agent any
            when {
                expression { !isPullRequest() }
            }
            steps {
                withModifiedFunctions(checkout: true) { functionName, config ->
                    script {
                        def sanitizedFunctionName = sanitizeFunctionName(functionName)
                        def imageName = "${GITHUB_REPOSITORY}-${sanitizedFunctionName}"
                        def commitHash = getCommitHash(functionName)
                        def prNumber = githubGetCommitPrs(
                            repo: GITHUB_REPOSITORY,
                            commitSha: commitHash
                        )[0].number
                        retagEcrImage(
                            awsRegions: AWS_REGIONS,
                            ecrAccountId: ECR_ACCOUNT_ID,
                            imageName: imageName,
                            imageTag: 'PR-' + prNumber,
                            newTag: commitHash
                        )
                        dockerScan(
                            awsRegion: AWS_REGIONS[0],
                            ecrAccountId: ECR_ACCOUNT_ID,
                            imageName: imageName,
                            imageTag: commitHash,
                            vulnerabilitiesToIgnore: config.vulnerabilitiesToIgnore
                        )
                    }
                }
            }
        }
        stage('Deploy to QA') {
            agent any
            when {
                expression { !isPullRequest() }
            }
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    script {
                        def commitHash = getCommitHash(functionName)
                        echo "Deploying to QA for ${functionName}"
                        dir("lambda/${functionName}") {
                            lambdaDeploy(
                                environment: 'qa',
                                awsRegions: AWS_REGIONS,
                                imageTag: commitHash,
                                imageName: "${GITHUB_REPOSITORY}-${sanitizeFunctionName(functionName)}",
                                ecrRegistryAccountId: ECR_ACCOUNT_ID,
                                awsDeploymentTargetAccountId: QA_ACCOUNT_ID,
                                awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
                            )
                        }
                    }
                }
            }
        }
        stage('Integration Tests') {
            agent any
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ '.*run integration tests.*' }
                }
            }
            steps {
                script {
                    def modifiedFunctions = []
                    withModifiedFunctions { functionName ->
                        modifiedFunctions << functionName
                    }
                    def functionNamesToTest = modifiedFunctions.join(',')
                    withEcr {
                        withAWS(roleAccount: QA_ACCOUNT_ID, role: INTEGRATION_TEST_ROLE, roleSessionName: env.BUILD_TAG, useNode: true) {
                            lock('product-transfer-integration-tests') {
                                withDatadogTestOptimizationEnv {
                                    dir('integration_tests') {
                                        try {
                                            withEnv(["LAMBDA_FUNCTION_NAMES=${functionNamesToTest}"]) {
                                                sh 'docker compose up --build --exit-code-from integration-tests'
                                            }
                                        } finally {
                                            sh 'docker compose down --remove-orphans'
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        stage('State Machine Integration Test') {
            agent any
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ '.*run sfn integration tests.*' }
                }
            }
            steps {
                script {
                    withEcr {
                        withAWS(roleAccount: QA_ACCOUNT_ID, role: INTEGRATION_TEST_ROLE, roleSessionName: env.BUILD_TAG, useNode: true) {
                            lock('product-transfer-integration-tests') {
                                withDatadogTestOptimizationEnv {
                                    dir('integration_tests') {
                                        try {
                                            withEnv(["LAMBDA_FUNCTION_NAMES=state-machine"]) {
                                                sh 'docker compose up --build --exit-code-from integration-tests'
                                            }
                                        } finally {
                                            sh 'docker compose down --remove-orphans'
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        stage('Deploy to Prod') {
            agent any
            when {
                expression { params.DEPLOY_TO_PROD && !isPullRequest() }
            }
            steps {
                withModifiedFunctions(checkout: true) { functionName ->
                    script {
                        def commitHash = getCommitHash(functionName)
                        echo "Deploying to PROD for ${functionName}"
                        dir("lambda/${functionName}") {
                            lambdaDeploy(
                                environment: 'prod',
                                awsRegions: AWS_REGIONS,
                                imageTag: commitHash,
                                imageName: "${GITHUB_REPOSITORY}-${sanitizeFunctionName(functionName)}",
                                ecrRegistryAccountId: ECR_ACCOUNT_ID,
                                awsDeploymentTargetAccountId: PROD_ACCOUNT_ID,
                                awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
                            )
                        }
                        datadogSoftwareCatalogPublish(servicePath: "lambda/${functionName}")
                    }
                }
            }
        }
    }

    post {
        regression {
            script {
                if (!isPullRequest()) {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
        fixed {
            script {
                if (!isPullRequest()) {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
    }
}

def getIntegrationTestsModified() {
    return getModifiedPaths(paths: ['integration_tests']).size() > 0
}

def isPullRequest() {
    return env.BRANCH_NAME != 'master'
}

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',
        projectConfig: FUNCTION_CONFIG,
        projectsToBuild: params.LAMBDA_FUNCTION_NAMES ? params.LAMBDA_FUNCTION_NAMES.split(',') as Set : null
    )
}

def getCommitHash(String functionName) {
    return sh(
        script: "git log -1 --pretty=format:'%H' -- lambda/${functionName}",
        returnStdout: true
    ).trim()
}

def sanitizeFunctionName(String functionName) {
    return functionName.replaceAll('_', '-')
}
