Skip to content

Commit

Permalink
Removed exception "IllegalStateException" if could not find New Cover…
Browse files Browse the repository at this point in the history
…age Condition in analysis and replaced to "0" mc1arke#85
  • Loading branch information
Grisha authored and Grisha committed Mar 13, 2020
1 parent d258ef9 commit fafd034
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,17 @@ public String createAnalysisSummary(FormatterFactory formatterFactory) {

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

BigDecimal newDuplications = getNewDuplications().orElse(null);

double coverage = findMeasure(CoreMetrics.COVERAGE_KEY).map(MeasureWrapper::getDoubleValue).orElse(0D);

double duplications = findMeasure(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY).map(MeasureWrapper::getDoubleValue).orElse(0D);
BigDecimal newDuplications = findQualityGateCondition(CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY)
.filter(condition -> condition.getStatus() != EvaluationStatus.NO_VALUE)
.map(QualityGate.Condition::getValue)
.map(BigDecimal::new)
.orElse(null);

double duplications =
findMeasure(CoreMetrics.DUPLICATED_LINES_DENSITY_KEY).map(MeasureWrapper::getDoubleValue).orElse(0D);

NumberFormat decimalFormat = new DecimalFormat("#0.00", DecimalFormatSymbols.getInstance(Locale.ENGLISH));

Expand Down Expand Up @@ -270,7 +276,7 @@ private Optional<MeasureWrapper> findMeasure(String metricKey) {
.map(MeasureWrapper::new);
}

public Optional<QualityGate.Condition> findQualityGateCondition(String metricKey) {
private Optional<QualityGate.Condition> findQualityGateCondition(String metricKey) {
return qualityGate.getConditions().stream().filter(c -> metricKey.equals(c.getMetricKey())).findFirst();
}

Expand Down Expand Up @@ -309,13 +315,6 @@ private static String format(QualityGate.Condition condition) {
}
}

public Optional<BigDecimal> getNewDuplications() {
return findQualityGateCondition(CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY)
.filter(condition -> condition.getStatus() != EvaluationStatus.NO_VALUE)
.map(QualityGate.Condition::getValue)
.map(BigDecimal::new);
}

public Optional<BigDecimal> getNewCoverage(){
return findQualityGateCondition(CoreMetrics.NEW_COVERAGE_KEY)
.filter(condition -> condition.getStatus() != EvaluationStatus.NO_VALUE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ public void decorateQualityGateStatus(AnalysisDetails analysis, UnifyConfigurati
final String mergeRequestURl = projectURL + String.format("/merge_requests/%s", pullRequestId);
final String prCommitsURL = mergeRequestURl + "/commits";
final String mergeRequestDiscussionURL = mergeRequestURl + "/discussions";
final String mergeRequestNoteURL = mergeRequestURl + "/notes";

LOGGER.info(String.format("Status url is: %s ", statusUrl));
LOGGER.info(String.format("PR commits url is: %s ", prCommitsURL));
Expand Down Expand Up @@ -153,8 +152,7 @@ public void decorateQualityGateStatus(AnalysisDetails analysis, UnifyConfigurati
String summaryComment = analysis.createAnalysisSummary(new MarkdownFormatterFactory());
List<NameValuePair> summaryContentParams = Collections.singletonList(new BasicNameValuePair("body", summaryComment));

boolean isNewCoverage = analysis.getNewCoverage().isPresent();
String coverageValue = isNewCoverage ? analysis.getNewCoverage().get().toString() : "0";
String coverageValue = analysis.getNewCoverage().orElse(BigDecimal.valueOf(0)).toString();

postStatus(statusUrl, headers, analysis, coverageValue, true);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.github.mc1arke.sonarqube.plugin.ce.pullrequest.gitlab;

import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
Expand Down Expand Up @@ -72,15 +73,13 @@ public void decorateQualityGateStatus() {

UnifyConfiguration unifyConfiguration = new UnifyConfiguration(configuration, scannerContext);

QualityGate.Condition coverage = mock(QualityGate.Condition.class);
when(coverage.getStatus()).thenReturn(QualityGate.EvaluationStatus.OK);
when(coverage.getValue()).thenReturn("10");
BigDecimal coverage = BigDecimal.TEN;

AnalysisDetails analysisDetails = mock(AnalysisDetails.class);
when(analysisDetails.getAnalysisProjectKey()).thenReturn(projectKey);
when(analysisDetails.getBranchName()).thenReturn(branchName);
when(analysisDetails.getCommitSha()).thenReturn(commitSHA);
when(analysisDetails.findQualityGateCondition(CoreMetrics.NEW_COVERAGE_KEY)).thenReturn(Optional.of(coverage));
when(analysisDetails.getNewCoverage()).thenReturn(Optional.of(coverage));
PostAnalysisIssueVisitor issueVisitor = mock(PostAnalysisIssueVisitor.class);
PostAnalysisIssueVisitor.ComponentIssue componentIssue = mock(PostAnalysisIssueVisitor.ComponentIssue.class);
DefaultIssue defaultIssue = mock(DefaultIssue.class);
Expand Down Expand Up @@ -139,7 +138,7 @@ public void decorateQualityGateStatus() {
.withQueryParam("name", equalTo("SonarQube"))
.withQueryParam("state", equalTo("failed"))
.withQueryParam("target_url", equalTo(sonarRootUrl + "/dashboard?id=" + projectKey + "&pullRequest=" + branchName))
.withQueryParam("coverage", equalTo(coverage.getValue()))
.withQueryParam("coverage", equalTo(coverage.toString()))
.willReturn(created()));

wireMockRule.stubFor(post(urlPathEqualTo("/api/v4/projects/" + urlEncode(repositorySlug) + "/merge_requests/" + branchName + "/discussions"))
Expand Down

0 comments on commit fafd034

Please sign in to comment.