Skip to content

Commit b0ea0fd

Browse files
authored
Merge pull request #21 from launchableinc/LCHUX-337
[LCHUX-337] Collect types of items leading up to the root
2 parents 83166e4 + 5cc0f3f commit b0ea0fd

File tree

3 files changed

+68
-34
lines changed

3 files changed

+68
-34
lines changed

pom.xml

+2-33
Original file line numberDiff line numberDiff line change
@@ -134,40 +134,9 @@
134134
</dependency>
135135
<dependency>
136136
<groupId>org.jenkins-ci.plugins</groupId>
137-
<artifactId>database</artifactId>
138-
<version>1.7</version>
137+
<artifactId>cloudbees-folder</artifactId>
138+
<version>6.0.4</version>
139139
<scope>test</scope>
140-
<exclusions>
141-
<exclusion> <!-- TODO move to bom in Jenkins core -->
142-
<groupId>antlr</groupId>
143-
<artifactId>antlr</artifactId>
144-
</exclusion>
145-
</exclusions>
146-
</dependency>
147-
<dependency>
148-
<groupId>org.jenkins-ci.plugins</groupId>
149-
<artifactId>database-h2</artifactId>
150-
<version>1.2</version>
151-
<scope>test</scope>
152-
</dependency>
153-
<dependency>
154-
<groupId>io.jenkins</groupId>
155-
<artifactId>configuration-as-code</artifactId>
156-
<version>${configuration-as-code.version}</version>
157-
<scope>test</scope>
158-
</dependency>
159-
<dependency>
160-
<groupId>io.jenkins.configuration-as-code</groupId>
161-
<artifactId>test-harness</artifactId>
162-
<version>${configuration-as-code.version}</version>
163-
<scope>test</scope>
164-
<!-- TODO bump version in jcasc -->
165-
<exclusions>
166-
<exclusion>
167-
<groupId>commons-validator</groupId>
168-
<artifactId>commons-validator</artifactId>
169-
</exclusion>
170-
</exclusions>
171140
</dependency>
172141
<!-- TODO https://github.com/jenkinsci/bom/pull/294 -->
173142
<dependency>

src/main/java/io/jenkins/plugins/launchable/PropsBuilder.java

+20-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package io.jenkins.plugins.launchable;
22

3+
import hudson.model.Item;
4+
import hudson.model.Job;
35
import hudson.model.Run;
6+
import net.sf.json.JSONArray;
47
import net.sf.json.JSONObject;
58

69
/**
@@ -28,6 +31,22 @@ protected JSONObject buildRunProperties() {
2831
protected JSONObject buildJobProperties() {
2932
return new JSONObject()
3033
.accumulate("fullName", run.getParent().getFullName())
31-
.accumulate("type", run.getParent().getClass().getName());
34+
.accumulate("type", run.getParent().getClass().getName())
35+
.accumulate("components", buildJobComponents(run.getParent()));
36+
}
37+
38+
private JSONArray buildJobComponents(Item i) {
39+
JSONArray a = new JSONArray();
40+
while (true) {
41+
a.add(0, new JSONObject()
42+
.accumulate("name", i.getName())
43+
.accumulate("type", i.getClass().getName()));
44+
45+
if (i.getParent() instanceof Item) {
46+
i = (Item) i.getParent();
47+
} else {
48+
return a;
49+
}
50+
}
3251
}
3352
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package io.jenkins.plugins.launchable;
2+
3+
import com.cloudbees.hudson.plugins.folder.Folder;
4+
import hudson.model.FreeStyleBuild;
5+
import hudson.model.FreeStyleProject;
6+
import net.sf.json.JSONObject;
7+
import org.hamcrest.MatcherAssert;
8+
import org.junit.Rule;
9+
import org.junit.Test;
10+
import org.jvnet.hudson.test.JenkinsRule;
11+
12+
import java.io.IOException;
13+
14+
import static org.hamcrest.CoreMatchers.*;
15+
import static org.junit.Assert.*;
16+
17+
public class PropBuilderTest {
18+
@Rule
19+
public JenkinsRule j = new JenkinsRule();
20+
21+
@Test
22+
public void basics() throws Exception {
23+
Folder folder1 = j.jenkins.createProject(Folder.class,"foo");
24+
FreeStyleProject project = folder1.createProject(FreeStyleProject.class,"bar");
25+
FreeStyleBuild build = project.scheduleBuild2(0).get();
26+
27+
JSONObject props = new PropsBuilder<>(build).build();
28+
MatcherAssert.assertThat(props, is(new JSONObject()
29+
.accumulate("job", new JSONObject()
30+
.accumulate("fullName", "foo/bar")
31+
.accumulate("type", FreeStyleProject.class.getName())
32+
.accumulate("components", new JSONObject[] {
33+
new JSONObject()
34+
.accumulate("name", "foo")
35+
.accumulate("type", Folder.class.getName()),
36+
new JSONObject()
37+
.accumulate("name", "bar")
38+
.accumulate("type", FreeStyleProject.class.getName())
39+
}))
40+
.accumulate("build", new JSONObject()
41+
.accumulate("number", 1)
42+
.accumulate("displayName", "#1")
43+
)
44+
));
45+
}
46+
}

0 commit comments

Comments
 (0)