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 - - 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; + } + } } } 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") + ) + )); + } +}