From 46aa1d0a427ea1d7db350c4b4f22e9a5a9d1c385 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Mon, 17 Feb 2025 11:33:00 +0900 Subject: [PATCH 1/3] [LCHUX-337] Collect types of items leading up to the root This allows the Launchable backend to more intelligently deal with the multi-branch project --- .../plugins/launchable/PropsBuilder.java | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/src/main/java/io/jenkins/plugins/launchable/PropsBuilder.java b/src/main/java/io/jenkins/plugins/launchable/PropsBuilder.java index bdc0dc0..99af4c0 100644 --- a/src/main/java/io/jenkins/plugins/launchable/PropsBuilder.java +++ b/src/main/java/io/jenkins/plugins/launchable/PropsBuilder.java @@ -1,6 +1,9 @@ package io.jenkins.plugins.launchable; +import hudson.model.Item; +import hudson.model.Job; import hudson.model.Run; +import net.sf.json.JSONArray; import net.sf.json.JSONObject; /** @@ -28,6 +31,22 @@ protected JSONObject buildRunProperties() { protected JSONObject buildJobProperties() { return new JSONObject() .accumulate("fullName", run.getParent().getFullName()) - .accumulate("type", run.getParent().getClass().getName()); + .accumulate("type", run.getParent().getClass().getName()) + .accumulate("components", buildJobComponents(run.getParent())); + } + + private JSONArray buildJobComponents(Item i) { + JSONArray a = new JSONArray(); + while (true) { + a.add(0, new JSONObject() + .accumulate("name", i.getName()) + .accumulate("type", i.getClass().getName())); + + if (i.getParent() instanceof Item) { + i = (Item) i.getParent(); + } else { + return a; + } + } } } From 1eb939f791e0df323e7f95a239b0ccf97c7b24ba Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Mon, 17 Feb 2025 11:50:32 +0900 Subject: [PATCH 2/3] Removed a bunch of test scope dependencies that are unused Those are likely introduced by cut & paste error --- pom.xml | 35 ++--------------------------------- 1 file changed, 2 insertions(+), 33 deletions(-) diff --git a/pom.xml b/pom.xml index ddac389..5fe66e8 100644 --- a/pom.xml +++ b/pom.xml @@ -134,40 +134,9 @@ org.jenkins-ci.plugins - database - 1.7 + cloudbees-folder + 6.0.4 test - - - antlr - antlr - - - - - org.jenkins-ci.plugins - database-h2 - 1.2 - test - - - io.jenkins - configuration-as-code - ${configuration-as-code.version} - test - - - io.jenkins.configuration-as-code - test-harness - ${configuration-as-code.version} - test - - - - commons-validator - commons-validator - - From 5cc0f3f4401486842b34e8b64f0df9184f5a39b7 Mon Sep 17 00:00:00 2001 From: Kohsuke Kawaguchi Date: Mon, 17 Feb 2025 11:51:02 +0900 Subject: [PATCH 3/3] Added a test case --- .../plugins/launchable/PropBuilderTest.java | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 src/test/java/io/jenkins/plugins/launchable/PropBuilderTest.java diff --git a/src/test/java/io/jenkins/plugins/launchable/PropBuilderTest.java b/src/test/java/io/jenkins/plugins/launchable/PropBuilderTest.java new file mode 100644 index 0000000..02d35c3 --- /dev/null +++ b/src/test/java/io/jenkins/plugins/launchable/PropBuilderTest.java @@ -0,0 +1,46 @@ +package io.jenkins.plugins.launchable; + +import com.cloudbees.hudson.plugins.folder.Folder; +import hudson.model.FreeStyleBuild; +import hudson.model.FreeStyleProject; +import net.sf.json.JSONObject; +import org.hamcrest.MatcherAssert; +import org.junit.Rule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import java.io.IOException; + +import static org.hamcrest.CoreMatchers.*; +import static org.junit.Assert.*; + +public class PropBuilderTest { + @Rule + public JenkinsRule j = new JenkinsRule(); + + @Test + public void basics() throws Exception { + Folder folder1 = j.jenkins.createProject(Folder.class,"foo"); + FreeStyleProject project = folder1.createProject(FreeStyleProject.class,"bar"); + FreeStyleBuild build = project.scheduleBuild2(0).get(); + + JSONObject props = new PropsBuilder<>(build).build(); + MatcherAssert.assertThat(props, is(new JSONObject() + .accumulate("job", new JSONObject() + .accumulate("fullName", "foo/bar") + .accumulate("type", FreeStyleProject.class.getName()) + .accumulate("components", new JSONObject[] { + new JSONObject() + .accumulate("name", "foo") + .accumulate("type", Folder.class.getName()), + new JSONObject() + .accumulate("name", "bar") + .accumulate("type", FreeStyleProject.class.getName()) + })) + .accumulate("build", new JSONObject() + .accumulate("number", 1) + .accumulate("displayName", "#1") + ) + )); + } +}