def APP_NAME = 'frontend-distribution-oa-components'
def SLACK_NOTIFY_CHANNEL = '#content-creation-mgmt-alerts'
def POEDITOR_PROJECT_ID = '252559'

def APPLICATION_NAME = 'oa-project-info'

pipeline {
    agent {
        label 'aws'
    }

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

    parameters {
        choice(name: 'MINIFY', choices: [1, 0], 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: 'packagecloud_read_token', variable: 'PACKAGECLOUD_TOKEN'),
                ]) {
                    yarnRun([
                        scriptNames: ['test'],
                        envVars: [
                            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'
                }
            }
        }
        stage('Deploy to QA') {
            when {
                branch 'master'
            }
            steps {
                withCredentials([
                    string(credentialsId: 'packagecloud_read_token', variable: 'PACKAGECLOUD_TOKEN')
                ]) {
                    suiteAppBuild (
                        env: 'qa',
                        envVars: [
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}",
                            POEDITOR_PROJECT_ID: POEDITOR_PROJECT_ID,
                            APPLICATION_NAME: APPLICATION_NAME,
                            DEPLOY_TIMESTAMP: env.GIT_COMMIT,
                            MINIFY: params.MINIFY
                        ]
                    )
                }

                suiteAppPublish (
                    env: "qa",
                    appName: APP_NAME,
                )
            }
        }
        stage('Invalidate QA CDN') {
            when {
                branch 'master'
            }
            steps {
                cdnInvalidate(
                    env: 'qa',
                    appName: APP_NAME
                )
            }
        }
        stage('E2E Tests') {
            when {
                branch 'master'
            }
            steps {
                playwrightTests tags: '@frontend_distribution_oa_components'
            }
        }
        stage('Deploy to Prod') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                withCredentials([
                    string(credentialsId: 'packagecloud_read_token', variable: 'PACKAGECLOUD_TOKEN')
                ]) {
                    suiteAppBuild (
                        env: 'prod',
                        envVars: [
                            PACKAGECLOUD_TOKEN: "\${PACKAGECLOUD_TOKEN}",
                            POEDITOR_PROJECT_ID: POEDITOR_PROJECT_ID,
                            APPLICATION_NAME: APPLICATION_NAME,
                            DEPLOY_TIMESTAMP: env.GIT_COMMIT,
                        ]
                    )
                }

                suiteAppPublish (
                    env: 'prod',
                    appName: APP_NAME,
                )
            }
        }
        stage('Invalidate Prod CDN') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                cdnInvalidate(
                    env: 'prod',
                    appName: APP_NAME
                )
            }
        }
        stage('Publish Software Catalog') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                datadogSoftwareCatalogPublish()
            }
        }
    }

    post {
        failure {
            cleanWs()
        }
    }
}
