#!groovy


def push_build() {
	//Don't use the build_stage function which outputs the compiler
	stage 'Push'

		status_prefix = 'Push'

		def out_dir = pwd tmp: true
		sh "mkdir -p ${out_dir}"

		//parse git logs to find what changed
		sh "git remote > ${out_dir}/GIT_REMOTE"
		git_remote = readFile("${out_dir}/GIT_REMOTE")
		remoteDoLangExists = git_remote.contains("DoLang")

		if( !remoteDoLangExists ) {
			sh 'git remote add DoLang git@gitlab.do-lang.org:internal/cfa-cc.git'
		}

		sh "git push DoLang ${gitRefNewValue}:master"
}


//Email notification on a full build failure
def promote_email(String status) {
	//Since tokenizer doesn't work, figure stuff out from the environnement variables and command line
	//Configurations for email format
	def email_subject = "[cforall git][PROMOTE - FAILURE]"
	def email_body = """This is an automated email from the Jenkins build machine. It was
generated because of a git hooks/post-receive script following
a ref change was pushed to the repository containing
the project "UNNAMED PROJECT".

Check console output at ${env.BUILD_URL} to view the results.

- Status --------------------------------------------------------------

PROMOTE FAILURE - ${status}
"""

	def email_to = "pabuhr@uwaterloo.ca, rschlunt@uwaterloo.ca, a3moss@uwaterloo.ca, tdelisle@uwaterloo.ca, brice.dobry@huawei.com"

	//send email notification
	emailext body: email_body, subject: email_subject, to: email_to, attachLog: true
}
