]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-23299 add flags in quality gate show endopint
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>
Mon, 28 Oct 2024 14:49:09 +0000 (15:49 +0100)
committersonartech <sonartech@sonarsource.com>
Tue, 5 Nov 2024 20:03:01 +0000 (20:03 +0000)
server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/ShowActionIT.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateModeChecker.java
server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java
sonar-ws/src/main/protobuf/ws-qualitygates.proto

index 058964faa851355bb2505b45b49439bd0b4c0c37..4393492c7fd6966b81fa459f58d42394cb87c95c 100644 (file)
@@ -19,6 +19,8 @@
  */
 package org.sonar.server.qualitygate.ws;
 
+import java.util.Collection;
+import org.assertj.core.api.Assertions;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -34,6 +36,7 @@ import org.sonar.server.component.TestComponentFinder;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.qualitygate.QualityGateCaycChecker;
 import org.sonar.server.qualitygate.QualityGateFinder;
+import org.sonar.server.qualitygate.QualityGateModeChecker;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
 import org.sonarqube.ws.Qualitygates.ShowWsResponse;
@@ -64,14 +67,16 @@ public class ShowActionIT {
   @Rule
   public DbTester db = DbTester.create();
   private final QualityGateCaycChecker qualityGateCaycChecker = mock(QualityGateCaycChecker.class);
-
+  private final QualityGateModeChecker qualityGateModeChecker = mock(QualityGateModeChecker.class);
   private final WsActionTester ws = new WsActionTester(
     new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()),
-      new QualityGatesWsSupport(db.getDbClient(), userSession, TestComponentFinder.from(db)), qualityGateCaycChecker));
+      new QualityGatesWsSupport(db.getDbClient(), userSession, TestComponentFinder.from(db)), qualityGateCaycChecker, qualityGateModeChecker));
 
   @Before
   public void setUp() {
     when(qualityGateCaycChecker.checkCaycCompliant(any(), any(String.class))).thenReturn(COMPLIANT);
+    when(qualityGateModeChecker.getUsageOfModeMetrics(any()))
+      .thenReturn(new QualityGateModeChecker.QualityModeResult(false, false));
   }
 
   @Test
@@ -122,6 +127,21 @@ public class ShowActionIT {
     assertEquals(COMPLIANT.toString(), response.getCaycStatus());
   }
 
+  @Test
+  public void execute_shouldShowModeFlags() {
+    QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
+    when(qualityGateModeChecker.getUsageOfModeMetrics(any(Collection.class)))
+      .thenReturn(new QualityGateModeChecker.QualityModeResult(true, true));
+    db.qualityGates().setDefaultQualityGate(qualityGate);
+
+    ShowWsResponse response = ws.newRequest()
+      .setParam("name", qualityGate.getName())
+      .executeProtobuf(ShowWsResponse.class);
+
+    Assertions.assertThat(response.getHasStandardConditions()).isTrue();
+    Assertions.assertThat(response.getHasMQRConditions()).isTrue();
+  }
+
   @Test
   public void show_by_name() {
     QualityGateDto qualityGate = db.qualityGates().insertQualityGate();
index 413aabad2db6bee6fa478663d1f044401dacc4f2..bde5ef3dc7e5e5c7aeb25f693be6e4f70765a03a 100644 (file)
@@ -19,7 +19,7 @@
  */
 package org.sonar.server.qualitygate;
 
-import java.util.List;
+import java.util.Collection;
 import java.util.Set;
 import java.util.stream.Collectors;
 import org.sonar.db.metric.MetricDto;
@@ -27,7 +27,7 @@ import org.sonar.server.metric.StandardToMQRMetrics;
 
 public class QualityGateModeChecker {
 
-  public QualityModeResult getUsageOfModeMetrics(List<MetricDto> metrics) {
+  public QualityModeResult getUsageOfModeMetrics(Collection<MetricDto> metrics) {
     Set<String> metricKeys = metrics.stream().map(MetricDto::getKey).collect(Collectors.toSet());
 
     boolean hasStandardConditions = metricKeys.stream().anyMatch(StandardToMQRMetrics::isStandardMetric);
index 849da21e93928ef42f5228da3f74ab1878dd9cd2..915c6fde52fb196fbc40d8e033096aa566fbb0d7 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.db.qualitygate.QualityGateDto;
 import org.sonar.server.qualitygate.QualityGateCaycChecker;
 import org.sonar.server.qualitygate.QualityGateCaycStatus;
 import org.sonar.server.qualitygate.QualityGateFinder;
+import org.sonar.server.qualitygate.QualityGateModeChecker;
 import org.sonarqube.ws.Qualitygates.ShowWsResponse;
 
 import static com.google.common.base.Preconditions.checkState;
@@ -50,12 +51,15 @@ public class ShowAction implements QualityGatesWsAction {
   private final QualityGateFinder qualityGateFinder;
   private final QualityGatesWsSupport wsSupport;
   private final QualityGateCaycChecker qualityGateCaycChecker;
+  private final QualityGateModeChecker qualityGateModeChecker;
 
-  public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport, QualityGateCaycChecker qualityGateCaycChecker) {
+  public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QualityGatesWsSupport wsSupport, QualityGateCaycChecker qualityGateCaycChecker,
+    QualityGateModeChecker qualityGateModeChecker) {
     this.dbClient = dbClient;
     this.qualityGateFinder = qualityGateFinder;
     this.wsSupport = wsSupport;
     this.qualityGateCaycChecker = qualityGateCaycChecker;
+    this.qualityGateModeChecker = qualityGateModeChecker;
   }
 
   @Override
@@ -66,6 +70,7 @@ public class ShowAction implements QualityGatesWsAction {
       .setSince("4.3")
       .setResponseExample(Resources.getResource(this.getClass(), "show-example.json"))
       .setChangelog(
+        new Change("10.8", "'hasMQRConditions' and 'hasStandardConditions' fields are added on quality gate"),
         new Change("10.3", "'isDefault' field is added to the response"),
         new Change("10.0", "Field 'id' in the response has been removed"),
         new Change("10.0", "Parameter 'id' is removed. Use 'name' instead."),
@@ -93,7 +98,8 @@ public class ShowAction implements QualityGatesWsAction {
       Map<String, MetricDto> metricsByUuid = getMetricsByUuid(dbSession, conditions);
       QualityGateDto defaultQualityGate = qualityGateFinder.getDefault(dbSession);
       QualityGateCaycStatus caycStatus = qualityGateCaycChecker.checkCaycCompliant(dbSession, qualityGate.getUuid());
-      writeProtobuf(buildResponse(dbSession, qualityGate, defaultQualityGate, conditions, metricsByUuid, caycStatus), request, response);
+      QualityGateModeChecker.QualityModeResult qualityModeResult = qualityGateModeChecker.getUsageOfModeMetrics(metricsByUuid.values());
+      writeProtobuf(buildResponse(dbSession, qualityGate, defaultQualityGate, conditions, metricsByUuid, caycStatus, qualityModeResult), request, response);
     }
   }
 
@@ -107,12 +113,15 @@ public class ShowAction implements QualityGatesWsAction {
   }
 
   private ShowWsResponse buildResponse(DbSession dbSession, QualityGateDto qualityGate, QualityGateDto defaultQualityGate,
-    Collection<QualityGateConditionDto> conditions, Map<String, MetricDto> metricsByUuid, QualityGateCaycStatus caycStatus) {
+    Collection<QualityGateConditionDto> conditions, Map<String, MetricDto> metricsByUuid, QualityGateCaycStatus caycStatus,
+    QualityGateModeChecker.QualityModeResult qualityModeResult) {
     return ShowWsResponse.newBuilder()
       .setName(qualityGate.getName())
       .setIsBuiltIn(qualityGate.isBuiltIn())
       .setIsDefault(qualityGate.getUuid().equals(defaultQualityGate.getUuid()))
       .setCaycStatus(caycStatus.toString())
+      .setHasMQRConditions(qualityModeResult.hasMQRConditions())
+      .setHasStandardConditions(qualityModeResult.hasStandardConditions())
       .addAllConditions(conditions.stream()
         .map(toWsCondition(metricsByUuid))
         .toList())
index 8a6e1b43ce1a664955bc171687bdc2f84ab11c63..e7de5d2513811cc050645a01f6ec41344ed16fc0 100644 (file)
@@ -126,6 +126,8 @@ message ShowWsResponse {
   optional Actions actions = 5;
   required string caycStatus = 6;
   required bool isDefault = 7;
+  optional bool hasStandardConditions = 8;
+  optional bool hasMQRConditions = 9;
 
   message Condition {
     required string id = 1;