Skip to content

Commit

Permalink
Add support for coverage thresholds and tolerance of minor drops in c…
Browse files Browse the repository at this point in the history
…overage in status checks
  • Loading branch information
storpeyCHT authored and michael-fredrickson committed Aug 20, 2020
1 parent d96f742 commit d6b4c8b
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,17 @@ public class CompareCoverageAction extends Recorder implements SimpleBuildStep {
private Map<String, String> scmVars;
private String jacocoCoverageCounter;
private String publishResultAs;
private String comparisonMode;

@DataBoundConstructor
public CompareCoverageAction() {
}

@DataBoundSetter
public void setComparisonMode(String comparisonMode) {
this.comparisonMode = comparisonMode;
}

@DataBoundSetter
public void setPublishResultAs(String publishResultAs) {
this.publishResultAs = publishResultAs;
Expand Down Expand Up @@ -100,6 +106,10 @@ public void setJacocoCoverageCounter(String jacocoCoverageCounter) {
this.jacocoCoverageCounter = jacocoCoverageCounter;
}

public String getComparisonMode() {
return comparisonMode;
}

public String getPublishResultAs() {
return publishResultAs;
}
Expand Down Expand Up @@ -156,7 +166,7 @@ public void perform(
publishComment(message, buildUrl, jenkinsUrl, settingsRepository, gitHubRepository, prId, listener);
} else {
buildLog.println(BUILD_LOG_PREFIX + "publishing result as status check");
publishStatusCheck(message, gitHubRepository, prId, masterCoverage, coverage, buildUrl, listener);
publishStatusCheck(message, settingsRepository, gitHubRepository, prId, masterCoverage, coverage, buildUrl, listener);
}
}

Expand Down Expand Up @@ -185,6 +195,7 @@ private void publishComment(

private void publishStatusCheck(
Message message,
SettingsRepository settingsRepository,
GHRepository gitHubRepository,
int prId,
float targetCoverage,
Expand All @@ -195,12 +206,27 @@ private void publishStatusCheck(
try {
String text = message.forStatusCheck();
List<GHPullRequestCommitDetail> commits = gitHubRepository.getPullRequest(prId).listCommits().asList();
String statusMessage;
boolean comparison;
if ("threshold".equalsIgnoreCase(comparisonMode)){
comparison = Percent.of(coverage) <= settingsRepository.getGreenThreshold();
statusMessage = message.forThresholdStatusCheck( settingsRepository.getGreenThreshold() );
} else if ("tolerance".equalsIgnoreCase(comparisonMode)){
float tolerance = settingsRepository.getTolerance();
float result = Math.abs(coverage - targetCoverage) * 100;
comparison = result > tolerance;
statusMessage = message.forToleranceStatusCheck(tolerance);
} else {
comparison = Percent.roundFourAfterDigit(coverage) < Percent.roundFourAfterDigit(targetCoverage);
statusMessage = message.forStatusCheck();
}

ServiceRegistry.getPullRequestRepository().createCommitStatus(
gitHubRepository,
commits.get(commits.size() - 1).getSha(),
coverage < targetCoverage ? GHCommitState.FAILURE : GHCommitState.SUCCESS,
comparison ? GHCommitState.FAILURE : GHCommitState.SUCCESS,
buildUrl,
message.forStatusCheck()
statusMessage
);
} catch (Exception e) {
PrintWriter pw = listener.error("Couldn't add status check to pull request #" + prId + "!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@ public static String getSonarToken() {
return DESCRIPTOR.getSonarToken();
}

public static String getTolerance() {
return Float.toString(DESCRIPTOR.getTolerance());
}

public static String getSonarLogin() {
return DESCRIPTOR.getSonarLogin();
}
Expand All @@ -91,6 +95,7 @@ public static final class ConfigurationDescriptor extends Descriptor<Configurati

private static final int DEFAULT_YELLOW_THRESHOLD = 80;
private static final int DEFAULT_GREEN_THRESHOLD = 90;
private static final float DEFAULT_TOLERANCE = 0;

private final Map<String, Float> coverageByRepo = new ConcurrentHashMap<String, Float>();

Expand All @@ -107,6 +112,7 @@ public static final class ConfigurationDescriptor extends Descriptor<Configurati

private int yellowThreshold = DEFAULT_YELLOW_THRESHOLD;
private int greenThreshold = DEFAULT_GREEN_THRESHOLD;
private float tolerance = DEFAULT_TOLERANCE;

public ConfigurationDescriptor() {
load();
Expand Down Expand Up @@ -147,6 +153,11 @@ public int getGreenThreshold() {
return greenThreshold;
}

@Override
public float getTolerance() {
return tolerance;
}

@Override
public boolean isPrivateJenkinsPublicGitHub() {
return privateJenkinsPublicGitHub;
Expand Down Expand Up @@ -191,6 +202,7 @@ public boolean configure(StaplerRequest req, JSONObject formData) throws FormExc
personalAccessToken = StringUtils.trimToNull(formData.getString("personalAccessToken"));
yellowThreshold = NumberUtils.toInt(formData.getString("yellowThreshold"), DEFAULT_YELLOW_THRESHOLD);
greenThreshold = NumberUtils.toInt(formData.getString("greenThreshold"), DEFAULT_GREEN_THRESHOLD);
tolerance = NumberUtils.toFloat(formData.getString("tolerance"), DEFAULT_TOLERANCE);
jenkinsUrl = StringUtils.trimToNull(formData.getString("jenkinsUrl"));
privateJenkinsPublicGitHub = BooleanUtils.toBoolean(formData.getString("privateJenkinsPublicGitHub"));
useSonarForMasterCoverage = BooleanUtils.toBoolean(formData.getString("useSonarForMasterCoverage"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,21 @@ public String forComment(
}

public String forStatusCheck() {
return String.format("Coverage %s changed %s vs master %s",
return forConsole();
}

public String forThresholdStatusCheck(final int greenThreshold){
return String.format("Coverage %s vs threshold %s",
Percent.toWholeNoSignString(coverage),
Integer.toString(greenThreshold));
}

public String forToleranceStatusCheck(float tolerance){
return String.format("Coverage %s changed %s vs master %s (tolerance %s)",
Percent.toWholeNoSignString(coverage),
Percent.toString(Percent.change(coverage, masterCoverage)),
Percent.toWholeNoSignString(masterCoverage));
Percent.toWholeNoSignString(masterCoverage),
Float.toString(tolerance));
}

private String shieldIoUrl(String icon, final int yellowThreshold, final int greenThreshold) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ interface SettingsRepository {
int getYellowThreshold();

int getGreenThreshold();

float getTolerance();

boolean isPrivateJenkinsPublicGitHub();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,11 @@
<f:option value="statusCheck">${%Status Check}</f:option>
</select>
</f:entry>
<f:entry title="${%Mode of comparing against master}" field="comparisonMode">
<select name="comparisonMode">
<f:option value="Strict">${%Strict}</f:option>
<f:option value="threshold">${%Threshold}</f:option>
<f:option value="tolerance">${%Tolerance}</f:option>
</select>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ f.section(title: descriptor.displayName) {
f.entry(field: "greenThreshold", title: _("Green Threshold 0-100%")) {
f.textbox()
}

f.entry(field: "tolerance", title: _("Tolerance for comparing to master 0.0-1.0")) {
f.textbox()
}

f.entry(field: "useSonarForMasterCoverage", title: _("Use Sonar for master coverage")) {
f.checkbox()
Expand Down

0 comments on commit d6b4c8b

Please sign in to comment.