Skip to content

Commit

Permalink
[JENKINS-33309] Set environment variables from ChangeRequestAction.
Browse files Browse the repository at this point in the history
Also make sure SCMHead.actions are exported to REST.
  • Loading branch information
jglick committed Mar 8, 2016
1 parent 60c05e7 commit 1d5c9ae
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Expand Up @@ -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>
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/jenkins/branch/Branch.java
Expand Up @@ -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 {

/**
Expand Down Expand Up @@ -104,6 +107,7 @@ public String getEncodedName() {
*
* @return the {@link SCMHead of the branch}
*/
@Exported
public SCMHead getHead() {
return head;
}
Expand All @@ -113,6 +117,7 @@ public SCMHead getHead() {
*
* @return the {@link SCM} for the branch.
*/
@Exported
public SCM getScm() {
return scm;
}
Expand Down
23 changes: 22 additions & 1 deletion src/main/java/jenkins/branch/BranchNameContributor.java
Expand Up @@ -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 {
Expand All @@ -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());
}
}
}
}
}
Expand Down

0 comments on commit 1d5c9ae

Please sign in to comment.