diff options
author | Zipeng WU <zipeng.wu@sonarsource.com> | 2023-01-10 15:17:47 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2023-01-10 20:03:01 +0000 |
commit | 215dd7343dc55f91613b820d43c208c19a50d176 (patch) | |
tree | 4618c053c36e2d3fd919974b4a122028a8205884 /server/sonar-webserver-webapi | |
parent | 61999c500e5bac37b73f868a954d87710c7f8574 (diff) | |
download | sonarqube-215dd7343dc55f91613b820d43c208c19a50d176.tar.gz sonarqube-215dd7343dc55f91613b820d43c208c19a50d176.zip |
SONAR-17815 isCaycCompliant flag should be false with extra non-CAYC conditions
Diffstat (limited to 'server/sonar-webserver-webapi')
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; @@ -53,6 +55,17 @@ public class QualityGateCaycCheckerTest { } @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(); |