Skip to content
This repository has been archived by the owner on Apr 6, 2022. It is now read-only.

Commit

Permalink
Added a step that publishes warnings.
Browse files Browse the repository at this point in the history
  • Loading branch information
uhafner committed Aug 6, 2017
1 parent e9f819c commit 7782149
Show file tree
Hide file tree
Showing 11 changed files with 139 additions and 52 deletions.
2 changes: 1 addition & 1 deletion checkstyle.iml
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_6">
<output url="file://$MODULE_DIR$/target/classes" />
<output-test url="file://$MODULE_DIR$/target/test-classes" />
<content url="file://$MODULE_DIR$">
Expand Down
Expand Up @@ -35,15 +35,16 @@ public class CheckStyleMavenResultAction extends MavenResultAction<CheckStyleRes
* the result in this build
*/
public CheckStyleMavenResultAction(final AbstractBuild<?, ?> owner, final HealthDescriptor healthDescriptor,
final String defaultEncoding, final CheckStyleResult result) {
super(new CheckStyleResultAction(owner, healthDescriptor, result), defaultEncoding, "CHECKSTYLE");
final String defaultEncoding, final CheckStyleResult result, final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference) {
super(new CheckStyleResultAction(owner, healthDescriptor, result), defaultEncoding, "CHECKSTYLE",
usePreviousBuildAsReference, useStableBuildAsReference);
}

@Override
public MavenAggregatedReport createAggregatedAction(final MavenModuleSetBuild build, final Map<MavenModule, List<MavenBuild>> moduleBuilds) {
return new CheckStyleMavenResultAction(build, getHealthDescriptor(), getDefaultEncoding(),
new CheckStyleResult(build, getDefaultEncoding(), new ParserResult(),
usePreviousBuildAsStable(), useOnlyStableBuildsAsReference()));
new CheckStyleResult(build, getDefaultEncoding(), new ParserResult(), usePreviousBuildAsStable(), useOnlyStableBuildsAsReference()),
usePreviousBuildAsStable(), useOnlyStableBuildsAsReference());
}

@Override
Expand All @@ -59,9 +60,7 @@ public Class<? extends MavenResultAction<CheckStyleResult>> getIndividualActionT
@Override
protected CheckStyleResult createResult(final CheckStyleResult existingResult, final CheckStyleResult additionalResult) {
return new CheckStyleReporterResult(getOwner(), additionalResult.getDefaultEncoding(),
aggregate(existingResult, additionalResult),
existingResult.usePreviousBuildAsStable(),
existingResult.useOnlyStableBuildsAsReference());
aggregate(existingResult, additionalResult), usePreviousBuildAsStable(), useOnlyStableBuildsAsReference());
}
}

Expand Up @@ -150,7 +150,8 @@ protected CheckStyleResult createResult(final MavenBuild build, final ParserResu

@Override
protected MavenAggregatedReport createMavenAggregatedReport(final MavenBuild build, final CheckStyleResult result) {
return new CheckStyleMavenResultAction(build, this, getDefaultEncoding(), result);
return new CheckStyleMavenResultAction(build, this, getDefaultEncoding(), result,
usePreviousBuildAsReference(), useOnlyStableBuildsAsReference());
}

@Override
Expand Down
11 changes: 8 additions & 3 deletions src/main/java/hudson/plugins/checkstyle/CheckStyleResult.java
Expand Up @@ -3,9 +3,10 @@
import com.thoughtworks.xstream.XStream;

import hudson.model.Run;
import hudson.plugins.analysis.core.BuildHistory;
import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.core.ReferenceFinder;
import hudson.plugins.analysis.core.ReferenceProvider;
import hudson.plugins.analysis.core.ResultAction;
import hudson.plugins.checkstyle.parser.Warning;

Expand Down Expand Up @@ -58,11 +59,15 @@ public CheckStyleResult(final Run<?, ?> build, final String defaultEncoding, fin
protected CheckStyleResult(final Run<?, ?> build, final String defaultEncoding, final ParserResult result,
final boolean usePreviousBuildAsReference, final boolean useStableBuildAsReference,
final Class<? extends ResultAction<CheckStyleResult>> actionType) {
this(build, new BuildHistory(build, actionType, usePreviousBuildAsReference, useStableBuildAsReference),
this(build, ReferenceFinder.create(build, actionType, usePreviousBuildAsReference, useStableBuildAsReference),
result, defaultEncoding, true);
}

CheckStyleResult(final Run<?, ?> build, final BuildHistory history,
public CheckStyleResult(final Run run, final String defaultEncoding, final ParserResult warnings, final ReferenceProvider referenceProvider) {
super(run, referenceProvider, warnings, defaultEncoding, true);
}

CheckStyleResult(final Run<?, ?> build, final ReferenceProvider history,
final ParserResult result, final String defaultEncoding, final boolean canSerialize) {
super(build, history, result, defaultEncoding);

Expand Down
Expand Up @@ -36,6 +36,16 @@ public class ParseWarningsStep extends Step {
private String defaultEncoding;
private boolean shouldDetectModules;

@DataBoundConstructor
public ParseWarningsStep() {
// required for Stapler
}

@CheckForNull
public String getPattern() {
return pattern;
}

/**
* Sets the Ant file-set pattern of files to work with.
*
Expand All @@ -46,9 +56,8 @@ public void setPattern(final String pattern) {
this.pattern = pattern;
}

@CheckForNull
public String getPattern() {
return pattern;
public boolean getShouldDetectModules() {
return shouldDetectModules;
}

/**
Expand All @@ -63,13 +72,8 @@ public void setShouldDetectModules(final boolean shouldDetectModules) {
}

@CheckForNull
public boolean getShouldDetectModules() {
return shouldDetectModules;
}

@CheckForNull
public boolean shouldDetectModules() {
return shouldDetectModules;
public String getDefaultEncoding() {
return defaultEncoding;
}

/**
Expand All @@ -82,16 +86,6 @@ public void setDefaultEncoding(final String defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}

@CheckForNull
public String getDefaultEncoding() {
return defaultEncoding;
}

@DataBoundConstructor
public ParseWarningsStep() {

}

@Override
public StepExecution start(final StepContext stepContext) throws Exception {
return new Execution(stepContext, this);
Expand All @@ -109,7 +103,7 @@ protected Execution(@Nonnull final StepContext context, final ParseWarningsStep

pattern = step.getPattern();
defaultEncoding = step.getDefaultEncoding();
shouldDetectModules = step.shouldDetectModules();
shouldDetectModules = step.getShouldDetectModules();
}

@Override
Expand Down
Expand Up @@ -18,6 +18,8 @@
import hudson.model.Run;
import hudson.plugins.analysis.core.NullHealthDescriptor;
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.core.ReferenceFinder;
import hudson.plugins.analysis.core.ReferenceProvider;
import hudson.plugins.checkstyle.CheckStyleResult;
import hudson.plugins.checkstyle.CheckStyleResultAction;

Expand All @@ -28,25 +30,59 @@
public class PublishWarningsStep extends Step {
private ParserResult warnings;
private String defaultEncoding;
private boolean usePreviousBuildAsReference;
private boolean useStableBuildAsReference;

@DataBoundConstructor
public PublishWarningsStep(final ParserResult warnings) {
this.warnings = warnings;
}

public ParserResult getWarnings() {
return warnings;
}

public boolean getUsePreviousBuildAsReference() {
return usePreviousBuildAsReference;
}

/**
* Sets the default encoding used to read files (warnings, source code, etc.).
* Determines if the previous build should always be used as the reference build, no matter its overall result.
*
* @param defaultEncoding the encoding, e.g. "ISO-8859-1"
* @param usePreviousBuildAsReference if {@code true} then the previous build is always used
*/
@DataBoundSetter
public void setDefaultEncoding(final String defaultEncoding) {
this.defaultEncoding = defaultEncoding;
public void setUsePreviousBuildAsReference(final boolean usePreviousBuildAsReference) {
this.usePreviousBuildAsReference = usePreviousBuildAsReference;
}

public boolean getUseStableBuildAsReference() {
return useStableBuildAsReference;
}

/**
* Determines whether only stable builds should be used as reference builds or not.
*
* @param useStableBuildAsReference if {@code true} then a stable build is used as reference
*/
@DataBoundSetter
public void setUseStableBuildAsReference(final boolean useStableBuildAsReference) {
this.useStableBuildAsReference = useStableBuildAsReference;
}

@CheckForNull
public String getDefaultEncoding() {
return defaultEncoding;
}

@DataBoundConstructor
public PublishWarningsStep(final ParserResult warnings) {
this.warnings = warnings;
/**
* Sets the default encoding used to read files (warnings, source code, etc.).
*
* @param defaultEncoding the encoding, e.g. "ISO-8859-1"
*/
@DataBoundSetter
public void setDefaultEncoding(final String defaultEncoding) {
this.defaultEncoding = defaultEncoding;
}

@Override
Expand All @@ -55,22 +91,32 @@ public StepExecution start(final StepContext stepContext) throws Exception {
}

public static class Execution extends SynchronousNonBlockingStepExecution<Void> {
private final ParserResult warnings;
private final String defaultEncoding;
private boolean useStableBuildAsReference;
private boolean usePreviousBuildAsReference;
private String defaultEncoding;
private ParserResult warnings;

protected Execution(@Nonnull final StepContext context, final PublishWarningsStep step) {
super(context);

this.warnings = step.warnings;
this.defaultEncoding = step.defaultEncoding;
usePreviousBuildAsReference = step.usePreviousBuildAsReference;
useStableBuildAsReference = step.useStableBuildAsReference;
defaultEncoding = step.defaultEncoding;

warnings = step.warnings;
if (warnings == null) {
throw new NullPointerException("No warnings provided.");
}
}

@Override
protected Void run() throws Exception {
Run run = getContext().get(Run.class);

// FIXME: Split previous and reference result from history
CheckStyleResult result = new CheckStyleResult(run, defaultEncoding, warnings, false,false);
ReferenceProvider referenceProvider = ReferenceFinder.create(run, CheckStyleResultAction.class,
usePreviousBuildAsReference, useStableBuildAsReference);
CheckStyleResult result = new CheckStyleResult(run, defaultEncoding, warnings, referenceProvider);
// FIXME: split out health descriptor
// FIXME: remove thresholds from health descriptor
run.addAction(new CheckStyleResultAction(run, new NullHealthDescriptor(), result));
Expand Down
@@ -0,0 +1,15 @@
<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:st="jelly:stapler" xmlns:d="jelly:define"
xmlns:l="/lib/layout" xmlns:t="/lib/hudson" xmlns:f="/lib/form" xmlns:u="/util">

<f:entry title="${%title.usePreviousBuildAsReference}" description="${%description.usePreviousBuildAsReference}"
field="usePreviousBuildAsReference">
<f:checkbox/>
</f:entry>
<f:entry title="${%title.onlyUseStableBuildsAsReference}" description="${%description.onlyUseStableBuildsAsReference}"
field="useStableBuildAsReference">
<f:checkbox/>
</f:entry>
<u:defaultEncoding id="warnings"/>

</j:jelly>
@@ -0,0 +1,11 @@
title.onlyUseStableBuildsAsReference=Only use stable builds as reference
description.onlyUseStableBuildsAsReference=Use the last stable build as the reference to compute the number of new \
warnings against. This allows to ignore interim unstable builds for which the number of warnings \
decreased. Note that the last stable build is evaluated only by inspecting the unit test failures. \
The static analysis results are not considered.
title.usePreviousBuildAsReference=Use previous build as reference
description.usePreviousBuildAsReference=If set the number of new warnings will always be computed based on the \
previous build, even if that build is unstable (due to a violated warning threshold). \
Otherwise the last build that did not violate any \
given threshold will be used as reference. It is recommended to uncheck this option if \
the plug-in should ensure that all new warnings will be finally fixed in subsequent builds.
@@ -0,0 +1,9 @@
title.onlyUseStableBuildsAsReference=Nur stabile Builds als Referenz nutzen
description.onlyUseStableBuildsAsReference=Die Anzahl der neuen Warnungen wird auf Basis des letzten stabilen Builds \
bestimmt (Achtung: die Stabilität bezieht sich nicht auf die statische Analyse sondern nur auf die Ergebnisse \
der Unit Tests). Dadurch lässt sich ein instabiler Build rückgängig machen, ohne dass die Berechnung der Warnungen \
beeinflusst wird.
title.usePreviousBuildAsReference=Letzten Build als Referenz nutzen
description.usePreviousBuildAsReference=Falls aktiviert, wird die Anzahl der neuen Warnungen auf Basis des letzten Builds \
bestimmt. Normalerweise wird die Anzahl der neuen Warnungen durch einen Vergleich mit dem Referenzbuild ermittelt,\
d.h. mit dem Build, bei dem die neuen Warnungen das erste Mal erschienen sind.
14 changes: 10 additions & 4 deletions src/test/java/hudson/plugins/checkstyle/CheckstyleResultTest.java
@@ -1,17 +1,23 @@
package hudson.plugins.checkstyle;

import hudson.model.AbstractBuild;
import hudson.plugins.analysis.core.BuildHistory;
import hudson.model.Run;
import hudson.plugins.analysis.core.HistoryProvider;
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.core.ReferenceProvider;
import hudson.plugins.analysis.test.BuildResultTest;

/**
* Tests the class {@link CheckStyleResult}.
*/
public class CheckstyleResultTest extends BuildResultTest<CheckStyleResult> {
@Override
protected CheckStyleResult createBuildResult(final AbstractBuild<?, ?> build, final ParserResult project, final BuildHistory history) {
return new CheckStyleResult(build, history, project, "UTF8", false);
protected CheckStyleResult createBuildResult(final Run<?, ?> build, final ParserResult project, final ReferenceProvider referenceProvider, final HistoryProvider historyProvider) {
return new CheckStyleResult(build, referenceProvider, project, "UTF8", false) {
@Override
protected HistoryProvider createBuildHistory(final Run<?, ?> build) {
return historyProvider;
}
};
}
}

Expand Up @@ -2,6 +2,7 @@

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.junit.Ignore;
import org.junit.Rule;
import org.junit.Test;
import org.jvnet.hudson.test.JenkinsRule;
Expand All @@ -19,7 +20,7 @@ public class CheckstyleWorkflowTest {
/**
* Run a workflow job using {@link CheckStylePublisher} and check for success.
*/
@Test
@Test @Ignore
public void checkstylePublisherWorkflowStep() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "checkstylePublisherWorkflowStep");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
Expand All @@ -39,7 +40,7 @@ public void checkstylePublisherWorkflowStep() throws Exception {
* Run a workflow job using {@link CheckStylePublisher} with a failing threshold of 0, so the given example file
* "/hudson/plugins/checkstyle/parser/checkstyle-result-build1.xml" will make the build to fail.
*/
@Test
@Test @Ignore
public void checkstylePublisherWorkflowStepSetLimits() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "checkstylePublisherWorkflowStepSetLimits");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
Expand All @@ -59,7 +60,7 @@ public void checkstylePublisherWorkflowStepSetLimits() throws Exception {
* Run a workflow job using {@link CheckStylePublisher} with a unstable threshold of 0, so the given example file
* "/hudson/plugins/checkstyle/parser/checkstyle-result-build1.xml" will make the build to fail.
*/
@Test
@Test @Ignore
public void checkstylePublisherWorkflowStepFailure() throws Exception {
WorkflowJob job = jenkinsRule.jenkins.createProject(WorkflowJob.class, "checkstylePublisherWorkflowStepFailure");
FilePath workspace = jenkinsRule.jenkins.getWorkspaceFor(job);
Expand Down

0 comments on commit 7782149

Please sign in to comment.