Skip to content

Commit

Permalink
Send total coverage back to GitLab instead of new coverage
Browse files Browse the repository at this point in the history
The coverage send back via the GitLab API should be the total coverage
of the project, not the coverage of the new code in the PR. The GitLab
docs explicitly mention this:
https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit

The coverage reported by decorating the PR via a comment is not affected;
this still shows both the total and new coverage.

Note that there is an open GitLab issue for natively displaying new coverage
for just the diff of a PR (https://gitlab.com/gitlab-org/gitlab/-/issues/20895),
but this is not yet implemented.

In addition, reporting coverage back is optional. So if there is no coverage
information, we do not report it back.
  • Loading branch information
Teake Nutma authored and mc1arke committed Mar 6, 2021
1 parent 9d1dac3 commit 3d2bd86
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,7 @@ public Optional<String> getScannerProperty(String propertyName) {
public String createAnalysisSummary(FormatterFactory formatterFactory) {

BigDecimal newCoverage = getNewCoverage().orElse(null);


double coverage = findMeasure(CoreMetrics.COVERAGE_KEY).map(Measure::getDoubleValue).orElse(0D);
BigDecimal coverage = getCoverage().orElse(null);

BigDecimal newDuplications = findQualityGateCondition(CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY)
.filter(condition -> condition.getStatus() != EvaluationStatus.NO_VALUE)
Expand Down Expand Up @@ -373,6 +371,10 @@ public Optional<BigDecimal> getNewCoverage(){
.map(BigDecimal::new);
}

public Optional<BigDecimal> getCoverage(){
return findMeasure(CoreMetrics.COVERAGE_KEY).map(Measure::getDoubleValue).map(BigDecimal::new);
}

public static class BranchDetails {

private final String branchName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ public DecorationResult decorateQualityGateStatus(AnalysisDetails analysis, AlmS
List<NameValuePair> summaryContentParams = Collections
.singletonList(new BasicNameValuePair("body", summaryComment));

String coverageValue = analysis.getNewCoverage().orElse(BigDecimal.ZERO).toString();
BigDecimal coverageValue = analysis.getCoverage().orElse(null);

postStatus(new StringBuilder(statusUrl), headers, analysis, coverageValue);

Expand Down Expand Up @@ -315,7 +315,7 @@ private void postCommitComment(String commitCommentUrl, Map<String, String> head
}

private void postStatus(StringBuilder statusPostUrl, Map<String, String> headers, AnalysisDetails analysis,
String coverage) throws IOException {
BigDecimal coverage) throws IOException {
//See https://docs.gitlab.com/ee/api/commits.html#post-the-build-status-to-a-commit
statusPostUrl.append("?name=SonarQube");
String status = (analysis.getQualityGateStatus() == QualityGate.Status.OK ? "success" : "failed");
Expand All @@ -326,7 +326,9 @@ private void postStatus(StringBuilder statusPostUrl, Map<String, String> headers
.encode(analysis.getBranchName(),
StandardCharsets.UTF_8.name())), StandardCharsets.UTF_8.name()));
statusPostUrl.append("&description=").append(URLEncoder.encode("SonarQube Status", StandardCharsets.UTF_8.name()));
statusPostUrl.append("&coverage=").append(coverage);
if (coverage != null) {
statusPostUrl.append("&coverage=").append(coverage.toString());
}
analysis.getScannerProperty(PULLREQUEST_GITLAB_PIPELINE_ID).ifPresent(pipelineId -> statusPostUrl.append("&pipeline_id=").append(pipelineId));

HttpPost httpPost = new HttpPost(statusPostUrl.toString());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void decorateQualityGateStatus() {
when(analysisDetails.getAnalysisProjectKey()).thenReturn(projectKey);
when(analysisDetails.getBranchName()).thenReturn(branchName);
when(analysisDetails.getCommitSha()).thenReturn(commitSHA);
when(analysisDetails.getNewCoverage()).thenReturn(Optional.of(BigDecimal.TEN));
when(analysisDetails.getCoverage()).thenReturn(Optional.of(BigDecimal.TEN));
PostAnalysisIssueVisitor issueVisitor = mock(PostAnalysisIssueVisitor.class);
PostAnalysisIssueVisitor.ComponentIssue componentIssue = mock(PostAnalysisIssueVisitor.ComponentIssue.class);
PostAnalysisIssueVisitor.LightIssue defaultIssue = mock(PostAnalysisIssueVisitor.LightIssue.class);
Expand Down

0 comments on commit 3d2bd86

Please sign in to comment.