Skip to content
Permalink
Browse files

[JENKINS-33309] Set environment variables from ChangeRequestAction.

Also make sure SCMHead.actions are exported to REST.
  • Loading branch information...
jglick committed Mar 8, 2016
1 parent 60c05e7 commit 1d5c9aeb17b8f92dbe24ad18a2498b93a4f944d6
Showing with 28 additions and 2 deletions.
  1. +1 −1 pom.xml
  2. +5 −0 src/main/java/jenkins/branch/Branch.java
  3. +22 −1 src/main/java/jenkins/branch/BranchNameContributor.java
@@ -87,7 +87,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>scm-api</artifactId>
<version>1.0</version>
<version>1.1-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
@@ -30,11 +30,14 @@
import java.util.concurrent.CopyOnWriteArrayList;
import jenkins.scm.api.SCMHead;
import jenkins.scm.impl.NullSCMSource;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;

/**
* A source code branch.
* Not to be subclassed outside this plugin.
*/
@ExportedBean
public class Branch {

/**
@@ -104,6 +107,7 @@ public String getEncodedName() {
*
* @return the {@link SCMHead of the branch}
*/
@Exported
public SCMHead getHead() {
return head;
}
@@ -113,6 +117,7 @@ public SCMHead getHead() {
*
* @return the {@link SCM} for the branch.
*/
@Exported
public SCM getScm() {
return scm;
}
@@ -31,9 +31,13 @@
import hudson.model.Job;
import hudson.model.TaskListener;
import java.io.IOException;
import java.net.URL;
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.actions.ChangeRequestAction;

/**
* Defines the environment variable {@code BRANCH_NAME} for multibranch builds.
* Also defines {@code CHANGE_*} variables when {@link ChangeRequestAction} is present.
*/
@Extension
public class BranchNameContributor extends EnvironmentContributor {
@@ -46,9 +50,26 @@ public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) thro
if (parent instanceof MultiBranchProject) {
BranchProjectFactory projectFactory = ((MultiBranchProject) parent).getProjectFactory();
if (projectFactory.isProject(j)) {
SCMHead head = projectFactory.getBranch(j).getHead();
// Note: not using Branch.name, since in the future that could be something different
// than SCMHead.name, which is what we really want here.
envs.put("BRANCH_NAME", projectFactory.getBranch(j).getHead().getName());
envs.put("BRANCH_NAME", head.getName());
ChangeRequestAction cr = head.getAction(ChangeRequestAction.class);
if (cr != null) {
envs.putIfNotNull("CHANGE_ID", cr.getId());
URL u = cr.getURL();
if (u != null) {
envs.put("CHANGE_URL", u.toString());
}
envs.putIfNotNull("CHANGE_TITLE", cr.getTitle());
envs.putIfNotNull("CHANGE_AUTHOR", cr.getAuthor());
envs.putIfNotNull("CHANGE_AUTHOR_DISPLAY_NAME", cr.getAuthorDisplayName());
envs.putIfNotNull("CHANGE_AUTHOR_EMAIL", cr.getAuthorEmail());
SCMHead target = cr.getTarget();
if (target != null) {
envs.put("CHANGE_TARGET", target.getName());
}
}
}
}
}

0 comments on commit 1d5c9ae

Please sign in to comment.
You can’t perform that action at this time.