Skip to content

Commit 3918371

Browse files
authored
Merge pull request #18 from sghill/add-api
Add api support to PluginUsageView
2 parents d028ada + d329d22 commit 3918371

File tree

12 files changed

+244
-0
lines changed

12 files changed

+244
-0
lines changed

pom.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,13 @@
5252
<version>3.0</version>
5353
<optional>true</optional>
5454
</dependency>
55+
56+
<dependency>
57+
<groupId>com.google.code.gson</groupId>
58+
<artifactId>gson</artifactId>
59+
<version>2.8.6</version>
60+
<scope>test</scope>
61+
</dependency>
5562
</dependencies>
5663

5764
<dependencyManagement>

src/main/java/org/jenkinsci/plugins/pluginusage/JobsPerPlugin.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@
22

33
import hudson.PluginWrapper;
44
import hudson.model.Job;
5+
import org.kohsuke.stapler.export.Exported;
6+
import org.kohsuke.stapler.export.ExportedBean;
57

68
import java.util.ArrayList;
79
import java.util.Comparator;
810
import java.util.HashMap;
911
import java.util.List;
1012
import java.util.Map;
1113

14+
@ExportedBean
1215
public class JobsPerPlugin {
1316

1417
private PluginWrapper plugin;
@@ -23,6 +26,7 @@ public void addProject(Job project) {
2326
this.jobMap.put(project.getFullDisplayName(), project);
2427
}
2528

29+
@Exported
2630
public List<Job> getProjects() {
2731
ArrayList<Job> projects = new ArrayList<Job>();
2832
projects.addAll(jobMap.values());
@@ -42,6 +46,7 @@ public int getNumberOfJobs() {
4246
return jobMap.size();
4347
}
4448

49+
@Exported
4550
public PluginWrapper getPlugin()
4651
{
4752
return plugin;

src/main/java/org/jenkinsci/plugins/pluginusage/PluginUsageModel.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,13 @@
66

77
import hudson.PluginWrapper;
88
import org.jenkinsci.plugins.pluginusage.analyzer.JobCollector;
9+
import org.kohsuke.stapler.export.Exported;
10+
import org.kohsuke.stapler.export.ExportedBean;
911

12+
@ExportedBean
1013
public class PluginUsageModel {
1114

15+
@Exported
1216
public List<JobsPerPlugin> getJobsPerPlugin() {
1317
ArrayList<JobsPerPlugin> list = new ArrayList<JobsPerPlugin>();
1418
list.addAll(new JobCollector().getJobsPerPlugin().values());

src/main/java/org/jenkinsci/plugins/pluginusage/PluginUsageView.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jenkinsci.plugins.pluginusage;
22

33
import hudson.Extension;
4+
import hudson.model.Api;
45
import hudson.model.RootAction;
56

67
@Extension
@@ -23,4 +24,7 @@ public PluginUsageModel getData() {
2324
return pluginUsageModel;
2425
}
2526

27+
public Api getApi() {
28+
return new Api(getData());
29+
}
2630
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package org.jenkinsci.plugins.pluginusage;
2+
3+
import com.google.common.collect.Lists;
4+
import com.google.common.io.Resources;
5+
import com.google.gson.Gson;
6+
import org.jenkinsci.plugins.pluginusage.api.Plugin;
7+
import org.jenkinsci.plugins.pluginusage.api.PluginProjects;
8+
import org.jenkinsci.plugins.pluginusage.api.PluginUsage;
9+
import org.jenkinsci.plugins.pluginusage.api.Project;
10+
import org.junit.Rule;
11+
import org.junit.Test;
12+
import org.jvnet.hudson.test.JenkinsRule;
13+
import org.jvnet.hudson.test.recipes.WithPlugin;
14+
15+
import java.io.File;
16+
import java.io.FileInputStream;
17+
import java.net.URL;
18+
import java.util.List;
19+
20+
import static org.junit.Assert.assertEquals;
21+
22+
public class PluginUsageViewApiTest {
23+
private final Gson gson = new Gson();
24+
25+
@Rule
26+
public JenkinsRule j = new JenkinsRule();
27+
28+
@Test
29+
@WithPlugin({"structs-1.17.hpi", "ant-1.9.hpi"})
30+
public void shouldRenderJson() throws Exception {
31+
// given two jobs are using the ant builder
32+
URL configUsingAnt = Resources.getResource("with-ant.xml");
33+
File config = new File(configUsingAnt.toURI());
34+
List<String> jobsUsingAnt = Lists.newArrayList("a", "b");
35+
for (String job : jobsUsingAnt) {
36+
try (FileInputStream is = new FileInputStream(config)) {
37+
j.jenkins.createProjectFromXML(job, is);
38+
}
39+
}
40+
// and one job is not
41+
j.createFreeStyleProject("c");
42+
PluginUsage expected = new PluginUsage(Lists.newArrayList(
43+
new PluginProjects(new Plugin("ant", "1.9"), Lists.newArrayList(
44+
new Project("a"),
45+
new Project("b")
46+
))
47+
));
48+
49+
// when the api is fetched
50+
String body = j.getJSON("pluginusage/api/json?depth=2").getContentAsString();
51+
PluginUsage actual = gson.fromJson(body, PluginUsage.class);
52+
53+
// then
54+
assertEquals(expected, actual);
55+
}
56+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.jenkinsci.plugins.pluginusage.api;
2+
3+
import java.util.Objects;
4+
import java.util.StringJoiner;
5+
6+
public class Plugin {
7+
String shortName;
8+
String version;
9+
10+
public Plugin() {
11+
}
12+
13+
public Plugin(String shortName, String version) {
14+
this.shortName = shortName;
15+
this.version = version;
16+
}
17+
18+
@Override
19+
public boolean equals(Object o) {
20+
if (this == o) return true;
21+
if (o == null || getClass() != o.getClass()) return false;
22+
Plugin plugin = (Plugin) o;
23+
return Objects.equals(shortName, plugin.shortName) && Objects.equals(version, plugin.version);
24+
}
25+
26+
@Override
27+
public int hashCode() {
28+
return Objects.hash(shortName, version);
29+
}
30+
31+
@Override
32+
public String toString() {
33+
return new StringJoiner(", ", Plugin.class.getSimpleName() + "[", "]")
34+
.add("shortName='" + shortName + "'")
35+
.add("version='" + version + "'")
36+
.toString();
37+
}
38+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package org.jenkinsci.plugins.pluginusage.api;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
import java.util.StringJoiner;
6+
7+
public class PluginProjects {
8+
Plugin plugin;
9+
List<Project> projects;
10+
11+
public PluginProjects() {
12+
}
13+
14+
public PluginProjects(Plugin plugin, List<Project> projects) {
15+
this.plugin = plugin;
16+
this.projects = projects;
17+
}
18+
19+
@Override
20+
public boolean equals(Object o) {
21+
if (this == o) return true;
22+
if (o == null || getClass() != o.getClass()) return false;
23+
PluginProjects that = (PluginProjects) o;
24+
return Objects.equals(plugin, that.plugin) && Objects.equals(projects, that.projects);
25+
}
26+
27+
@Override
28+
public int hashCode() {
29+
return Objects.hash(plugin, projects);
30+
}
31+
32+
@Override
33+
public String toString() {
34+
return new StringJoiner(", ", PluginProjects.class.getSimpleName() + "[", "]")
35+
.add("plugin=" + plugin)
36+
.add("projects=" + projects)
37+
.toString();
38+
}
39+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package org.jenkinsci.plugins.pluginusage.api;
2+
3+
import java.util.List;
4+
import java.util.Objects;
5+
import java.util.StringJoiner;
6+
7+
public class PluginUsage {
8+
private List<PluginProjects> jobsPerPlugin;
9+
10+
public PluginUsage() {
11+
}
12+
13+
public PluginUsage(List<PluginProjects> jobsPerPlugin) {
14+
this.jobsPerPlugin = jobsPerPlugin;
15+
}
16+
17+
@Override
18+
public boolean equals(Object o) {
19+
if (this == o) return true;
20+
if (o == null || getClass() != o.getClass()) return false;
21+
PluginUsage that = (PluginUsage) o;
22+
return Objects.equals(jobsPerPlugin, that.jobsPerPlugin);
23+
}
24+
25+
@Override
26+
public int hashCode() {
27+
return Objects.hash(jobsPerPlugin);
28+
}
29+
30+
@Override
31+
public String toString() {
32+
return new StringJoiner(", ", PluginUsage.class.getSimpleName() + "[", "]")
33+
.add("jobsPerPlugin=" + jobsPerPlugin)
34+
.toString();
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package org.jenkinsci.plugins.pluginusage.api;
2+
3+
import java.util.Objects;
4+
import java.util.StringJoiner;
5+
6+
public class Project {
7+
String fullName;
8+
9+
public Project() {
10+
}
11+
12+
public Project(String fullName) {
13+
this.fullName = fullName;
14+
}
15+
16+
@Override
17+
public boolean equals(Object o) {
18+
if (this == o) return true;
19+
if (o == null || getClass() != o.getClass()) return false;
20+
Project project = (Project) o;
21+
return Objects.equals(fullName, project.fullName);
22+
}
23+
24+
@Override
25+
public int hashCode() {
26+
return Objects.hash(fullName);
27+
}
28+
29+
@Override
30+
public String toString() {
31+
return new StringJoiner(", ", Project.class.getSimpleName() + "[", "]")
32+
.add("fullName='" + fullName + "'")
33+
.toString();
34+
}
35+
}
81 KB
Binary file not shown.

0 commit comments

Comments
 (0)