Enable running product builds on PluginRefUpdatedEvent

Product builds until now are triggered on PluginChangeMergedEvent,
resulting in one build per submitted Gerrit change if multiple
changes are submitted at once ("Submit including parents").

Triggering on PluginRefUpdatedEvent should trigger only one build
for the whole change series.

The GerritTrigger plug-in in Jenkins sets different environment
variables for these two cases. Adapt the pipeline scripts so they
can work with either.

Bug: 552963
Change-Id: Iea3cd90c5f77dc4d1f6e169b92a72de714bb5e23
Signed-off-by: Thomas Wolf <thomas.wolf@paranor.ch>
diff --git a/src/org/eclipse/egit/jenkins/Lib.groovy b/src/org/eclipse/egit/jenkins/Lib.groovy
index 9d975da..b0e8e27 100644
--- a/src/org/eclipse/egit/jenkins/Lib.groovy
+++ b/src/org/eclipse/egit/jenkins/Lib.groovy
@@ -165,13 +165,15 @@
 	/**
 	 * Determines the folder to publish to.
 	 *
+	 * @param branch
+	 * 		branch we're building
 	 * @param ownVersion
 	 * 		version of the project being built
 	 * @return The subfolder name to publish the p2 repo to
 	 */
-	def String getPublishFolder(String ownVersion) {
+	def String getPublishFolder(String branch, String ownVersion) {
 		if (ownVersion.endsWith('-SNAPSHOT')) {
-			if (script.env.GERRIT_BRANCH.contains('master')) {
+			if (branch.contains('master')) {
 				return 'updates-nightly';
 			} else {
 				// We only ever build the last release or master, so two directories are sufficient
diff --git a/src/org/eclipse/egit/jenkins/Tools.groovy b/src/org/eclipse/egit/jenkins/Tools.groovy
index a52bba5..fd2afe7 100644
--- a/src/org/eclipse/egit/jenkins/Tools.groovy
+++ b/src/org/eclipse/egit/jenkins/Tools.groovy
@@ -46,7 +46,7 @@
 			]
 		}
 		if (branches instanceof String) {
-			branchSpecs.add([$class: "Branch", compareType: "PLAIN", pattern: b])
+			branchSpecs.add([$class: "Branch", compareType: "PLAIN", pattern: branches])
 		} else {
 			for (b in branches) {
 				if (b instanceof Collection && b.size() == 2) {
diff --git a/vars/productBuild.groovy b/vars/productBuild.groovy
index 0841f48..83c2324 100644
--- a/vars/productBuild.groovy
+++ b/vars/productBuild.groovy
@@ -13,7 +13,7 @@
 
 /**
  * EGit product build for a given branch, either determined by the gerrit trigger's
- * $GERRIT_BRANCH or the {@code cfg.defaultBranch}.
+ * $GERRIT_BRANCH or $GERRIT_REFNAME or the {@code cfg.defaultBranch}.
  *
  * @param lib
  * 		library to use
@@ -29,7 +29,7 @@
 	lib.configCheck(config, [
 		timeOut : 'Job timeout in minutes, default 60',
 		repoPath : 'Full path to the repository to build, for instance "egit/egit".',
-		// defaultBranch is optional: branch to build if $GERRIT_BRANCH is not set
+		// defaultBranch is optional: branch to build if $GERRIT_BRANCH and $GERRIT_REFNAME are not set
 		upstreamRepoPath: 'Path to the upstream repo, for instance the first "jgit" for "jgit/jgit".',
 		upstreamRepo: 'Upstream repository name, for instance the second "jgit" for "jgit/jgit".',
 		// upstreamVersion is optional; auto-determined if not set
@@ -40,8 +40,18 @@
 	])
 	uiNode(config.timeOut) {
 		try {
-			if (!env.GERRIT_BRANCH) {
-				env.GERRIT_BRANCH = config.defaultBranch
+			// If run on a RefChanged event, GERRIT_BRANCH is not set, but GERRIT_REFNAME is.
+			// If run on a ChangeMerged event, GERRIT_BRANCH is set, but GERRIT_REFNAME is not.
+			def branchToBuild = null
+			if (!env.GERRIT_BRANCH && !env.GERRIT_REFNAME) {
+				branchToBuild = config.defaultBranch
+			} else if (!!env.GERRIT_BRANCH) {
+				branchToBuild = env.GERRIT_BRANCH
+			} else if (!!env.GERRIT_REFNAME) {
+				branchToBuild = env.GERRIT_REFNAME
+			}
+			if (!branchToBuild) {
+				branchToBuild = config.defaultBranch
 			}
 			if (config.jdk) {
 				def jdk = tool name: "${config.jdk}", type: 'jdk'
@@ -49,10 +59,10 @@
 			}
 			stage('Checkout') {
 				sh '$JAVA_HOME/bin/java -version'
-				tooling.cloneAndCheckout(config.repoPath, env.GERRIT_BRANCH, '+refs/heads/*:refs/remotes/origin/*');
+				tooling.cloneAndCheckout(config.repoPath, branchToBuild, '+refs/heads/*:refs/remotes/origin/*');
 			}
 			def ownVersion = lib.getOwnVersion('pom.xml')
-			def publishFolder = "/${config.publishRoot}/" + lib.getPublishFolder(ownVersion)
+			def publishFolder = "/${config.publishRoot}/" + lib.getPublishFolder(branchToBuild, ownVersion)
 			def publishDirectory = '/home/data/httpd/download.eclipse.org' + publishFolder
 			def upstreamVersion = config.upstreamVersion
 			if (!upstreamVersion) {