From cdfc361c1253726fca1a1d9ac561a5c68126476a Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= Date: Mon, 28 Oct 2024 15:49:09 +0100 Subject: [PATCH] SONAR-23299 add flags in quality gate show endopint --- .../server/qualitygate/ws/ShowActionIT.java | 24 +++++++++++++++++-- .../qualitygate/QualityGateModeChecker.java | 4 ++-- .../server/qualitygate/ws/ShowAction.java | 15 +++++++++--- .../src/main/protobuf/ws-qualitygates.proto | 2 ++ 4 files changed, 38 insertions(+), 7 deletions(-) diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/ShowActionIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/ShowActionIT.java index 058964faa85..4393492c7fd 100644 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/ShowActionIT.java +++ b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/qualitygate/ws/ShowActionIT.java @@ -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(); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateModeChecker.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateModeChecker.java index 413aabad2db..bde5ef3dc7e 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateModeChecker.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/QualityGateModeChecker.java @@ -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 metrics) { + public QualityModeResult getUsageOfModeMetrics(Collection metrics) { Set metricKeys = metrics.stream().map(MetricDto::getKey).collect(Collectors.toSet()); boolean hasStandardConditions = metricKeys.stream().anyMatch(StandardToMQRMetrics::isStandardMetric); diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java index 849da21e939..915c6fde52f 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java @@ -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 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 conditions, Map metricsByUuid, QualityGateCaycStatus caycStatus) { + Collection conditions, Map 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()) diff --git a/sonar-ws/src/main/protobuf/ws-qualitygates.proto b/sonar-ws/src/main/protobuf/ws-qualitygates.proto index 8a6e1b43ce1..e7de5d25138 100644 --- a/sonar-ws/src/main/protobuf/ws-qualitygates.proto +++ b/sonar-ws/src/main/protobuf/ws-qualitygates.proto @@ -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; -- 2.39.5