diff --git a/pom.xml b/pom.xml index ea667fe..b15a417 100755 --- a/pom.xml +++ b/pom.xml @@ -7,12 +7,17 @@ org.jenkins-ci.plugins plugin - 1.455 + 1.596.1 log-parser hpi 1.1-SNAPSHOT Log Parser Plugin + + + 1.7 + + @@ -47,6 +52,15 @@ jborghi@java.net + + + + org.jenkins-ci.plugins.workflow + workflow-aggregator + ${workflow-jenkins-plugin.version} + test + + diff --git a/src/main/java/hudson/plugins/logparser/LogParserAction.java b/src/main/java/hudson/plugins/logparser/LogParserAction.java index 37bbc2a..6565d03 100755 --- a/src/main/java/hudson/plugins/logparser/LogParserAction.java +++ b/src/main/java/hudson/plugins/logparser/LogParserAction.java @@ -3,6 +3,7 @@ import hudson.Functions; import hudson.model.Action; import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.util.Area; import hudson.util.ChartUtil; import hudson.util.ColorPalette; @@ -31,13 +32,17 @@ public class LogParserAction implements Action { - final private AbstractBuild build; + final private Run build; final private LogParserResult result; private static String urlName = "parsed_console"; - public LogParserAction(final AbstractBuild build, - final LogParserResult result) { + @Deprecated + public LogParserAction(final AbstractBuild build, final LogParserResult result) { + this((Run) build, result); + } + + public LogParserAction(final Run build, final LogParserResult result) { this.build = build; this.result = result; @@ -59,7 +64,7 @@ public static String getUrlNameStat() { return urlName; } - public AbstractBuild getOwner() { + public Run getOwner() { return build; } @@ -70,7 +75,7 @@ public LogParserResult getResult() { } public LogParserAction getPreviousAction() { - AbstractBuild build = this.getOwner(); + Run build = this.getOwner(); while (true) { diff --git a/src/main/java/hudson/plugins/logparser/LogParserParser.java b/src/main/java/hudson/plugins/logparser/LogParserParser.java index d05ae96..90adcea 100755 --- a/src/main/java/hudson/plugins/logparser/LogParserParser.java +++ b/src/main/java/hudson/plugins/logparser/LogParserParser.java @@ -3,6 +3,7 @@ import hudson.FilePath; import hudson.console.ConsoleNote; import hudson.model.AbstractBuild; +import hudson.model.Run; import hudson.remoting.VirtualChannel; import java.io.BufferedReader; @@ -69,8 +70,12 @@ public LogParserParser(final FilePath parsingRulesFile, * lists of links to these errors/warnings/info messages respectively : * errorLinks.html, warningLinks.html, infoLinks.html */ - public LogParserResult parseLog(final AbstractBuild build) - throws IOException, InterruptedException { + @Deprecated + public LogParserResult parseLog(final AbstractBuild build) throws IOException, InterruptedException { + return this.parseLog((Run) build); + } + + public LogParserResult parseLog(final Run build) throws IOException, InterruptedException { // init logger final Logger logger = Logger.getLogger(getClass().getName()); @@ -292,10 +297,9 @@ private String addMarkerAndLink(final String line, return markedLine.toString(); } - private void parseLogBody(final AbstractBuild build, - final BufferedWriter writer, final FilePath filePath, - final String logFileLocation, final int linesInLog, - final Logger logger) throws IOException, InterruptedException { + private void parseLogBody(final Run build, final BufferedWriter writer, final FilePath filePath, final + String logFileLocation, final int linesInLog, final Logger logger) throws IOException, InterruptedException { + // Logging information - start final String signature = build.getParent().getName() + "_build_" + build.getNumber(); diff --git a/src/main/java/hudson/plugins/logparser/LogParserPublisher.java b/src/main/java/hudson/plugins/logparser/LogParserPublisher.java index 3c7c583..c49f358 100755 --- a/src/main/java/hudson/plugins/logparser/LogParserPublisher.java +++ b/src/main/java/hudson/plugins/logparser/LogParserPublisher.java @@ -2,11 +2,14 @@ import hudson.FilePath; import hudson.Launcher; +import hudson.Util; import hudson.model.Action; import hudson.model.BuildListener; import hudson.model.Result; import hudson.model.AbstractBuild; import hudson.model.AbstractProject; +import hudson.model.Run; +import hudson.model.TaskListener; import hudson.plugins.logparser.action.LogParserProjectAction; import hudson.tasks.BuildStepDescriptor; import hudson.tasks.BuildStepMonitor; @@ -19,18 +22,21 @@ import java.util.logging.Level; import java.util.logging.Logger; +import jenkins.tasks.SimpleBuildStep; import net.sf.json.JSONObject; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.DataBoundSetter; import org.kohsuke.stapler.StaplerRequest; -public class LogParserPublisher extends Recorder implements Serializable { +public class LogParserPublisher extends Recorder implements SimpleBuildStep, Serializable { private static final long serialVersionUID = 1L; - public final boolean unstableOnWarning; - public final boolean failBuildOnError; - public final boolean showGraphs; - public final String parsingRulesPath; - public final boolean useProjectRule; - public final String projectRulePath; + public boolean unstableOnWarning; + public boolean failBuildOnError; + public boolean showGraphs; + public String parsingRulesPath = null; + public boolean useProjectRule; + public String projectRulePath = null; /** * Create new LogParserPublisher. @@ -48,6 +54,7 @@ public class LogParserPublisher extends Recorder implements Serializable { * @param projectRulePath * path to project specific rules relative to workspace root. */ + @Deprecated private LogParserPublisher(final boolean unstableOnWarning, final boolean failBuildOnError, final boolean showGraphs, final String parsingRulesPath, final boolean useProjectRule, @@ -61,32 +68,65 @@ private LogParserPublisher(final boolean unstableOnWarning, this.projectRulePath = projectRulePath; } + @DataBoundConstructor + public LogParserPublisher(boolean useProjectRule, String projectRulePath, String parsingRulesPath) { + super(); + if (useProjectRule) { + this.projectRulePath = Util.fixEmpty(projectRulePath); + this.parsingRulesPath = null; + } else { + this.parsingRulesPath = Util.fixEmpty(parsingRulesPath); + this.projectRulePath = null; + } + this.useProjectRule = useProjectRule; + } + + @DataBoundSetter + public void setUnstableOnWarning(boolean unstableOnWarning) { + this.unstableOnWarning = unstableOnWarning; + } + + @DataBoundSetter + public void setFailBuildOnError(boolean failBuildOnError) { + this.failBuildOnError = failBuildOnError; + } + + @DataBoundSetter + public void setShowGraphs(boolean showGraphs) { + this.showGraphs = showGraphs; + } + @Override public boolean prebuild(final AbstractBuild build, final BuildListener listener) { return true; } + @Deprecated @Override - public boolean perform(final AbstractBuild build, - final Launcher launcher, final BuildListener listener) + public boolean perform(final AbstractBuild build, final Launcher launcher, final BuildListener listener) throws InterruptedException, IOException { + + this.perform((Run) build, build.getWorkspace(), launcher, listener); + return true; + } + + @Override + public void perform(Run build, FilePath workspace, Launcher launcher, TaskListener listener) throws + InterruptedException, IOException { + final Logger logger = Logger.getLogger(getClass().getName()); LogParserResult result = new LogParserResult(); try { - // Create a parser with the parsing rules as configured : colors, - // regular expressions, etc. - boolean preformattedHtml = !((DescriptorImpl) getDescriptor()) - .getLegacyFormatting(); + // Create a parser with the parsing rules as configured : colors, regular expressions, etc. + boolean preformattedHtml = !((DescriptorImpl) getDescriptor()).getLegacyFormatting(); final FilePath parsingRulesFile; if (useProjectRule) { - parsingRulesFile = new FilePath(build.getWorkspace(), - projectRulePath); + parsingRulesFile = new FilePath(workspace, projectRulePath); } else { parsingRulesFile = new FilePath(new File(parsingRulesPath)); } - final LogParserParser parser = new LogParserParser( - parsingRulesFile, preformattedHtml, launcher.getChannel()); + final LogParserParser parser = new LogParserParser(parsingRulesFile, preformattedHtml, launcher.getChannel()); // Parse the build's log according to these rules and get the result result = parser.parseLog(build); @@ -116,9 +156,7 @@ public boolean perform(final AbstractBuild build, // Add an action created with the above results final LogParserAction action = new LogParserAction(build, result); - build.getActions().add(0, action); - - return true; + build.addAction(action); } @Override @@ -171,38 +209,6 @@ public boolean configure(final StaplerRequest req, final JSONObject json) save(); return true; } - - /** - * Cannot use simple DataBoundConstructor due to radioBlock usage where - * a JSON object is returned holding the selected value of the block. - * - * {@inheritDoc} - */ - @Override - public LogParserPublisher newInstance(StaplerRequest req, - JSONObject json) throws FormException { - - String configuredParsingRulesPath = null; - String configuredProjectRulePath = null; - boolean configuredUseProjectRule = false; - final JSONObject useProjectRuleJSON = json.getJSONObject("useProjectRule"); - - if (useProjectRuleJSON != null) { - configuredUseProjectRule = useProjectRuleJSON.getBoolean("value"); - - if (!configuredUseProjectRule && useProjectRuleJSON.containsKey("parsingRulesPath")) { - configuredParsingRulesPath = useProjectRuleJSON.getString("parsingRulesPath"); - } else if (configuredUseProjectRule && useProjectRuleJSON.containsKey("projectRulePath")) { - configuredProjectRulePath = useProjectRuleJSON.getString("projectRulePath"); - } - } - return new LogParserPublisher(json.getBoolean("unstableOnWarning"), - json.getBoolean("failBuildOnError"), - json.getBoolean("showGraphs"), - configuredParsingRulesPath, - configuredUseProjectRule, - configuredProjectRulePath); - } } public BuildStepMonitor getRequiredMonitorService() { diff --git a/src/main/java/hudson/plugins/logparser/LogParserStatusComputer.java b/src/main/java/hudson/plugins/logparser/LogParserStatusComputer.java index 6a96a5b..8e4b3d5 100755 --- a/src/main/java/hudson/plugins/logparser/LogParserStatusComputer.java +++ b/src/main/java/hudson/plugins/logparser/LogParserStatusComputer.java @@ -3,6 +3,7 @@ import hudson.FilePath; import hudson.remoting.Callable; import hudson.remoting.VirtualChannel; +import jenkins.security.MasterToSlaveCallable; import java.io.BufferedReader; import java.io.File; @@ -43,7 +44,7 @@ private HashMap computeStatusMatches( HashMap result = null; result = channel - .call(new Callable, RuntimeException>() { + .call(new MasterToSlaveCallable, RuntimeException>() { private static final long serialVersionUID = 1L; diff --git a/src/main/java/hudson/plugins/logparser/LogParserUtils.java b/src/main/java/hudson/plugins/logparser/LogParserUtils.java index f760ca7..c7c1df9 100755 --- a/src/main/java/hudson/plugins/logparser/LogParserUtils.java +++ b/src/main/java/hudson/plugins/logparser/LogParserUtils.java @@ -13,9 +13,12 @@ public final class LogParserUtils { - public static String[] readParsingRules(final FilePath parsingRulesFile) - throws IOException { - return parsingRulesFile.readToString().split("\n"); + public static String[] readParsingRules(final FilePath parsingRulesFile) throws IOException { + try { + return parsingRulesFile.readToString().split("\n"); + } catch (InterruptedException ie) { + throw new IOException(ie); + } } public static boolean skipParsingRule(final String parsingRule) { diff --git a/src/main/resources/hudson/plugins/logparser/LogParserPublisher/config.jelly b/src/main/resources/hudson/plugins/logparser/LogParserPublisher/config.jelly index 2bd1d76..ff269c9 100755 --- a/src/main/resources/hudson/plugins/logparser/LogParserPublisher/config.jelly +++ b/src/main/resources/hudson/plugins/logparser/LogParserPublisher/config.jelly @@ -9,7 +9,7 @@ - + - + diff --git a/src/test/java/org/jenkinsci/plugins/logparser/LogParserWorkflowTest.java b/src/test/java/org/jenkinsci/plugins/logparser/LogParserWorkflowTest.java new file mode 100644 index 0000000..f276ea2 --- /dev/null +++ b/src/test/java/org/jenkinsci/plugins/logparser/LogParserWorkflowTest.java @@ -0,0 +1,54 @@ +package org.jenkinsci.plugins.logparser; + +import hudson.FilePath; +import hudson.plugins.logparser.LogParserAction; +import hudson.plugins.logparser.LogParserPublisher; +import hudson.tasks.Maven; +import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition; +import org.jenkinsci.plugins.workflow.job.WorkflowJob; +import org.junit.BeforeClass; +import org.junit.ClassRule; +import org.junit.Test; +import org.jvnet.hudson.test.JenkinsRule; + +import static org.junit.Assert.assertEquals; + +/** + * In this test suite we initialize the Job workspaces with a resource (maven-project1.zip) that contains a Maven + * project. + */ +public class LogParserWorkflowTest { + + @ClassRule + public static JenkinsRule jenkinsRule = new JenkinsRule(); + + private static Maven.MavenInstallation mavenInstallation; + + @BeforeClass + public static void init() throws Exception { + mavenInstallation = jenkinsRule.configureMaven3(); + } + + /** + * Run a workflow job using {@link LogParserPublisher} and check for success. + */ + @Test + public void logParserPublisherWorkflowStep() throws Exception { + WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "logParserPublisherWorkflowStep"); + FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job); + workspace.unzipFrom(getClass().getResourceAsStream("./maven-project1.zip")); + job.setDefinition(new CpsFlowDefinition("" + + "node {\n" + + " def mvnHome = tool '" + mavenInstallation.getName() + "'\n" + + " sh \"${mvnHome}/bin/mvn clean install\"\n" + + " step([$class: 'LogParserPublisher', projectRulePath: 'logparser-rules.txt', useProjectRule: true])\n" + + "}\n", true) + ); + jenkinsRule.assertBuildStatusSuccess(job.scheduleBuild2(0)); + LogParserAction result = job.getLastBuild().getAction(LogParserAction.class); + assertEquals(0, result.getResult().getTotalErrors()); + assertEquals(2, result.getResult().getTotalWarnings()); + assertEquals(0, result.getResult().getTotalInfos()); + } + +} diff --git a/src/test/resources/org/jenkinsci/plugins/logparser/maven-project1.zip b/src/test/resources/org/jenkinsci/plugins/logparser/maven-project1.zip new file mode 100644 index 0000000..6ec2268 Binary files /dev/null and b/src/test/resources/org/jenkinsci/plugins/logparser/maven-project1.zip differ