]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-17818 Add coverage and duplication conditions by default on new Quality Gates
authorDimitris Kavvathas <dimitris.kavvathas@sonarsource.com>
Mon, 9 Jan 2023 13:19:17 +0000 (14:19 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 10 Jan 2023 20:03:00 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateCaycChecker.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/CreateAction.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/QualityGateCaycCheckerTest.java
server/sonar-webserver-webapi/src/test/java/org/sonar/server/qualitygate/ws/CreateActionTest.java

index 94afc9a3462b0e1b2bc945454c3f576d325787bd..49446e85d2dc170f881fb7dd56e08225595b80c0 100644 (file)
@@ -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<Metric<Integer>> CAYC_METRICS = List.of(
+  public static final List<Metric<? extends Serializable>> 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<String, Double> CAYC_REQUIREMENTS = CAYC_METRICS.stream()
index d394d5375ba358730ca6ce4d1f05bb3ca291f784..cad83a28e84e99f09cecea95fb3f922f83dc3852 100644 (file)
  */
 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<String, Integer> DEFAULT_METRIC_VALUES = Map.of(
+    NEW_COVERAGE_KEY, 80,
+    NEW_DUPLICATED_LINES_DENSITY_KEY, 3
+  );
+
   private static final Map<Integer, Condition.Operator> 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<? extends Serializable> metric) {
+    return DEFAULT_METRIC_VALUES.containsKey(metric.getKey()) ?
+      DEFAULT_METRIC_VALUES.get(metric.getKey()) :
+      Objects.requireNonNull(metric.getBestValue()).intValue();
+  }
 }
index e9896afd8f663fbd36d77053e33e14dbe4420b48..d1cf22fd214641b5567381565a37c0785d8898ec 100644 (file)
@@ -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<Metric<Integer>> CAYC_REQUIREMENT_METRICS = List.of(NEW_MAINTAINABILITY_RATING, NEW_RELIABILITY_RATING, NEW_SECURITY_HOTSPOTS_REVIEWED, NEW_SECURITY_RATING);
+    List<Metric<? extends Serializable>> 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();
index 88cba16e754394be086e06bccf377ef67edf08e4..a7b880a9c82474c5bd04aefcf96524747b515d83 100644 (file)
@@ -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();
+  }
+
 }