From f7c1333f0a22f968b1f89f80e178f0d89da6b0fb Mon Sep 17 00:00:00 2001 From: Iain Adams Date: Fri, 11 Nov 2022 18:11:59 +0000 Subject: [PATCH] add in FOLDER_NAME for the base job of a multibranch project --- src/main/java/jenkins/branch/BranchNameContributor.java | 1 + .../jenkins/branch/BranchNameContributor/buildEnv.jelly | 3 +++ .../branch/BranchNameContributor/buildEnv.properties | 1 + src/test/java/integration/EnvironmentTest.java | 1 + src/test/java/jenkins/branch/BranchNameContributorTest.java | 6 +++++- 5 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/main/java/jenkins/branch/BranchNameContributor.java b/src/main/java/jenkins/branch/BranchNameContributor.java index b287e4c5..c9abb754 100644 --- a/src/main/java/jenkins/branch/BranchNameContributor.java +++ b/src/main/java/jenkins/branch/BranchNameContributor.java @@ -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"); diff --git a/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly b/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly index af21a5c2..cd72b053 100644 --- a/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly +++ b/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.jelly @@ -70,4 +70,7 @@ THE SOFTWARE. + + + diff --git a/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties b/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties index 4c3cc6ff..84fdd090 100644 --- a/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties +++ b/src/main/resources/jenkins/branch/BranchNameContributor/buildEnv.properties @@ -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 java.util.Date#toString() (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. diff --git a/src/test/java/integration/EnvironmentTest.java b/src/test/java/integration/EnvironmentTest.java index a8b536b8..af22d6c6 100644 --- a/src/test/java/integration/EnvironmentTest.java +++ b/src/test/java/integration/EnvironmentTest.java @@ -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")); } } diff --git a/src/test/java/jenkins/branch/BranchNameContributorTest.java b/src/test/java/jenkins/branch/BranchNameContributorTest.java index 5870927d..216f07ff 100644 --- a/src/test/java/jenkins/branch/BranchNameContributorTest.java +++ b/src/test/java/jenkins/branch/BranchNameContributorTest.java @@ -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"), @@ -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"), @@ -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"), @@ -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") ));