Skip to content

Commit

Permalink
Fixing
Browse files Browse the repository at this point in the history
JENKINS-16837: Don't change build status
  • Loading branch information
ognjenb committed Feb 21, 2013
1 parent f0843cc commit cb492c1
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 29 deletions.
1 change: 1 addition & 0 deletions src/main/java/hudson/plugins/jacoco/JacocoBuildAction.java
Expand Up @@ -270,6 +270,7 @@ public synchronized CoverageReport getResult() {
try {
CoverageReport r = new CoverageReport(this, getJacocoReports(reportFolder, inclusions, exclusions));
report = new WeakReference<CoverageReport>(r);
r.setThresholds(thresholds);
return r;
} catch (IOException e) {
logger.println("Failed to load " + reportFolder);
Expand Down
@@ -1,5 +1,9 @@
package hudson.plugins.jacoco;

import hudson.plugins.jacoco.model.Coverage;
import hudson.plugins.jacoco.model.CoverageElement.Type;
import hudson.plugins.jacoco.report.AbstractReport;

import java.io.Serializable;

import org.kohsuke.stapler.DataBoundConstructor;
Expand Down Expand Up @@ -51,6 +55,8 @@ private int applyRange(int min , int value, int max) {
return value;
}

public enum RESULT {BELLOWMINIMUM, BETWEENMINMAX, ABOVEMAXIMUM};

public void ensureValid() {
maxClass = applyRange(0, maxClass, 100);
minClass = applyRange(0, minClass, maxClass);
Expand Down Expand Up @@ -162,6 +168,51 @@ public void setMaxComplexity(int maxComplexity) {
this.maxComplexity = maxComplexity;
}

public RESULT getResultByTypeAndRatio(Coverage ratio) {
RESULT result = RESULT.ABOVEMAXIMUM;
Type covType = ratio.getType();

if (covType == Type.INSTRUCTION) {
if (ratio.getPercentage() < minInstruction) {
result = RESULT.BELLOWMINIMUM;
} else if (ratio.getPercentage() < maxInstruction) {
result = RESULT.BETWEENMINMAX;
}

} else if (covType == Type.BRANCH) {
if (ratio.getPercentage() < minBranch) {
result = RESULT.BELLOWMINIMUM;
} else if (ratio.getPercentage() < maxBranch) {
result = RESULT.BETWEENMINMAX;
}
} else if (covType == Type.LINE) {
if (ratio.getPercentage() < minLine) {
result = RESULT.BELLOWMINIMUM;
} else if (ratio.getPercentage() < maxLine) {
result = RESULT.BETWEENMINMAX;
}
} else if (covType == Type.COMPLEXITY) {
if (ratio.getPercentage() < minComplexity) {
result = RESULT.BELLOWMINIMUM;
} else if (ratio.getPercentage() < maxComplexity) {
result = RESULT.BETWEENMINMAX;
}
} else if (covType == Type.METHOD) {
if (ratio.getPercentage() < minMethod) {
result = RESULT.BELLOWMINIMUM;
} else if (ratio.getPercentage() < maxMethod) {
result = RESULT.BETWEENMINMAX;
}
} else if (covType == Type.CLASS) {
if (ratio.getPercentage() < minClass) {
result = RESULT.BELLOWMINIMUM;
} else if (ratio.getPercentage() < maxClass) {
result = RESULT.BETWEENMINMAX;
}
}

return result;
}
@Override
public String toString() {
return "JacocoHealthReportThresholds [minClass=" + minClass
Expand Down
42 changes: 28 additions & 14 deletions src/main/java/hudson/plugins/jacoco/JacocoPublisher.java
Expand Up @@ -85,18 +85,30 @@ public JacocoPublisher(String execPattern, String classPattern, String sourcePat
this.sourcePattern = sourcePattern;
this.inclusionPattern = inclusionPattern;
this.exclusionPattern = exclusionPattern;
this.minimumInstructionCoverage = minimumInstructionCoverage;
this.minimumBranchCoverage = minimumBranchCoverage;
this.minimumComplexityCoverage = minimumComplexityCoverage;
this.minimumLineCoverage = minimumLineCoverage;
this.minimumMethodCoverage = minimumMethodCoverage;
this.minimumClassCoverage = minimumClassCoverage;
this.maximumInstructionCoverage = maximumInstructionCoverage;
this.maximumBranchCoverage = maximumBranchCoverage;
this.maximumComplexityCoverage = maximumComplexityCoverage;
this.maximumLineCoverage = maximumLineCoverage;
this.maximumMethodCoverage = maximumMethodCoverage;
this.maximumClassCoverage = maximumClassCoverage;
this.minimumInstructionCoverage = checkThresholdInput(minimumInstructionCoverage);
this.minimumBranchCoverage = checkThresholdInput(minimumBranchCoverage);
this.minimumComplexityCoverage = checkThresholdInput(minimumComplexityCoverage);
this.minimumLineCoverage = checkThresholdInput(minimumLineCoverage);
this.minimumMethodCoverage = checkThresholdInput(minimumMethodCoverage);
this.minimumClassCoverage = checkThresholdInput(minimumClassCoverage);
this.maximumInstructionCoverage = checkThresholdInput(maximumInstructionCoverage);
this.maximumBranchCoverage = checkThresholdInput(maximumBranchCoverage);
this.maximumComplexityCoverage = checkThresholdInput(maximumComplexityCoverage);
this.maximumLineCoverage = checkThresholdInput(maximumLineCoverage);
this.maximumMethodCoverage = checkThresholdInput(maximumMethodCoverage);
this.maximumClassCoverage = checkThresholdInput(maximumClassCoverage);
}

public String checkThresholdInput(String input) {
if ((input == null) || ("".equals(input))) {
return 0+"";
}
try {
Integer.parseInt(input);
} catch(NumberFormatException nfe) {
return 0+"";
}
return input;
}


Expand Down Expand Up @@ -293,7 +305,7 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene
healthReports = new JacocoHealthReportThresholds(Integer.parseInt(minimumClassCoverage), Integer.parseInt(maximumClassCoverage), Integer.parseInt(minimumMethodCoverage), Integer.parseInt(maximumMethodCoverage), Integer.parseInt(minimumLineCoverage), Integer.parseInt(maximumLineCoverage)
,Integer.parseInt(minimumBranchCoverage), Integer.parseInt(maximumBranchCoverage), Integer.parseInt(minimumInstructionCoverage), Integer.parseInt(maximumInstructionCoverage), Integer.parseInt(minimumComplexityCoverage), Integer.parseInt(maximumComplexityCoverage));
} catch(NumberFormatException nfe) {
healthReports = new JacocoHealthReportThresholds(0,80,0,80,0,80,0,80,0,80,0,80);
healthReports = new JacocoHealthReportThresholds(0,0,0,0,0,0,0,0,0,0,0,0);
}

if ((execPattern==null) || (classPattern==null) || (sourcePattern==null)) {
Expand Down Expand Up @@ -367,11 +379,13 @@ public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListene

logger.println("[JaCoCo plugin] Publishing the results..");
final CoverageReport result = action.getResult();

if (result == null) {
logger.println("[JaCoCo plugin] Could not parse coverage results. Setting Build to failure.");
build.setResult(Result.FAILURE);
} else {
result.setThresholds(healthReports);
}
build.setResult(checkResult(action));
return true;
}

Expand Down
51 changes: 42 additions & 9 deletions src/main/java/hudson/plugins/jacoco/report/CoverageReport.java
Expand Up @@ -3,23 +3,17 @@
import hudson.model.AbstractBuild;
import hudson.plugins.jacoco.ExecutionFileLoader;
import hudson.plugins.jacoco.JacocoBuildAction;
import hudson.plugins.jacoco.JacocoHealthReportThresholds;
import hudson.plugins.jacoco.model.Coverage;
import hudson.plugins.jacoco.model.CoverageElement;
import hudson.plugins.jacoco.model.CoverageObject;
import hudson.util.IOException2;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;
import java.text.DecimalFormatSymbols;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Locale;

import org.jacoco.core.analysis.IBundleCoverage;
import org.jacoco.core.analysis.IClassCoverage;
import org.jacoco.core.analysis.ICoverageNode;
import org.jacoco.core.analysis.IMethodCoverage;
import org.jacoco.core.analysis.IPackageCoverage;

Expand All @@ -36,7 +30,14 @@ private CoverageReport(JacocoBuildAction action) {
this.action = action;
setName("Jacoco");
}


private String instructionColor;
private String classColor;
private String branchColor;
private String complexityColor;
private String lineColor;
private String methodColor;
public JacocoHealthReportThresholds healthReports;

/**
* Loads the exec files using JaCoCo API. Creates the reporting objects and the report tree.
Expand Down Expand Up @@ -92,10 +93,29 @@ public CoverageReport(JacocoBuildAction action, ExecutionFileLoader executionFil
}
}


static NumberFormat dataFormat = new DecimalFormat("000.00", new DecimalFormatSymbols(Locale.US));
static NumberFormat percentFormat = new DecimalFormat("0.0", new DecimalFormatSymbols(Locale.US));
static NumberFormat intFormat = new DecimalFormat("0", new DecimalFormatSymbols(Locale.US));

@Override
protected void printRatioCell(boolean failed, Coverage ratio, StringBuilder buf) {
if (ratio != null && ratio.isInitialized()) {
//String className = "nowrap" + (failed ? " red" : "");
String bgColor = "#FFFFFF";

if (JacocoHealthReportThresholds.RESULT.BETWEENMINMAX == healthReports.getResultByTypeAndRatio(ratio)) {
bgColor = "#FF8000";
} else if (JacocoHealthReportThresholds.RESULT.BELLOWMINIMUM == healthReports.getResultByTypeAndRatio(ratio)) {
bgColor = "#FF0000";
}
buf.append("<td bgcolor=\" "+ bgColor +" \" class='").append("").append("'");
buf.append(" data='").append(dataFormat.format(ratio.getPercentageFloat()));
buf.append("'>\n");
printRatioTable(ratio, buf);
buf.append("</td>\n");
}
}

@Override
protected void printRatioTable(Coverage ratio, StringBuilder buf){
String percent = percentFormat.format(ratio.getPercentageFloat());
Expand Down Expand Up @@ -128,4 +148,17 @@ public AbstractBuild<?,?> getBuild() {
}


public void setThresholds(JacocoHealthReportThresholds healthReports) {
this.healthReports = healthReports;
/*if (healthReports.getMaxBranch() < branch.getPercentage()) {
branchColor = "#000000";
} else if (healthReports.getMinBranch() < branch.getPercentage()) {
branchColor = "#FF8000";
} else {
branchColor = "#FF0000";
}
*/
}


}
Expand Up @@ -77,22 +77,22 @@
title="${%health.100.title}"/>
</th>
<td>
<f:textbox field="maximumInstructionCoverage" default="80" />
<f:textbox field="maximumInstructionCoverage" default="0" />
</td>
<td>
<f:textbox field="maximumBranchCoverage" default="80" />
<f:textbox field="maximumBranchCoverage" default="0" />
</td>
<td>
<f:textbox field="maximumComplexityCoverage" default="80" />
<f:textbox field="maximumComplexityCoverage" default="0" />
</td>
<td>
<f:textbox field="maximumLineCoverage" default="80" />
<f:textbox field="maximumLineCoverage" default="0" />
</td>
<td>
<f:textbox field="maximumMethodCoverage" default="70" />
<f:textbox field="maximumMethodCoverage" default="0" />
</td>
<td>
<f:textbox field="maximumClassCoverage" default="100" />
<f:textbox field="maximumClassCoverage" default="0" />
</td>
</tr>
<tr>
Expand Down

0 comments on commit cb492c1

Please sign in to comment.