Skip to content
Permalink
Browse files

JENKINS-39410: removed @stapler-constructor annotation from the Clove…

…rPublisher constructor, because stapler handles simple types only (primitive, strings but not CoverageTarget); binding request parameters using a set of StaplerRequest.getParameter calls, instead of deprecated StaplerRequest.bindParameters (which was failing on our annotated constructor); added null-check for failingMetrics field
  • Loading branch information
mparfianowicz committed Dec 9, 2016
1 parent 9757d8a commit 0591eeabb798f2fb656c2c391324216fbae4715f
Showing with 28 additions and 6 deletions.
  1. +28 −6 src/main/java/hudson/plugins/clover/CloverPublisher.java
@@ -24,6 +24,7 @@

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;

@@ -64,7 +65,6 @@ public CloverPublisher(String cloverReportDir, String cloverReportFileName) {
* @param healthyTarget target values for healthy build
* @param unhealthyTarget target values for unhealthy build
* @param failingTarget target values for failing build
* @stapler-constructor
*/
@DataBoundConstructor
public CloverPublisher(String cloverReportDir, String cloverReportFileName, CoverageTarget healthyTarget, CoverageTarget unhealthyTarget, CoverageTarget failingTarget) {
@@ -236,7 +236,10 @@ public String invoke(File file, VirtualChannel virtualChannel) throws IOExceptio
build.setResult(Result.FAILURE);
}
build.addAction(CloverBuildAction.load(workspacePath, result, healthyTarget, unhealthyTarget));
Set<CoverageMetric> failingMetrics = failingTarget.getFailingMetrics(result);

final Set<CoverageMetric> failingMetrics = failingTarget != null
? failingTarget.getFailingMetrics(result)
: Collections.<CoverageMetric>emptySet();
if (!failingMetrics.isEmpty()) {
listener.getLogger().println("Code coverage enforcement failed for the following metrics:");
for (CoverageMetric metric : failingMetrics) {
@@ -368,10 +371,13 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
*/
@Override
public CloverPublisher newInstance(StaplerRequest req, JSONObject formData) throws FormException {
CloverPublisher instance = req.bindParameters(CloverPublisher.class, "clover.");
req.bindParameters(instance.failingTarget, "cloverFailingTarget.");
req.bindParameters(instance.healthyTarget, "cloverHealthyTarget.");
req.bindParameters(instance.unhealthyTarget, "cloverUnhealthyTarget.");
final CloverPublisher instance = new CloverPublisher(
req.getParameter("clover.cloverReportDir"),
req.getParameter("clover.cloverReportFileName"),
fromRequest(req, "cloverHealthyTarget."),
fromRequest(req, "cloverUnhealthyTarget."),
fromRequest(req, "cloverFailingTarget.")
);
// start ugly hack
if (instance.healthyTarget.isEmpty()) {
instance.healthyTarget = new CoverageTarget(70, 80, 80);
@@ -384,5 +390,21 @@ public CloverPublisher newInstance(StaplerRequest req, JSONObject formData) thro
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}

private CoverageTarget fromRequest(StaplerRequest req, String namePrefix) {
return new CoverageTarget(
getIntParameter(req, namePrefix + ".methodCoverage"),
getIntParameter(req, namePrefix + ".conditionalCoverage"),
getIntParameter(req, namePrefix + ".statementCoverage")
);
}

private Integer getIntParameter(StaplerRequest req, String name) {
try {
return Integer.valueOf(req.getParameter(name));
} catch (NumberFormatException ex) {
return null;
}
}
}
}

0 comments on commit 0591eea

Please sign in to comment.
You can’t perform that action at this time.