String SLACK_NOTIFICATIONS_CHANNEL = '#publishing-alerts'
String AWS_ACCOUNT_ID = '437795906767'
String MYSQL_CHANGELOG_USER = 'compositions-changelog'
String MYSQL_CHANGELOG_DATABASE = 'publishing_compositions'
String MYSQL_CHANGELOG_HOST = 'publishing-compositions.cluster-cb22xqmk0y0q.us-east-1.rds.amazonaws.com'

pipeline {
    agent any

    parameters {
        string(name: 'SHARED_LIBRARIES_VERSION', defaultValue: 'master', description: 'Shared Libraries Version')
        choice(name: 'ENVIRONMENT', choices: ['qa', 'prod'], description: 'The environment to fix the non-utf8 characters')
        choice(name: 'FIELDS_TO_FIX', choices: ['All','Composition title','Alternate Titles', 'Songwriter Legal Name', 'Publisher name'], description: 'The field to fix the non-utf8 characters')
        string(name: 'CHUNK_SIZE', description: 'Number of compositions to be fetched', defaultValue: '250')
        string(name: 'INIT_ROW', description: 'Initial row to start the process', defaultValue: '0')
    }

    stages {    
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@${params.SHARED_LIBRARIES_VERSION}"
            }
        }
        stage('Fix Non-UTF8 Characters') {
            steps {
                
                withSecrets(awsAccountId: AWS_ACCOUNT_ID, awsRole: "${params.ENVIRONMENT}-publishing-fix-non-utf8-characters-script-role", secrets:[
                    [id: "${params.ENVIRONMENT}/graphql-publishing/MYSQL_CHANGELOG_PASSWORD", environmentVariable: 'MYSQL_CHANGELOG_PASSWORD'],
                ] ){
                    script {
                        def environment = params.ENVIRONMENT
                        def fieldsToFix = params.FIELDS_TO_FIX
                        def chunkSize = params.CHUNK_SIZE
                        def initRow = params.INIT_ROW
                        
                        dir('scripts/changelog-non-utf8-fixer'){
                            withAWS(roleAccount: AWS_ACCOUNT_ID, 
                                role: "${params.ENVIRONMENT}-publishing-fix-non-utf8-characters-script-role", 
                                roleSessionName: env.BUILD_TAG, 
                                useNode: true
                            ) {
                                yarnRun(
                                    envVars: [
                                        ENV: "${environment}",
                                        NODE_ENV: "${environment}",
                                        MYSQL_CHANGELOG_USER: "${MYSQL_CHANGELOG_USER}",
                                        MYSQL_CHANGELOG_DATABASE: "${MYSQL_CHANGELOG_DATABASE}",
                                        MYSQL_CHANGELOG_HOST: "${environment}-${MYSQL_CHANGELOG_HOST}",
                                        MYSQL_CHANGELOG_PASSWORD: "${MYSQL_CHANGELOG_PASSWORD}",
                                        AWS_ACCESS_KEY_ID: "${AWS_ACCESS_KEY_ID}",
                                        AWS_SECRET_ACCESS_KEY: "${AWS_SECRET_ACCESS_KEY}",
                                        AWS_SESSION_TOKEN: "${AWS_SESSION_TOKEN}",
                                        AWS_REGION: "us-east-1",
                                    ],
                                    scriptNames: ["start ${environment} '${fieldsToFix}' ${chunkSize} ${initRow}"],
                                )
                            }
                        }
                    }
                }
                
            }
        }
    }
    post{
        regression {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        fixed {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        cleanup {
            cleanWs()
        }
    }

}