Skip to content

Commit 0c58ff3

Browse files
JENKINS-57427 support warnings-ng plugin
1 parent 638895d commit 0c58ff3

File tree

2 files changed

+152
-6
lines changed

2 files changed

+152
-6
lines changed

jenkins-plugin/pom.xml

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
workflow-step-api or workflow-api-->
6464
<junit-plugin.version>1.26.1</junit-plugin.version>
6565
<asm.version>5.2</asm.version>
66-
<workflow-support-plugin.version>2.22</workflow-support-plugin.version>
66+
<workflow-support-plugin.version>3.2</workflow-support-plugin.version>
6767
</properties>
6868
<dependencies>
6969
<dependency>
@@ -119,7 +119,7 @@
119119
<dependency>
120120
<groupId>org.jenkins-ci.plugins.workflow</groupId>
121121
<artifactId>workflow-job</artifactId>
122-
<version>2.29</version>
122+
<version>2.32</version>
123123
</dependency>
124124
<dependency>
125125
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -145,7 +145,7 @@
145145
TODO when there is enough adoption of workflow-step-api with https://github.com/jenkinsci/workflow-step-api-plugin/pull/38
146146
Replace org.jenkinsci.plugins.pipeline.maven.fix.jenkins49337.GeneralNonBlockingStepExecution by org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution;
147147
-->
148-
<version>2.16</version>
148+
<version>2.19</version>
149149
</dependency>
150150
<dependency>
151151
<groupId>org.jenkins-ci.plugins.workflow</groupId>
@@ -175,7 +175,7 @@
175175
<dependency>
176176
<groupId>org.jenkins-ci.plugins</groupId>
177177
<artifactId>script-security</artifactId>
178-
<version>1.54.1</version>
178+
<version>1.58</version>
179179
</dependency>
180180
<dependency>
181181
<groupId>org.jenkins-ci.plugins</groupId>
@@ -200,6 +200,22 @@
200200
<scope>compile</scope>
201201
<optional>true</optional>
202202
</dependency>
203+
<dependency>
204+
<groupId>io.jenkins.plugins</groupId>
205+
<artifactId>warnings-ng</artifactId>
206+
<version>5.0.0</version>
207+
<optional>true</optional>
208+
<exclusions>
209+
<exclusion>
210+
<groupId>org.ow2.asm</groupId>
211+
<artifactId>asm</artifactId>
212+
</exclusion>
213+
<exclusion>
214+
<groupId>org.antlr</groupId>
215+
<artifactId>antlr4-runtime</artifactId>
216+
</exclusion>
217+
</exclusions>
218+
</dependency>
203219
<dependency>
204220
<groupId>org.jenkins-ci.plugins</groupId>
205221
<artifactId>htmlpublisher</artifactId>
@@ -541,7 +557,7 @@
541557
<dependency>
542558
<groupId>commons-io</groupId>
543559
<artifactId>commons-io</artifactId>
544-
<version>2.5</version>
560+
<version>2.6</version>
545561
</dependency>
546562
<dependency>
547563
<groupId>org.jvnet.localizer</groupId>
@@ -581,7 +597,7 @@
581597
<dependency>
582598
<groupId>org.apache.commons</groupId>
583599
<artifactId>commons-lang3</artifactId>
584-
<version>3.8.1</version>
600+
<version>3.9</version>
585601
</dependency>
586602
<dependency>
587603
<groupId>org.jenkins-ci</groupId>
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
package org.jenkinsci.plugins.pipeline.maven.publishers;
2+
3+
import hudson.FilePath;
4+
import hudson.Launcher;
5+
import hudson.model.Run;
6+
import hudson.model.TaskListener;
7+
import io.jenkins.plugins.analysis.core.model.Tool;
8+
import io.jenkins.plugins.analysis.core.steps.IssuesRecorder;
9+
import io.jenkins.plugins.analysis.warnings.SpotBugs;
10+
import org.jenkinsci.plugins.pipeline.maven.MavenArtifact;
11+
import org.jenkinsci.plugins.pipeline.maven.MavenPublisher;
12+
import org.jenkinsci.plugins.pipeline.maven.MavenSpyLogProcessor;
13+
import org.jenkinsci.plugins.pipeline.maven.util.XmlUtils;
14+
import org.jenkinsci.plugins.workflow.steps.StepContext;
15+
import org.w3c.dom.Element;
16+
17+
import java.io.IOException;
18+
import java.util.ArrayList;
19+
import java.util.List;
20+
import java.util.logging.Level;
21+
import java.util.logging.Logger;
22+
import java.util.stream.Collectors;
23+
24+
import javax.annotation.Nonnull;
25+
26+
/**
27+
* @author <a href="mailto:cleclerc@cloudbees.com">Cyrille Le Clerc</a>
28+
*/
29+
public class WarningsNgPublisher extends MavenPublisher {
30+
private static final Logger LOGGER = Logger.getLogger(SpotBugsAnalysisPublisher.class.getName());
31+
32+
private static final long serialVersionUID = 1L;
33+
34+
@Override
35+
public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsElt) throws IOException, InterruptedException {
36+
37+
TaskListener listener = context.get(TaskListener.class);
38+
FilePath workspace = context.get(FilePath.class);
39+
Run run = context.get(Run.class);
40+
Launcher launcher = context.get(Launcher.class);
41+
42+
List<Element> spotbugsEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, "com.github.spotbugs", "spotbugs-maven-plugin", "spotbugs", "MojoSucceeded", "MojoFailed");
43+
44+
if (spotbugsEvents.isEmpty()) {
45+
LOGGER.log(Level.FINE, "No com.github.spotbugs:spotbugs-maven-plugin:spotbugs execution found");
46+
return;
47+
}
48+
49+
List<SpotBugsReportDetails> spotBugsReportDetails = new ArrayList<>();
50+
for (Element spotBugsTestEvent : spotbugsEvents) {
51+
String spotBugsEventType = spotBugsTestEvent.getAttribute("type");
52+
if (!spotBugsEventType.equals("MojoSucceeded") && !spotBugsEventType.equals("MojoFailed")) {
53+
continue;
54+
}
55+
56+
Element pluginElt = XmlUtils.getUniqueChildElement(spotBugsTestEvent, "plugin");
57+
Element xmlOutputDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "xmlOutputDirectory");
58+
Element projectElt = XmlUtils.getUniqueChildElement(spotBugsTestEvent, "project");
59+
MavenArtifact mavenArtifact = XmlUtils.newMavenArtifact(projectElt);
60+
MavenSpyLogProcessor.PluginInvocation pluginInvocation = XmlUtils.newPluginInvocation(pluginElt);
61+
62+
if (xmlOutputDirectoryElt == null) {
63+
listener.getLogger().println("[withMaven] No <xmlOutputDirectoryElt> element found for <plugin> in " + XmlUtils.toString(spotBugsTestEvent));
64+
continue;
65+
}
66+
String xmlOutputDirectory = xmlOutputDirectoryElt.getTextContent().trim();
67+
if (xmlOutputDirectory.contains("${project.build.directory}")) {
68+
String projectBuildDirectory = XmlUtils.getProjectBuildDirectory(projectElt);
69+
if (projectBuildDirectory == null || projectBuildDirectory.isEmpty()) {
70+
listener.getLogger().println("[withMaven] '${project.build.directory}' found for <project> in " + XmlUtils.toString(spotBugsTestEvent));
71+
continue;
72+
}
73+
74+
xmlOutputDirectory = xmlOutputDirectory.replace("${project.build.directory}", projectBuildDirectory);
75+
76+
} else if (xmlOutputDirectory.contains("${basedir}")) {
77+
String baseDir = projectElt.getAttribute("baseDir");
78+
if (baseDir.isEmpty()) {
79+
listener.getLogger().println("[withMaven] '${basedir}' found for <project> in " + XmlUtils.toString(spotBugsTestEvent));
80+
continue;
81+
}
82+
83+
xmlOutputDirectory = xmlOutputDirectory.replace("${basedir}", baseDir);
84+
}
85+
86+
xmlOutputDirectory = XmlUtils.getPathInWorkspace(xmlOutputDirectory, workspace);
87+
88+
String spotBugsResultsFile = xmlOutputDirectory + "/spotbugsXml.xml";
89+
90+
SpotBugsReportDetails details = new SpotBugsReportDetails(mavenArtifact, pluginInvocation, spotBugsResultsFile);
91+
spotBugsReportDetails.add(details);
92+
}
93+
94+
List<Tool> tools = new ArrayList<>();
95+
tools.addAll(spotBugsReportDetails.stream().map(details -> details.toTool()).collect(Collectors.toList()));
96+
97+
IssuesRecorder issuesRecorder = new IssuesRecorder();
98+
issuesRecorder.setTools(tools);
99+
issuesRecorder.perform(run, workspace, launcher, listener);
100+
101+
}
102+
103+
public static class SpotBugsReportDetails {
104+
final MavenArtifact mavenArtifact;
105+
final MavenSpyLogProcessor.PluginInvocation pluginInvocation;
106+
final String spotBugsReportFile;
107+
108+
public SpotBugsReportDetails(MavenArtifact mavenArtifact, MavenSpyLogProcessor.PluginInvocation pluginInvocation, String spotBugsReportFile) {
109+
this.mavenArtifact = mavenArtifact;
110+
this.pluginInvocation = pluginInvocation;
111+
this.spotBugsReportFile = spotBugsReportFile;
112+
}
113+
114+
@Override
115+
public String toString() {
116+
return "spotBugsReportDetails{" +
117+
"mavenArtifact=" + mavenArtifact +
118+
", pluginInvocation='" + pluginInvocation + '\'' +
119+
", spotBugsReportFile='" + spotBugsReportFile + '\'' +
120+
'}';
121+
}
122+
public SpotBugs toTool() {
123+
SpotBugs spotBugs = new SpotBugs();
124+
spotBugs.setId(spotBugs.getDescriptor().getId() + "_" + mavenArtifact.getId() + "_" + pluginInvocation.getId());
125+
spotBugs.setName(spotBugs.getDescriptor().getName() + " " + mavenArtifact.getId() + " " + pluginInvocation.getId());
126+
spotBugs.setPattern(spotBugsReportFile);
127+
return spotBugs;
128+
}
129+
}
130+
}

0 commit comments

Comments
 (0)