Skip to content

Commit

Permalink
Merge pull request #236 from cwholmes/is-primary-env
Browse files Browse the repository at this point in the history
Add is primary environment to branch name contributor
  • Loading branch information
bitwiseman committed Feb 8, 2021
2 parents ae0091e + b0bb4ce commit e39a004
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 4 deletions.
8 changes: 7 additions & 1 deletion src/main/java/jenkins/branch/BranchNameContributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,13 @@
import jenkins.scm.api.SCMHead;
import jenkins.scm.api.metadata.ContributorMetadataAction;
import jenkins.scm.api.metadata.ObjectMetadataAction;
import jenkins.scm.api.metadata.PrimaryInstanceMetadataAction;
import jenkins.scm.api.mixin.ChangeRequestSCMHead;
import jenkins.scm.api.mixin.ChangeRequestSCMHead2;
import jenkins.scm.api.mixin.TagSCMHead;

/**
* Defines the environment variable {@code BRANCH_NAME} for multibranch builds.
* Defines the environment variable {@code BRANCH_NAME} and {@code BRANCH_IS_PRIMARY} for multibranch builds.
* Also defines {@code CHANGE_*} variables for {@link ChangeRequestSCMHead} instances and
* {@code TAG_*} variables for {@link TagSCMHead} instances.
*/
Expand All @@ -61,6 +62,11 @@ public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) thro
// 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", head.getName());
if (branch.getAction(PrimaryInstanceMetadataAction.class) != null) {
envs.put("BRANCH_IS_PRIMARY", "true");
} else {
envs.put("BRANCH_IS_PRIMARY", "false");
}
if (head instanceof ChangeRequestSCMHead) {
envs.putIfNotNull("CHANGE_ID", ((ChangeRequestSCMHead) head).getId());
SCMHead target = ((ChangeRequestSCMHead) head).getTarget();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ THE SOFTWARE.
<t:buildEnvVar name="BRANCH_NAME">
<j:out value="${%blurb.BRANCH_NAME}"/>
</t:buildEnvVar>
<t:buildEnvVar name="BRANCH_IS_PRIMARY">
<j:out value="${%blurb.BRANCH_IS_PRIMARY}"/>
</t:buildEnvVar>
<t:buildEnvVar name="CHANGE_ID">
<j:out value="${%blurb.CHANGE_ID}"/>
</t:buildEnvVar>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
# THE SOFTWARE.

blurb.BRANCH_NAME=For a multibranch project, this will be set to the name of the branch being built, for example in case you wish to deploy to production from <code>master</code> but not from feature branches; if corresponding to some kind of change request, the name is generally arbitrary (refer to <code>CHANGE_ID</code> and <code>CHANGE_TARGET</code>).
blurb.BRANCH_IS_PRIMARY=For a multibranch project, if the SCM source reports that the branch being built is a primary branch, this will be set to <code>true</code>, otherwise this will be set to <code>false</code>. Some SCM sources may report more than one branch as a primary branch while others may not supply this information.
blurb.CHANGE_ID=For a multibranch project corresponding to some kind of change request, this will be set to the change ID, such as a pull request number, if supported; else unset.
blurb.CHANGE_URL=For a multibranch project corresponding to some kind of change request, this will be set to the change URL, if supported; else unset.
blurb.CHANGE_TITLE=For a multibranch project corresponding to some kind of change request, this will be set to the title of the change, if supported; else unset.
Expand Down
25 changes: 22 additions & 3 deletions src/test/java/jenkins/branch/BranchNameContributorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import hudson.model.TopLevelItem;
import hudson.util.LogTaskListener;
import integration.harness.BasicMultiBranchProject;
import java.util.Collection;
import java.util.Collections;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand Down Expand Up @@ -76,33 +75,39 @@ public void buildEnvironmentFor() throws Exception {
assertThat("The extension is registered", instance, notNullValue());
try (MockSCMController c = MockSCMController.create()) {
c.createRepository("foo", MockRepositoryFlags.FORKABLE);
c.setPrimaryBranch("foo", "main");
Integer cr1Num = c.openChangeRequest("foo", "master");
Integer cr2Num = c.openChangeRequest("foo", "master", MockChangeRequestFlags.FORK);
c.createTag("foo", "master", "v1.0");
c.createBranch("foo", "main");
BasicMultiBranchProject prj = r.jenkins.createProject(BasicMultiBranchProject.class, "foo");
prj.setCriteria(null);
prj.getSourcesList().add(new BranchSource(new MockSCMSource(c, "foo", new MockSCMDiscoverBranches(), new MockSCMDiscoverTags(), new MockSCMDiscoverChangeRequests())));
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
assertThat("We now have branches",
prj.getItems(), not(is((Collection<FreeStyleProject>) Collections.<FreeStyleProject>emptyList())));
prj.getItems(), not(is(Collections.<FreeStyleProject>emptyList())));
FreeStyleProject master = prj.getItem("master");
FreeStyleProject cr1 = prj.getItem("CR-" + cr1Num);
FreeStyleProject cr2 = prj.getItem("CR-" + cr2Num);
FreeStyleProject tag = prj.getItem("v1.0");
FreeStyleProject primaryBranch = prj.getItem("main");
assertThat("We now have the master branch", master, notNullValue());
assertThat("We now have the origin CR branch", cr1, notNullValue());
assertThat("We now have the form CR branch", cr2, notNullValue());
assertThat("We now have the tag branch", tag, notNullValue());
assertThat("We now have the primary branch", primaryBranch, notNullValue());
EnvVars env = new EnvVars();
instance.buildEnvironmentFor(master, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), contains(is("BRANCH_NAME")));
assertThat(env.keySet(), containsInAnyOrder(is("BRANCH_NAME"), is("BRANCH_IS_PRIMARY")));
assertThat(env.get("BRANCH_NAME"), is("master"));
assertThat(env.get("BRANCH_IS_PRIMARY"), is("false"));

env = new EnvVars();
instance.buildEnvironmentFor(cr1, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("BRANCH_NAME"),
is("BRANCH_IS_PRIMARY"),
is("CHANGE_ID"),
is("CHANGE_TARGET"),
is("CHANGE_TITLE"),
Expand All @@ -113,6 +118,7 @@ public void buildEnvironmentFor() throws Exception {
is("CHANGE_AUTHOR_DISPLAY_NAME")
));
assertThat(env.get("BRANCH_NAME"), is("CR-" + cr1Num));
assertThat(env.get("BRANCH_IS_PRIMARY"), is("false"));
assertThat(env.get("CHANGE_ID"), is(cr1Num.toString()));
assertThat(env.get("CHANGE_TARGET"), is("master"));
assertThat(env.get("CHANGE_BRANCH"), is("CR-" + cr1Num));
Expand All @@ -126,6 +132,7 @@ public void buildEnvironmentFor() throws Exception {
instance.buildEnvironmentFor(cr2, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("BRANCH_NAME"),
is("BRANCH_IS_PRIMARY"),
is("CHANGE_ID"),
is("CHANGE_TARGET"),
is("CHANGE_TITLE"),
Expand All @@ -137,6 +144,7 @@ public void buildEnvironmentFor() throws Exception {
is("CHANGE_AUTHOR_DISPLAY_NAME")
));
assertThat(env.get("BRANCH_NAME"), is("CR-" + cr2Num));
assertThat(env.get("BRANCH_IS_PRIMARY"), is("false"));
assertThat(env.get("CHANGE_ID"), is(cr2Num.toString()));
assertThat(env.get("CHANGE_TARGET"), is("master"));
assertThat(env.get("CHANGE_BRANCH"), is("CR-" + cr2Num));
Expand All @@ -151,16 +159,27 @@ public void buildEnvironmentFor() throws Exception {
instance.buildEnvironmentFor(tag, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("BRANCH_NAME"),
is("BRANCH_IS_PRIMARY"),
is("TAG_NAME"),
is("TAG_TIMESTAMP"),
is("TAG_UNIXTIME"),
is("TAG_DATE")
));
assertThat(env.get("BRANCH_NAME"), is("v1.0"));
assertThat(env.get("BRANCH_IS_PRIMARY"), is("false"));
assertThat(env.get("TAG_NAME"), is("v1.0"));
assertThat(env.get("TAG_TIMESTAMP"), not(is("")));
assertThat(env.get("TAG_UNIXTIME"), not(is("")));
assertThat(env.get("TAG_DATE"), not(is("")));

env = new EnvVars();
instance.buildEnvironmentFor(primaryBranch, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("BRANCH_NAME"),
is("BRANCH_IS_PRIMARY")
));
assertThat(env.get("BRANCH_NAME"), is("main"));
assertThat(env.get("BRANCH_IS_PRIMARY"), is("true"));
}
}

Expand Down

0 comments on commit e39a004

Please sign in to comment.