From 6ae9b9d3d3c3ac7e868db74fb02a0554ba3a21d9 Mon Sep 17 00:00:00 2001 From: Dimitris Kavvathas Date: Mon, 9 Jan 2023 14:19:17 +0100 Subject: [PATCH] SONAR-17818 Add coverage and duplication conditions by default on new Quality Gates --- .../qualitygate/QualityGateCaycChecker.java | 9 +++++++-- .../server/qualitygate/ws/CreateAction.java | 18 +++++++++++++++++- .../QualityGateCaycCheckerTest.java | 6 +++++- .../qualitygate/ws/CreateActionTest.java | 17 +++++++++++++++-- 4 files changed, 44 insertions(+), 6 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 94afc9a3462..49446e85d2d 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 @@ -19,6 +19,7 @@ */ package org.sonar.server.qualitygate; +import java.io.Serializable; import java.util.List; import java.util.Map; import java.util.Optional; @@ -30,6 +31,8 @@ import org.sonar.db.metric.MetricDto; import org.sonar.db.qualitygate.QualityGateConditionDto; import static java.util.stream.Collectors.toUnmodifiableMap; +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; import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING; import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED; @@ -38,11 +41,13 @@ import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; public class QualityGateCaycChecker { - public static final List> CAYC_METRICS = List.of( + public static final List> CAYC_METRICS = List.of( NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, - NEW_SECURITY_RATING + NEW_SECURITY_RATING, + NEW_DUPLICATED_LINES_DENSITY, + NEW_COVERAGE ); private static final Map CAYC_REQUIREMENTS = CAYC_METRICS.stream() diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java index d394d5375ba..cad83a28e84 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java @@ -19,7 +19,10 @@ */ package org.sonar.server.qualitygate.ws; +import java.io.Serializable; import java.util.Map; +import java.util.Objects; +import org.sonar.api.measures.Metric; import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; @@ -34,6 +37,8 @@ import org.sonar.server.qualitygate.QualityGateUpdater; import org.sonar.server.user.UserSession; import org.sonarqube.ws.Qualitygates.CreateResponse; +import static org.sonar.api.measures.CoreMetrics.NEW_COVERAGE_KEY; +import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY_KEY; import static org.sonar.api.measures.Metric.DIRECTION_BETTER; import static org.sonar.api.measures.Metric.DIRECTION_WORST; import static org.sonar.server.qualitygate.Condition.Operator.GREATER_THAN; @@ -45,6 +50,11 @@ import static org.sonar.server.ws.WsUtils.writeProtobuf; public class CreateAction implements QualityGatesWsAction { + static final Map DEFAULT_METRIC_VALUES = Map.of( + NEW_COVERAGE_KEY, 80, + NEW_DUPLICATED_LINES_DENSITY_KEY, 3 + ); + private static final Map OPERATORS_BY_DIRECTION = Map.of( DIRECTION_BETTER, LESS_THAN, DIRECTION_WORST, GREATER_THAN); @@ -103,7 +113,13 @@ public class CreateAction implements QualityGatesWsAction { private void addCaycConditions(DbSession dbSession, QualityGateDto newQualityGate) { CAYC_METRICS.forEach(m -> qualityGateConditionsUpdater.createCondition(dbSession, newQualityGate, m.getKey(), OPERATORS_BY_DIRECTION.get(m.getDirection()).getDbValue(), - String.valueOf(m.getBestValue().intValue())) + String.valueOf(getDefaultCaycValue(m))) ); } + + private static int getDefaultCaycValue(Metric metric) { + return DEFAULT_METRIC_VALUES.containsKey(metric.getKey()) ? + DEFAULT_METRIC_VALUES.get(metric.getKey()) : + Objects.requireNonNull(metric.getBestValue()).intValue(); + } } 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 e9896afd8f6..d1cf22fd214 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 @@ -19,6 +19,7 @@ */ package org.sonar.server.qualitygate; +import java.io.Serializable; import java.util.List; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -32,6 +33,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.NEW_COVERAGE; +import static org.sonar.api.measures.CoreMetrics.NEW_DUPLICATED_LINES_DENSITY; import static org.sonar.api.measures.CoreMetrics.NEW_MAINTAINABILITY_RATING; import static org.sonar.api.measures.CoreMetrics.NEW_RELIABILITY_RATING; import static org.sonar.api.measures.CoreMetrics.NEW_SECURITY_HOTSPOTS_REVIEWED; @@ -46,7 +49,8 @@ public class QualityGateCaycCheckerTest { @Test public void checkCaycCompliant() { String qualityGateUuid = "abcd"; - List> CAYC_REQUIREMENT_METRICS = List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING); + List> CAYC_REQUIREMENT_METRICS = List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING, + NEW_DUPLICATED_LINES_DENSITY, NEW_COVERAGE); CAYC_REQUIREMENT_METRICS .forEach(metric -> insertCondition(insertMetric(metric), qualityGateUuid, metric.getBestValue())); assertThat(underTest.checkCaycCompliant(db.getSession(), qualityGateUuid)).isTrue(); diff --git a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java index 88cba16e754..a7b880a9c82 100644 --- a/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java +++ b/server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java @@ -36,6 +36,7 @@ import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.metric.MetricDto; import org.sonar.db.qualitygate.QualityGateConditionDto; import org.sonar.db.qualitygate.QualityGateDto; import org.sonar.server.exceptions.ForbiddenException; @@ -50,6 +51,7 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.sonar.db.permission.GlobalPermission.ADMINISTER_QUALITY_GATES; import static org.sonar.server.qualitygate.QualityGateCaycChecker.CAYC_METRICS; +import static org.sonar.server.qualitygate.ws.CreateAction.DEFAULT_METRIC_VALUES; import static org.sonar.server.qualitygate.ws.QualityGatesWsParameters.PARAM_NAME; @RunWith(DataProviderRunner.class) @@ -90,8 +92,8 @@ public class CreateActionTest { CAYC_METRICS.stream() .map(m -> dbClient.metricDao().selectByKey(dbSession, m.getKey())) - .forEach(metricDto -> - assertThat(conditions).anyMatch(c -> metricDto.getUuid().equals(c.getMetricUuid()))); + .forEach(metricDto -> assertThat(conditions) + .anyMatch(c -> metricDto.getUuid().equals(c.getMetricUuid()) && c.getErrorThreshold().equals(String.valueOf(getDefaultCaycValue(metricDto))))); } @Test @@ -167,4 +169,15 @@ public class CreateActionTest { .setHidden(metric.isHidden()) .setDirection(metric.getDirection())); } + + private Integer getDefaultCaycValue(MetricDto metricDto) { + return DEFAULT_METRIC_VALUES.containsKey(metricDto.getKey()) + ? DEFAULT_METRIC_VALUES.get(metricDto.getKey()) + : CAYC_METRICS.stream() + .filter(metric -> metricDto.getKey().equals(metric.getKey())) + .findAny() + .orElseThrow() + .getBestValue().intValue(); + } + } -- 2.39.5