String GITHUB_REPOSITORY = 'pdp-backfill'
String ECR_ACCOUNT_ID = '086679231553'
List<String> AWS_REGIONS = ['us-east-1']
String SLACK_NOTIFICATIONS_CHANNEL = '#permissions-platform-alerts'
String QA_ACCOUNT_ID = '591204808501'
String QA_DEPLOYMENT_ROLE = 'qa-jenkins-pipeline-deploy-role'
String QA_JENKINS_PIPELINE_ROLE = 'qa-pdp-backfill-integration-test'
String SESSION_NAME = "qa-pdp-backfill-integration-test"
String PROD_ACCOUNT_ID = '031099521156'
String PROD_DEPLOYMENT_ROLE = 'prod-jenkins-pipeline-deploy-role'
List<String> VULNERABILITIES_TO_IGNORE = []


pipeline {
    agent {
        label 'aws'
    }

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

    parameters {
        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.')
        booleanParam(name: 'FAIL_BUILD_ECR_SCAN', defaultValue: false, description: 'If ECR Scan fails/times out, fail the build.')
    }

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

    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('Unit Tests and Style Checks') {
            steps {
                withEcr{
                    sh 'make ci_test_unit_lint'
                }
            }
            post {
                always {
                	xunit tools: [JUnit(pattern: 'build/pyunit.xml', deleteOutputFiles: true, failIfNotNew: true, stopProcessingIfError: true)]
                     recordCoverage(tools: [[parser: 'COBERTURA', pattern: 'build/coverage.xml']], enabledForFailure: true, failOnError: true, sourceDirectories: [[path: "backfill"]])
                 }
                cleanup {
                    sh 'make ci_clean_unit_lint'
                }
            }
        }
        stage('Static Application Security Tests') {
            steps {
                sastTests()
            }
        }
        stage('Sonar Scan and Analysis') {
            when {
                branch 'master'
            }
            steps {
                sonarScan project: GITHUB_REPOSITORY, language: 'py'
            }
        }
        stage('Create a Release') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                dockerToEcr awsRegions: AWS_REGIONS, ecrAccountId: ECR_ACCOUNT_ID,
                    imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT,
                    dockerBuildTarget: 'deploy'
            }
        }
        stage('Integration Tests') {
            when {
                branch 'master'
            }
            steps {
                withEcr{
                    withAWS(role: QA_JENKINS_PIPELINE_ROLE, roleAccount: QA_ACCOUNT_ID, roleSessionName: SESSION_NAME, useNode: true) {
                        sh 'make ci_test_integration'
                    }
                }
            }
        }
        stage('Deploy to QA') {
            when {
                branch 'master'
            }
            steps {
                fargateDeploy environment: 'qa', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: GITHUB_REPOSITORY,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, deployType: 'CREATE_TASK_DEFINITION',
                    awsDeploymentTargetAccountId: QA_ACCOUNT_ID, awsDeploymentRoleName: QA_DEPLOYMENT_ROLE
            }
        }
        stage('Update pdp_qa_refresh files in S3') {
            when {
                allOf {
                    branch 'master'
                    expression { isPdpQARefreshUpdated() == true }
                }
            }
            steps {
                withAWS(role: QA_DEPLOYMENT_ROLE, roleAccount: QA_ACCOUNT_ID, roleSessionName: "upload_pdp_qa_refresh", useNode: true) {
                    sh 'echo "uploading pdp_qa_refresh/*.csv to s3://qa-pdp-backfill/pdp_qa_refresh"'
                    s3Upload(
                        bucket: "qa-pdp-backfill",
                        path: "pdp_qa_refresh/",
                        workingDir: 'pdp_qa_refresh',
                        includePathPattern: '**/*.csv',
                        excludePathPattern: '*.json',
                    )

                    sh 'echo "finally uploading pdp_qa_refresh/manifest.json to s3://qa-pdp-backfill/pdp_qa_refresh/manifest.json"'
                    s3Upload(
                        bucket: "qa-pdp-backfill",
                        path: "pdp_qa_refresh/",
                        workingDir: 'pdp_qa_refresh',
                        includePathPattern: '*.json',
                    )
                }
            }
        }
        stage('Scan Docker Image') {
            when {
                anyOf {
                    branch 'master'
                    expression { env.GITHUB_COMMENT =~ 'build docker' }
                }
            }
            steps {
                dockerScan awsRegion: AWS_REGIONS[0], ecrAccountId: ECR_ACCOUNT_ID,
                    imageName: GITHUB_REPOSITORY, imageTag: env.GIT_COMMIT,
                    vulnerabilitiesToIgnore: VULNERABILITIES_TO_IGNORE
            }
        }
        stage('Deploy to Prod') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                fargateDeploy environment: 'prod', awsRegions: AWS_REGIONS, gitCommit: env.GIT_COMMIT, serviceName: GITHUB_REPOSITORY,
                    ecrRegistryAccountId: ECR_ACCOUNT_ID, deployType: 'CREATE_TASK_DEFINITION',
                    awsDeploymentTargetAccountId: PROD_ACCOUNT_ID, awsDeploymentRoleName: PROD_DEPLOYMENT_ROLE
            }
        }
        stage('Publish Software Catalog Definition') {
            when {
                allOf {
                    branch 'master'
                    expression { params.DEPLOY_TO_PROD == 'Yes' }
                }
            }
            steps {
                datadogSoftwareCatalogPublish()
            }
        }
    }

    post {
        always {
            script {
                if (env.BRANCH_NAME == 'master') {
                    slackNotify channel: SLACK_NOTIFICATIONS_CHANNEL
                }
            }
        }
        cleanup {
            cleanWs()
        }
    }
}

/**
 * isPdpQARefreshUpdated returns True if
 * csvs or manifest.json have been updated.
 * Else, isPdpQARefreshUpdated returns False.
 */
def isPdpQARefreshUpdated() {
    def modifiedPaths = getModifiedPaths()
    for (modifiedPath in modifiedPaths) {
        if (modifiedPath.startsWith('pdp_qa_refresh/')) {
            return true
        }
    }
    return false
}
