From 215dd7343dc55f91613b820d43c208c19a50d176 Mon Sep 17 00:00:00 2001 From: Zipeng WU Date: Tue, 10 Jan 2023 15:17:47 +0100 Subject: [PATCH] SONAR-17815 isCaycCompliant flag should be false with extra non-CAYC conditions --- .../server/qualitygate/QualityGateCaycChecker.java | 10 ++++++++-- .../qualitygate/QualityGateCaycCheckerTest.java | 13 +++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateCaycChecker.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateCaycChecker.java index 1bac942bda2..c1ae05f9364 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateCaycChecker.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateCaycChecker.java @@ -24,7 +24,6 @@ import java.util.List; import java.util.Map; import java.util.Optional; import java.util.Set; -import java.util.stream.Collectors; import org.sonar.api.measures.Metric; import org.sonar.db.DbClient; import org.sonar.db.DbSession; @@ -72,13 +71,20 @@ public class QualityGateCaycChecker { var conditionsByMetricId = dbClient.gateConditionDao().selectForQualityGate(dbSession, qualityGateUuid) .stream() .collect(uniqueIndex(QualityGateConditionDto::getMetricUuid)); + + if (conditionsByMetricId.size() != CAYC_METRICS.size()) { + return false; + } + var metrics = dbClient.metricDao().selectByUuids(dbSession, conditionsByMetricId.keySet()) .stream() .filter(MetricDto::isEnabled) - .collect(Collectors.toSet()); + .toList(); + long count = metrics.stream() .filter(metric -> checkMetricCaycCompliant(conditionsByMetricId.get(metric.getUuid()), metric)) .count(); + return count == CAYC_METRICS.size(); } diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateCaycCheckerTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateCaycCheckerTest.java index c86475c5e38..51471717a45 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateCaycCheckerTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateCaycCheckerTest.java @@ -31,6 +31,8 @@ import org.sonar.db.metric.MetricDto; import org.sonar.db.qualitygate.QualityGateConditionDto; import static org.assertj.core.api.Assertions.assertThat; +import static org.sonar.api.measures.CoreMetrics.DUPLICATED_LINES; +import static org.sonar.api.measures.CoreMetrics.LINE_COVERAGE; import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE; import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY; import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING; @@ -52,6 +54,17 @@ public class QualityGateCaycCheckerTest { assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isTrue(); } + @Test + public void check_Cayc_non_compliant_with_extra_conditions() { + String qualityGateUuid = "abcd"; + CAYC_METRICS.forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue())); + + // extra conditions outside of CAYC requirements + List.of(LINE_COVERAGE, DUPLICATED_LINES).forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue())); + + assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isFalse(); + } + @Test public void check_Cayc_NonCompliant_with_lesser_threshold_value() { var metrics = CAYC_METRICS.stream().map(this::insertMetric).toList(); -- 2.39.5