String GITHUB_REPOSITORY = 'scripts-product-staging'

pipeline {
    agent none

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

    parameters {
        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).')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }

        stage('Compliance Checks') {
            agent any
            steps {
                complianceChecks()
            }
        }

        stage('Unit Tests and Style Checks') {
            steps {
                script {
                    sh '''
                        docker compose -f docker-compose.yaml up --build \ 
                            --exit-code-from unit-lint \
                            --remove-orphans \
                            unit-lint
                    '''
                }
            }
        }

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