def APP_NAME = 'frontend-promo-player-builder'
def SLACK_NOTIFY_CHANNEL = '#content-review-team'
def DEFAULT_AWS_REGION = 'us-east-1'
def POEDITOR_PROJECT_ID = '160769'

def QA_CDN_URL = 'https://qa-cdn.theorchard.io'
def PROD_CDN_URL = 'https://cdn.theorchard.io'

pipeline {
    agent {
        label 'aws'
    }

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

    parameters {
        choice(name: 'MINIFY', choices: [0, 1], description: 'Whether or not to minify.')
        choice(name: 'DEPLOY_TO_PROD', choices: ['Yes', 'No'], description: 'Whether or not to deploy to prod.')
        string(name: 'SHARED_LIBRARIES_VERSION', defaultValue: 'master', description: 'The version of the Jenkins shared libraries to use. Can be a branch, tag or Git revision.')
    }

    triggers {
        issueCommentTrigger('.*retest this please.*|.*deploy pri.*')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Compliance Checks') {
            steps {
                complianceChecks()
            }
        }
        stage('Validate Software Catalog Definition') {
            steps {
                datadogSoftwareCatalogValidate()
            }
        }
        stage('Static Application Security Tests') {
            steps {
                sastTests()
            }
        }
        stage('Unit Tests and Style Checks') {
            when {
                not {
                    environment name: 'GITHUB_COMMENT', value: 'deploy pri'
                }
            }
            steps {
                withCredentials([
                    string(credentialsId: 'github_packages_token', variable: 'GITHUB_NPM_TOKEN'),
                    string(credentialsId: 'packagecloud_read_token', variable: 'PACKAGECLOUD_TOKEN'),
                ]) {
                    yarnRun([
                        scriptNames: ['test'],
                        nodeVersion: '24',
                        envVars: [
                            GITHUB_NPM_TOKEN: "\${GITHUB_NPM_TOKEN}",
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}"
                        ]
                    ])
                }
            }
        }
        stage('Sonar Scan and Analysis') {
            when {
                branch 'master'
            }
            steps {
                script {
                    echo "Running Sonar scan for ${APP_NAME}"
                    sonarScan project: APP_NAME, language: 'js'
                }
            }
        }

        // Publish the package to the packagecloud repository

        stage('Publish to Packagecloud') {
            when {
                branch 'master'
            }
            steps {
                withCredentials([
                    usernamePassword(credentialsId: 'packagecloud_api_credentials', usernameVariable: 'PACKAGECLOUD_USER', passwordVariable: 'PACKAGECLOUD_TOKEN'),
                    string(credentialsId: 'poeditor-api-token', variable: 'API_KEY')
                ]) {
                    nodeSh(
                        envVars: [
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}",
                            POEDITOR_PROJECT_ID: POEDITOR_PROJECT_ID,
                            API_KEY: "\${API_KEY}",
                        ],
                        nodeVersion: '24',
                        script: """
                            set -e
                            yarn install --frozen-lockfile
                            yarn deploy-lib
                            yarn pack
                        """
                    )
                    script {
                        def envVars = [
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}",
                            PACKAGECLOUD_USER: "\${PACKAGECLOUD_USER}",
                        ]
                        dockerRun (
                            imageTag: "ruby32",
                            envVars: envVars,
                            user: "worker",
                            workdir: "/var/app",
                            volumes: ["\$(pwd):/var/app"],
                            script: "./publish_package.sh"
                        )
                    }
                }
            }
        }
        stage('Publish Software Catalog') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                datadogSoftwareCatalogPublish()
            }
        }
    }
    post {
        cleanup {
            cleanWs()
        }
    }
}
