Skip to content

Commit

Permalink
add in FOLDER_NAME for the base job of a multibranch project
Browse files Browse the repository at this point in the history
  • Loading branch information
iwarapter committed Nov 11, 2022
1 parent 32f0861 commit f7c1333
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/main/java/jenkins/branch/BranchNameContributor.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ public void buildEnvironmentFor(Job j, EnvVars envs, TaskListener listener) thro
SCMHead head = branch.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("FOLDER_NAME", parent.getFullName());
envs.put("BRANCH_NAME", head.getName());
if (branch.getAction(PrimaryInstanceMetadataAction.class) != null) {
envs.put("BRANCH_IS_PRIMARY", "true");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,7 @@ THE SOFTWARE.
<t:buildEnvVar name="TAG_DATE">
<j:out value="${%blurb.TAG_DATE}"/>
</t:buildEnvVar>
<t:buildEnvVar name="FOLDER_NAME">
<j:out value="${%blurb.FOLDER_NAME}"/>
</t:buildEnvVar>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,4 @@ blurb.TAG_NAME=For a multibranch project corresponding to some kind of tag, this
blurb.TAG_TIMESTAMP=For a multibranch project corresponding to some kind of tag, this will be set to a timestamp of the tag in milliseconds since Unix epoch, if supported; else unset.
blurb.TAG_UNIXTIME=For a multibranch project corresponding to some kind of tag, this will be set to a timestamp of the tag in seconds since Unix epoch, if supported; else unset.
blurb.TAG_DATE=For a multibranch project corresponding to some kind of tag, this will be set to a timestamp in the format as defined by <a href="https://docs.oracle.com/javase/7/docs/api/java/util/Date.html#toString()" target="_blank">java.util.Date#toString()</a> (e.g., Wed Jan 1 00:00:00 UTC 2020), if supported; else unset.
blurb.FOLDER_NAME=For a multibranch project, this will be set to the name of the multibranch project.
1 change: 1 addition & 0 deletions src/test/java/integration/EnvironmentTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void given_multibranch_when_buildingABranch_then_environmentContainsBranc
prj.scheduleBuild2(0).getFuture().get();
r.waitUntilNoActivity();
assertThat(r.getLog(prj.getItem("master").getBuildByNumber(1)), containsString("BRANCH_NAME=master"));
assertThat(r.getLog(prj.getItem("master").getBuildByNumber(1)), containsString("FOLDER_NAME=foo"));
}
}

Expand Down
6 changes: 5 additions & 1 deletion src/test/java/jenkins/branch/BranchNameContributorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ public void buildEnvironmentFor() throws Exception {
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(), contains(is("BRANCH_NAME"), is("FOLDER_NAME")));
assertThat(env.get("BRANCH_NAME"), is("master"));
assertThat(env.keySet(), not(contains(is("BRANCH_IS_PRIMARY"))));

env = new EnvVars();
instance.buildEnvironmentFor(cr1, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("FOLDER_NAME"),
is("BRANCH_NAME"),
is("CHANGE_ID"),
is("CHANGE_TARGET"),
Expand All @@ -130,6 +131,7 @@ public void buildEnvironmentFor() throws Exception {
env = new EnvVars();
instance.buildEnvironmentFor(cr2, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("FOLDER_NAME"),
is("BRANCH_NAME"),
is("CHANGE_ID"),
is("CHANGE_TARGET"),
Expand All @@ -156,6 +158,7 @@ public void buildEnvironmentFor() throws Exception {
env = new EnvVars();
instance.buildEnvironmentFor(tag, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("FOLDER_NAME"),
is("BRANCH_NAME"),
is("TAG_NAME"),
is("TAG_TIMESTAMP"),
Expand All @@ -172,6 +175,7 @@ public void buildEnvironmentFor() throws Exception {
env = new EnvVars();
instance.buildEnvironmentFor(primaryBranch, env, new LogTaskListener(LOGGER, Level.FINE));
assertThat(env.keySet(), containsInAnyOrder(
is("FOLDER_NAME"),
is("BRANCH_NAME"),
is("BRANCH_IS_PRIMARY")
));
Expand Down

0 comments on commit f7c1333

Please sign in to comment.