#!groovy
import uk.gov.hmcts.contino.GithubAPI
properties(
        [[$class: 'GithubProjectProperty', projectUrlStr: 'https://github.com/hmcts/ccd-api-gateway'],
         pipelineTriggers([[$class: 'GitHubPushTrigger']])]
)

@Library("Infrastructure@cve-dashboard")

def product = "ccd"
def component = "api-gateway-web"
def branchesToSync = ['demo', 'ithc', 'perftest']

// Variables to switch pipeline logic and wiring per type of build
def gatewayApiDevelopPr = "PR-332" // This doesn't change frequently, but when it does, only change this value.

def secrets = [
    'ccd-${env}': [
        secret('postcode-info-address-lookup-token', 'ADDRESS_LOOKUP_TOKEN'),
    ]
]

// Prevent Docker hub rate limit errors by ensuring that testcontainers uses images from hmctsprod ACR
env.TESTCONTAINERS_HUB_IMAGE_NAME_PREFIX = "hmctsprod.azurecr.io/imported/"

static LinkedHashMap<String, Object> secret(String secretName, String envVar) {
    [ $class: 'AzureKeyVaultSecret',
      secretType: 'Secret',
      name: secretName,
      version: '',
      envVariable: envVar
    ]
}

def vaultOverrides = [
    'preview': 'aat',
    'spreview': 'saat'
]

env.PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=true

withPipeline("nodejs", product, component) {
    onMaster {
        enableSlackNotifications('#ccd-master-builds')
    }
    if (env.BRANCH_NAME.equalsIgnoreCase(gatewayApiDevelopPr)) {
        enableSlackNotifications('#ccd-develop-builds')
    }
    onDemo {
        enableSlackNotifications('#ccd-demo-builds')
    }
    onPR {
        enableSlackNotifications('#ccd-pr-builds')
        def githubApi = new GithubAPI(this)
    }

    syncBranchesWithMaster(branchesToSync)
    overrideVaultEnvironments(vaultOverrides)
    loadVaultSecrets(secrets)

    enableAksStagingDeployment()
    disableLegacyDeployment()
    enableCveDashboardIngestion()

    afterAlways('checkOut') {
        sh "yarn cache clean"
    }

    afterAlways('build') {
        sh "yarn --check-cache"
    }

}
