|
| 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