String SLACK_NOTIFICATIONS_CHANNEL = '#publishing-alerts'

pipeline {
    agent any

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

    parameters {
        stashedFile 'backfill.csv'
        choice(name: 'ENVIRONMENT', choices: ['qa', 'prod'], description: 'The environment to fix terraform state for')
        string(name: 'IDENTITY_ID', description: '''The user's Neo4j identity that will be on set the createBy/modifiedBy fields. Think twice about this. It should not be yours.<br>
        <br>
        Lucner Frederique   85f48271-017e-4600-8e0c-5ff434c265fb<br>
        Aidan Miller    11e259e5-5b61-48a6-b38b-2dd348ca4156<br>
        Hiram Wilson    af895e59-fa45-484e-999d-80094675a5cd''')
        string(name: 'PROFILE_ID', description: '''The user's Profile ID. This should be the profile associated with the identity above and should be the person who requested the ingestion.<br>
        <br>
        Lucner Frederique 131816<br>
        Aidan Miller    74838<br>
        Hiram Wilson    326982<br>''')
        string(name: 'CHUNK_SIZE', description: 'Number of parallel requests', defaultValue: '10')
    }

    stages {
        stage('Load Shared Libraries') {
            steps {
                library "jenkins-global-libraries@master"
            }
        }
        stage('Run script') {
            steps {
                unstash 'backfill.csv'
                yarnRun(
                    scriptNames: ["run:csv-ingest ${params.ENVIRONMENT} backfill.csv ${params.PROFILE_ID} ${params.IDENTITY_ID} ${params.CHUNK_SIZE}"],
                )
            }
        }
    }
    post {
        regression {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        fixed {
            slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
        }
        cleanup {
            cleanWs()
        }
    }
}
