Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP JENKINS-57427 support warnings-ng plugin #221

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 22 additions & 6 deletions jenkins-plugin/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
workflow-step-api or workflow-api-->
<junit-plugin.version>1.26.1</junit-plugin.version>
<asm.version>5.2</asm.version>
<workflow-support-plugin.version>2.22</workflow-support-plugin.version>
<workflow-support-plugin.version>3.2</workflow-support-plugin.version>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can switch to BOM

</properties>
<dependencies>
<dependency>
Expand Down Expand Up @@ -119,7 +119,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-job</artifactId>
<version>2.29</version>
<version>2.32</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand All @@ -145,7 +145,7 @@
TODO when there is enough adoption of workflow-step-api with https://github.com/jenkinsci/workflow-step-api-plugin/pull/38
Replace org.jenkinsci.plugins.pipeline.maven.fix.jenkins49337.GeneralNonBlockingStepExecution by org.jenkinsci.plugins.workflow.steps.GeneralNonBlockingStepExecution;
-->
<version>2.16</version>
<version>2.19</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
Expand Down Expand Up @@ -175,7 +175,7 @@
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>script-security</artifactId>
<version>1.54.1</version>
<version>1.58</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
Expand All @@ -200,6 +200,22 @@
<scope>compile</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>io.jenkins.plugins</groupId>
<artifactId>warnings-ng</artifactId>
<version>5.0.0</version>
<optional>true</optional>
<exclusions>
<exclusion>
<groupId>org.ow2.asm</groupId>
<artifactId>asm</artifactId>
</exclusion>
<exclusion>
<groupId>org.antlr</groupId>
<artifactId>antlr4-runtime</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins</groupId>
<artifactId>htmlpublisher</artifactId>
Expand Down Expand Up @@ -541,7 +557,7 @@
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.5</version>
<version>2.6</version>
</dependency>
<dependency>
<groupId>org.jvnet.localizer</groupId>
Expand Down Expand Up @@ -581,7 +597,7 @@
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.8.1</version>
<version>3.9</version>
</dependency>
<dependency>
<groupId>org.jenkins-ci</groupId>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
package org.jenkinsci.plugins.pipeline.maven.publishers;

import hudson.FilePath;
import hudson.Launcher;
import hudson.model.Run;
import hudson.model.TaskListener;
import io.jenkins.plugins.analysis.core.model.Tool;
import io.jenkins.plugins.analysis.core.steps.IssuesRecorder;
import io.jenkins.plugins.analysis.warnings.SpotBugs;
import org.jenkinsci.plugins.pipeline.maven.MavenArtifact;
import org.jenkinsci.plugins.pipeline.maven.MavenPublisher;
import org.jenkinsci.plugins.pipeline.maven.MavenSpyLogProcessor;
import org.jenkinsci.plugins.pipeline.maven.util.XmlUtils;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.w3c.dom.Element;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.annotation.Nonnull;

/**
* @author <a href="mailto:[email protected]">Cyrille Le Clerc</a>
*/
public class WarningsNgPublisher extends MavenPublisher {
private static final Logger LOGGER = Logger.getLogger(SpotBugsAnalysisPublisher.class.getName());

private static final long serialVersionUID = 1L;

@Override
public void process(@Nonnull StepContext context, @Nonnull Element mavenSpyLogsElt) throws IOException, InterruptedException {

TaskListener listener = context.get(TaskListener.class);
FilePath workspace = context.get(FilePath.class);
Run run = context.get(Run.class);
Launcher launcher = context.get(Launcher.class);

List<Element> spotbugsEvents = XmlUtils.getExecutionEventsByPlugin(mavenSpyLogsElt, "com.github.spotbugs", "spotbugs-maven-plugin", "spotbugs", "MojoSucceeded", "MojoFailed");

if (spotbugsEvents.isEmpty()) {
LOGGER.log(Level.FINE, "No com.github.spotbugs:spotbugs-maven-plugin:spotbugs execution found");
return;
}

List<SpotBugsReportDetails> spotBugsReportDetails = new ArrayList<>();
for (Element spotBugsTestEvent : spotbugsEvents) {
String spotBugsEventType = spotBugsTestEvent.getAttribute("type");
if (!spotBugsEventType.equals("MojoSucceeded") && !spotBugsEventType.equals("MojoFailed")) {
continue;
}

Element pluginElt = XmlUtils.getUniqueChildElement(spotBugsTestEvent, "plugin");
Element xmlOutputDirectoryElt = XmlUtils.getUniqueChildElementOrNull(pluginElt, "xmlOutputDirectory");
Element projectElt = XmlUtils.getUniqueChildElement(spotBugsTestEvent, "project");
MavenArtifact mavenArtifact = XmlUtils.newMavenArtifact(projectElt);
MavenSpyLogProcessor.PluginInvocation pluginInvocation = XmlUtils.newPluginInvocation(pluginElt);

if (xmlOutputDirectoryElt == null) {
listener.getLogger().println("[withMaven] No <xmlOutputDirectoryElt> element found for <plugin> in " + XmlUtils.toString(spotBugsTestEvent));
continue;
}
String xmlOutputDirectory = xmlOutputDirectoryElt.getTextContent().trim();
if (xmlOutputDirectory.contains("${project.build.directory}")) {
String projectBuildDirectory = XmlUtils.getProjectBuildDirectory(projectElt);
if (projectBuildDirectory == null || projectBuildDirectory.isEmpty()) {
listener.getLogger().println("[withMaven] '${project.build.directory}' found for <project> in " + XmlUtils.toString(spotBugsTestEvent));
continue;
}

xmlOutputDirectory = xmlOutputDirectory.replace("${project.build.directory}", projectBuildDirectory);

} else if (xmlOutputDirectory.contains("${basedir}")) {
String baseDir = projectElt.getAttribute("baseDir");
if (baseDir.isEmpty()) {
listener.getLogger().println("[withMaven] '${basedir}' found for <project> in " + XmlUtils.toString(spotBugsTestEvent));
continue;
}

xmlOutputDirectory = xmlOutputDirectory.replace("${basedir}", baseDir);
}

xmlOutputDirectory = XmlUtils.getPathInWorkspace(xmlOutputDirectory, workspace);

String spotBugsResultsFile = xmlOutputDirectory + "/spotbugsXml.xml";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since this only supports SpotBugs so far, maybe better to reflect that in the PR title.


SpotBugsReportDetails details = new SpotBugsReportDetails(mavenArtifact, pluginInvocation, spotBugsResultsFile);
spotBugsReportDetails.add(details);
}

List<Tool> tools = new ArrayList<>();
tools.addAll(spotBugsReportDetails.stream().map(details -> details.toTool()).collect(Collectors.toList()));

IssuesRecorder issuesRecorder = new IssuesRecorder();
issuesRecorder.setTools(tools);
issuesRecorder.perform(run, workspace, launcher, listener);

}

public static class SpotBugsReportDetails {
final MavenArtifact mavenArtifact;
final MavenSpyLogProcessor.PluginInvocation pluginInvocation;
final String spotBugsReportFile;

public SpotBugsReportDetails(MavenArtifact mavenArtifact, MavenSpyLogProcessor.PluginInvocation pluginInvocation, String spotBugsReportFile) {
this.mavenArtifact = mavenArtifact;
this.pluginInvocation = pluginInvocation;
this.spotBugsReportFile = spotBugsReportFile;
}

@Override
public String toString() {
return "spotBugsReportDetails{" +
"mavenArtifact=" + mavenArtifact +
", pluginInvocation='" + pluginInvocation + '\'' +
", spotBugsReportFile='" + spotBugsReportFile + '\'' +
'}';
}
public SpotBugs toTool() {
SpotBugs spotBugs = new SpotBugs();
spotBugs.setId(spotBugs.getDescriptor().getId() + "_" + mavenArtifact.getId() + "_" + pluginInvocation.getId());
spotBugs.setName(spotBugs.getDescriptor().getName() + " " + mavenArtifact.getId() + " " + pluginInvocation.getId());
spotBugs.setPattern(spotBugsReportFile);
return spotBugs;
}
}
}