String GITHUB_REPOSITORY = 'scripts-assets'

DIRECTORY_CONFIG = [
    'bulk-asset-downloader': [
        test: true
    ],
    'bulk-download-s3-assets': [
        test: false
    ],
    'create-asset-finals': [
        test: false
    ],
    'enqueue-hive-backfill': [
        test: true
    ],
    'hive-regression-tracking': [
        test: false
    ],
    'invoke-hive-ai-detection': [
        test: true
    ],
    'sync-s3-assets-to-qa': [
        test: true
    ],
    'spatial-asset-ingester': [
        test: true
    ],
]

pipeline {
    agent none

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

    parameters {
        string(
            name: 'DIRECTORIES',
            defaultValue: '',
            description: "Comma-separated list of directories to build. If not specified, all modified directories will be built."
        )
        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') {
            agent any
            steps {
                complianceChecks()
            }
        }
        stage('Static Application Security Tests') {
            steps {
                withModifiedProjects(checkout: true) { project, config ->
                    sastTests(projectDir: project)
                }
            }
        }
        stage('Unit Tests and Style Checks') {
            steps {
                withModifiedProjects(checkout: true) { project, config ->
                    withEnv(["COMPOSE_PROJECT_NAME=${env.BUILD_TAG.toLowerCase()}-${project.replaceAll('/', '-')}"]) {
                        withEcr {
                            script {
                                if (config.test == true) {
                                    dir(project) {
                                        try {
                                            sh "docker compose run --rm --build lint-and-test"
                                        }
                                        finally {
                                            sh 'docker compose down --remove-orphans'
                                        }
                                    }
                                } else {
                                    echo "No unit tests or style checks configured for project: ${project}"
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

def withModifiedProjects(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,
        projectConfig: DIRECTORY_CONFIG,
        projectsToBuild: params.DIRECTORIES ? params.DIRECTORIES.split(',') : null
    )
}
