Skip to content

Commit

Permalink
JENKINS-47639 Display a message when missing coverage fails build (#81)
Browse files Browse the repository at this point in the history
* JENKINS-47639 Display a message when missing coverage fails build

* Update comment for CoberturaAbortException

* Fix build error

* Minor format change

* Add license to POM
  • Loading branch information
jxpearce-godaddy committed Nov 11, 2017
1 parent f244b1f commit 544b248
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
8 changes: 7 additions & 1 deletion pom.xml
Expand Up @@ -20,6 +20,13 @@
<workflow-jenkins-plugin.version>1.11</workflow-jenkins-plugin.version>
</properties>

<licenses>
<license>
<name>MIT License</name>
<url>http://opensource.org/licenses/MIT</url>
</license>
</licenses>

<scm>
<connection>scm:git:git://github.com/jenkinsci/cobertura-plugin.git</connection>
<developerConnection>scm:git:git@github.com:jenkinsci/cobertura-plugin.git</developerConnection>
Expand Down Expand Up @@ -217,4 +224,3 @@
</pluginRepository>
</pluginRepositories>
</project>

Expand Up @@ -3,12 +3,15 @@
import hudson.AbortException;

/**
*
* @author jeffpearce
* Common abort exception that should be thrown to abort the build. Prepends
* a common prefix to the abort message.
*/
public class CoberturaAbortException extends AbortException {
/**
* CoberturaAbortException constructor
* @param message the abort message
*/
public CoberturaAbortException(String message) {
super("[Cobertura] " + message);
}

}
36 changes: 28 additions & 8 deletions src/main/java/hudson/plugins/cobertura/CoberturaBuildAction.java
Expand Up @@ -17,7 +17,6 @@
import java.io.File;
import java.io.IOException;
import java.lang.ref.WeakReference;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
Expand All @@ -42,19 +41,21 @@ public class CoberturaBuildAction implements HealthReportingAction, StaplerProxy
private transient Run<?, ?> owner;
private CoverageTarget healthyTarget;
private CoverageTarget unhealthyTarget;
private boolean failUnhealthy;
private boolean failUnstable;
private boolean autoUpdateHealth;
private boolean autoUpdateStability;
private boolean zoomCoverageChart;
private int maxNumberOfBuilds;
private final boolean failUnhealthy;
private final boolean failUnstable;
private final boolean autoUpdateHealth;
private final boolean autoUpdateStability;
private final boolean zoomCoverageChart;
private final int maxNumberOfBuilds;
/**
* Overall coverage result.
*/
private Map<CoverageMetric, Ratio> result;
private HealthReport health = null;
private transient WeakReference<CoverageResult> report;
private boolean onlyStable;
private final boolean onlyStable;

private String failMessage = null;

/**
* {@inheritDoc}
Expand Down Expand Up @@ -158,6 +159,25 @@ private synchronized void setOwner(Run<?, ?> owner) {
}
}

/**
* Getter for property 'failMessage'
*
* @return Value for property 'failMessage'
*/
public String getFailMessage() {
return failMessage;
}

/**
* Setter for property 'failMessage'
*
* @param failMessage value to set for 'failMessage'
*/
public void setFailMessage(String failMessage) {
this.failMessage = failMessage;
}


public Map<CoverageMetric, Ratio> getResults() {
return result;
}
Expand Down
Expand Up @@ -42,7 +42,6 @@
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
import jdk.nashorn.internal.ir.BreakableNode;

import net.sf.json.JSONArray;
import net.sf.json.JSONException;
Expand Down Expand Up @@ -635,6 +634,7 @@ unhealthyTarget, getOnlyStable(), getFailUnhealthy(), getFailUnstable(), getAuto
logMessage(listener, "Setting Build to unstable.");
build.setResult(Result.UNSTABLE);
} else {
action.setFailMessage(String.format("Build failed because following metrics did not meet stability target: %s.", failingMetrics.toString()));
throw new CoberturaAbortException("Failing build due to unstability.");
}
}
Expand All @@ -649,6 +649,7 @@ unhealthyTarget, getOnlyStable(), getFailUnhealthy(), getFailUnstable(), getAuto
setHealthyPercent = unhealthyTarget.getSetPercent(result, metric);
listener.getLogger().println(" " + metric.getName() + "'s health is " + roundDecimalFloat(oldHealthyPercent * 100f) + " and set minimum health is " + roundDecimalFloat(setHealthyPercent * 100f) + ".");
}
action.setFailMessage(String.format("Build failed because following metrics did not meet health target: %s.", unhealthyMetrics.toString()));
throw new CoberturaAbortException("Failing build because it is unhealthy.");
}
}
Expand Down
Expand Up @@ -11,6 +11,12 @@
&amp;nbsp;
</j:forEach>
</div>
<j:set var="failMessage" value="${it.failMessage}" />
<j:if test="${failMessage != null}">
<div style="margin: 0 0 0 1ex">
${failMessage}
</div>
</j:if>
</j:if>
</t:summary>
</j:jelly>

0 comments on commit 544b248

Please sign in to comment.