pipeline {
    agent none
    parameters {
        choice(name: 'ENV', choices: ['qa', 'stage', 'prod', 'sme', 'uat'], description: 'Name of environment')
        string(name: 'PR_NUMBER', defaultValue: '0000', description: 'Number of PR, minus special characters')
        string(name: 'S3_BUCKET', defaultValue: '', description: 'Optional S3 Bucket that can be injected into sql. Paths within buckets should be included as well, e.g. prod-temp/db-deploy')
        choice(name: 'type', choices: ['update', 'rollback'], description: 'Update will execute the db changeset, rollback will execute the rollback query (assumes only 1 changeset)')
        choice(name: 'FORCE_NEO4J_KAFKA_LOCK_RELEASE', choices: ['false', 'true'], description: 'Whether to release kafka-db-deploy lock. Only used for kafka-db-deploy deployments.')
        string(name: 'SNOWFLAKE_BATCH_SIZE_FOR_KAFKA_DB_DEPLOY', defaultValue: '5000', description: 'SNOWFLAKE_BATCH_SIZE for reads from snowflake. Only used for kafka-db-deploy deployments.')
        string(name: 'SINK_BATCH_SIZE_FOR_KAFKA_DB_DEPLOY', defaultValue: '5', description: 'Batch Size for target/sink writes. Only used for kafka-db-deploy deployments.')
        choice(name: 'SINK_CONTINUE_ON_DLQ', choices: ['false', 'true'], description: 'Whether to immediately stop on DLQ error or continue all the batches and log dlq errors. Only used for kafka-db-deploy deployments.')
        string(name: 'ECR_REPOSITORY_TAG', defaultValue: 'latest', description: 'Optional ECR_REPOSITORY_TAG. Used in kafka-db-deploy tests only.')
    }
    environment {
        GITHUB_TOKEN = credentials('orchardci-webhook-token')
    }
    stages {
        stage('Check if PR has been approved') {
            agent any
            steps {
                script {
                    checkout([$class: 'GitSCM', branches: [[name: '*/master']], doGenerateSubmoduleConfigurations: false, extensions: [], submoduleCfg: [], userRemoteConfigs: [[credentialsId: '577cbc72-7d9e-4eba-924c-ecbbe6de9805', url: 'git@github.com:theorchard/database.git']]])
                    currentBuild.description = "#${params.PR_NUMBER} - ${params.ENV} - ${params.type}"
                }
                sh """
                    python3.8 -m venv venv
                    venv/bin/pip3 install --upgrade pip
                    venv/bin/pip3 install -r ./tools/python/requirements.txt
                    venv/bin/python3.8 ./tools/python/pr_approval_check.py --pr-number ${params.PR_NUMBER} --raise-exception
                """
            }
        }
        stage('Deploy DB Change') {
            steps {
                script {
                    build job: 'db-deploy', parameters: [
                        string(name: 'ENV', value: params.ENV),
                        string(name: 'FORCE_NEO4J_KAFKA_LOCK_RELEASE', value: params.FORCE_NEO4J_KAFKA_LOCK_RELEASE),
                        string(name: 'SNOWFLAKE_BATCH_SIZE_FOR_KAFKA_DB_DEPLOY', value: params.SNOWFLAKE_BATCH_SIZE_FOR_KAFKA_DB_DEPLOY),
                        string(name: 'SINK_BATCH_SIZE_FOR_KAFKA_DB_DEPLOY', value: params.SINK_BATCH_SIZE_FOR_KAFKA_DB_DEPLOY),
                        string(name: 'SINK_CONTINUE_ON_DLQ', value: params.SINK_CONTINUE_ON_DLQ),
                        string(name: 'sha1', value: "refs/remotes/origin/pr/${params.PR_NUMBER}/head"),
                        string(name: 'S3_BUCKET', value: params.S3_BUCKET),
                        string(name: 'ECR_REPOSITORY_TAG', value: params.ECR_REPOSITORY_TAG),
                        string(name: 'type', value: params.type)
                    ]
                }
            }
        }
    }
}
