@Library('nf-jenkins-shared-libraries')_
import jenkins.model.Jenkins
import org.apache.commons.lang3.StringUtils;
import hudson.model.*

repo = "admin-gui"
channel = "cd-ui-staging"
dep_environment = "sandbox"

failure_msg = ""
stage_name = ""
link_url = ""

BRANCH_NAME="develop"

def CF_DISTRIBUTION =  [ 'sandbox': 'E2XXWR5XCP9VOX', 'staging': 'ESJT33GN4NMB1', 'production': 'E2PDVWYFH4TD0H' ]

pipeline {
    agent {
        ecs {
            inheritFrom 'agent-nodejs10'
            cpu 4096
            memoryReservation 8192
        }
    }

    environment {
        BITBUCKET_AUTH = credentials('bitbucket_auth')
        BITBUCKET_ADMIN = credentials('bitbucket_admin')
    }

    stages {
        stage('Prepare') {
            steps {
                script {
                    stage_name = env.STAGE_NAME
                    author_email = fetch_author_email("${repo}", "${GIT_COMMIT}")
                    bitbucketStatus repo: "${repo}", BRANCH_NAME: "${BRANCH_NAME}", GIT_COMMIT: "${GIT_COMMIT}", PR_CHECK: "false", status: "INPROGRESS"
                    sh "npm install"
                    sh "node version.js"
                    sh "npm run lint"
                    sh "npm run test"
                    sh "npm run build-aot"
                }
            }
        }

        stage('Deployment to Sandox') {
            steps {
                script {
                    stage_name = env.STAGE_NAME
                    try{
                        dep_environment = "sandbox"
                        sh "npm install"
                        sh "node version.js"
                        sh "npm test"
                        sh "node --max_old_space_size=12000 ./node_modules/.bin/ng build --configuration=sandbox --prod --aot"
                    
                        archiveArtifacts "dist/**"
                    } catch(e){
                        failure_msg = "Build Failed for branch ${BRANCH_NAME} during Deployment to Sandox"
                        error "${failure_msg}"
                    }

                    adminGUIDeploy("${dep_environment}", "${repo}", CF_DISTRIBUTION, "${GIT_COMMIT}")

                    sh "rm -rf coverage"
                    sh "rm -rf dist"
                    sh "rm -rf node_modules"

                    //notification
                    fn_msg = "Merged changes are now deployed to ${dep_environment}, commit_hash: ${GIT_COMMIT}"
                    send_custom_notification("${author_email}", "${repo}", "${channel}", "${fn_msg}", "good", " ")
                }
            }
        }

        stage('Promote to Staging') {
            steps {
                script {
                    stage_name = env.STAGE_NAME
                    try{
                        dep_environment = "staging"
                        currentBuild.description = "Promoting Build - Environment: ${dep_environment} REPO: ${repo} Hash: ${GIT_COMMIT}"

                        sh "npm install"
                        sh "node version.js"
                        sh "node --max_old_space_size=8000 ./node_modules/.bin/ng build --configuration=staging --prod --aot"
                    
                        archiveArtifacts "dist/**"
                    } catch(e){
                        failure_msg = "Build Failed for branch ${BRANCH_NAME} during Promote to Staging"
                        error "${failure_msg}"
                    }

                    adminGUIDeploy("${dep_environment}", "${repo}", CF_DISTRIBUTION, "${GIT_COMMIT}")

                    sh "rm -rf coverage"
                    sh "rm -rf dist"
                    sh "rm -rf node_modules"

                    //notification
                    fn_msg = "Merged changes are now deployed to ${dep_environment}, commit_hash: ${GIT_COMMIT}"
                    send_custom_notification("${author_email}", "${repo}", "${channel}", "${fn_msg}", "good", " ")
                }
            }
        }
    }

	post {
        always {
            script{
                sh "ls -A1 | xargs rm -r"
            }
        }
        aborted {
            bitbucketStatus repo: "${repo}", BRANCH_NAME: BRANCH_NAME, GIT_COMMIT: GIT_COMMIT, PR_CHECK: "false", status: "STOPPED"
        }
        failure {
            script{
                bitbucketStatus repo: "${repo}", BRANCH_NAME: BRANCH_NAME, GIT_COMMIT: GIT_COMMIT, PR_CHECK: "false", status: "FAILED"
                mattermostFailureNotification repo: "${repo}", commit: GIT_COMMIT, build_url: RUN_DISPLAY_URL, channel: "${channel}", link_url: "${link_url}", reason: "Deployment to ${dep_environment} failed at stage ${stage_name}. ${failure_msg}. Please check link for details."
            }
        }
        success {
            script{
                bitbucketStatus repo: "${repo}", BRANCH_NAME: BRANCH_NAME, GIT_COMMIT: GIT_COMMIT, PR_CHECK: "false", status: "SUCCESSFUL"
            }
        }
    }
}
