import groovy.json.JsonOutput

// https://jenkins-og.xarth.tv/job/eventbus-controlplane-updater-deploy/

STAGING_BRANCH = "master" 
STAGING_ACCT = "twitch-eventbus-dev"
PROD_ACCT = "twitch-eventbus-prod"
AWS_REGION = "us-west-2"

def make(cmd) {
    sh "make ${cmd}"
}

def gitShortCommitSha() {
    return sh(script: "git rev-parse --short HEAD", returnStdout: true).trim()
}

def gitShortCommitMsg() {
    return sh(script: "git log --pretty=oneline --abbrev-commit -1 --no-decorate", returnStdout: true).trim()
}

def commitLink() {
  sha = gitShortCommitSha()
  msg = gitShortCommitMsg()
  return "<https://git.xarth.tv/eventbus/controlplane/commit/${sha}|*\"${msg}\"*>"
}

def message(payload) {
  try {
    slackSend(
      attachments: JsonOutput.toJson(payload),
      channel: '#eventbus-deploys',
      tokenCredentialId: 'samus-slack-api-token'
    )
  } catch (Exception e) {
    println e
  }
}

pipeline {
    agent any
    options {
        disableConcurrentBuilds()
        timeout(time: 1, unit: 'HOURS')
        ansiColor('xterm')
        timestamps()
    }
    stages {
        stage('Build') {
            steps {
                make("build-controlplane-updater")
            }
        }

        stage('Deploy Staging') {
            steps {
                withAWS(credentials: STAGING_ACCT, region: AWS_REGION) {
                    sh "AWS_ACCT_ID=297385687169 ./cmd/controlplane-updater-lambda/deploy-controlplane-updater.sh"
                }
            }
            post {
                success {
                    script {
                        message([[
                            title: 'Deployed controlplane-updater lambda to staging successfully ✔',
                            color: 'good',
                            text: "Successfully deployed commit ${commitLink()}",
                            actions: [[
                            type: 'button',
                            text: 'Deploy to *Production* 🚀',
                            url: "https://jenkins-og.xarth.tv/job/eventbus-controlplane-updater-deploy/"
                            ]]
                        ]])
                    }
                }
                failure {
                    script {
                        message([[
                            title: 'Failed to deploy to controlplane-updater lambda to staging ❌',
                            color: 'danger',
                            text: "Failed to deploy commit ${commitLink()}",
                            actions: [[
                            type: 'button',
                            text: 'Read build logs',
                            url: "${env.BUILD_URL}console"
                            ]]
                        ]])
                    }
                }
            }
        }
        stage('Deploy Production') {
            steps {
                input(message: "Deploy to production?", ok: 'Yes')
                withAWS(credentials: PROD_ACCT, region: AWS_REGION) {
                    sh "AWS_ACCT_ID=859517684765 ./cmd/controlplane-updater-lambda/deploy-controlplane-updater.sh"
                }
            }
            post {
                success {
                    script {
                        message([[
                            title: 'Deployed controlplane-updater lambda to production successfully ✔',
                            color: 'good',
                            text: "Successfully deployed commit ${commitLink()}"
                        ]])
                    }
                }
                failure {
                    script {
                        message([[
                            title: 'Failed to deploy controlplane-updater lambda to production ❌',
                            color: 'danger',
                            text: "Failed to deploy commit ${commitLink()}"
                        ]])
                    }
                }
            }
        }
    }
}
