From 40c0ef789063c78a5fef3b0c5520326fcf39ce03 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 22 Nov 2017 15:05:14 +0100 Subject: [PATCH] SONAR-10087 Add built-in field in api/qualitygates/show --- .../sonar/db/qualitygate/QualityGateDbTester.java | 13 +++++++++---- .../sonar/server/qualitygate/ws/ShowAction.java | 3 +++ .../sonar/server/qualitygate/ws/show-example.json | 1 + .../server/qualitygate/ws/ShowActionTest.java | 15 ++++++++++++++- sonar-ws/src/main/protobuf/ws-qualitygates.proto | 1 + 5 files changed, 28 insertions(+), 5 deletions(-) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java index 38b3ab546de..84e53e052fd 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualitygate/QualityGateDbTester.java @@ -43,12 +43,17 @@ public class QualityGateDbTester { this.dbSession = db.getSession(); } - public QualityGateDto insertQualityGate() { - return insertQualityGate(randomAlphanumeric(30)); + public QualityGateDto insertQualityGate(String name) { + return insertQualityGate(qualityGate -> qualityGate.setName(name)); } - public QualityGateDto insertQualityGate(String name) { - QualityGateDto updatedUser = dbClient.qualityGateDao().insert(dbSession, new QualityGateDto().setName(name).setBuiltIn(false)); + @SafeVarargs + public final QualityGateDto insertQualityGate(Consumer... dtoPopulators) { + QualityGateDto qualityGate = new QualityGateDto() + .setName(randomAlphanumeric(30)) + .setBuiltIn(false); + Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(qualityGate)); + QualityGateDto updatedUser = dbClient.qualityGateDao().insert(dbSession, qualityGate); db.commit(); return updatedUser; } diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java index d9d9d1efcc1..53eb74529be 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java @@ -25,6 +25,7 @@ import java.util.Map; import java.util.Set; import java.util.function.Function; import javax.annotation.Nullable; +import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.Request; import org.sonar.api.server.ws.Response; import org.sonar.api.server.ws.WebService; @@ -62,6 +63,7 @@ public class ShowAction implements QualityGatesWsAction { .setDescription("Display the details of a quality gate") .setSince("4.3") .setResponseExample(Resources.getResource(this.getClass(), "show-example.json")) + .setChangelog(new Change("7.0", "'isBuiltIn' field is added to the response")) .setHandler(this); action.createParam(PARAM_ID) @@ -102,6 +104,7 @@ public class ShowAction implements QualityGatesWsAction { return ShowWsResponse.newBuilder() .setId(qualityGate.getId()) .setName(qualityGate.getName()) + .setIsBuiltIn(qualityGate.isBuiltIn()) .addAllConditions(conditions.stream() .map(toWsCondition(metricsById)) .collect(toList())) diff --git a/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/show-example.json b/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/show-example.json index 7bca1004eb3..f01a2079fe6 100644 --- a/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/show-example.json +++ b/server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/show-example.json @@ -1,6 +1,7 @@ { "id": 2, "name": "My Quality Gate", + "isBuiltIn": false, "conditions": [ { "id": 9, diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java index 3be7de287f3..ddb3b8b57a2 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java @@ -22,6 +22,7 @@ package org.sonar.server.qualitygate.ws; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; +import org.sonar.api.server.ws.Change; import org.sonar.api.server.ws.WebService; import org.sonar.api.server.ws.WebService.Param; import org.sonar.api.utils.System2; @@ -55,7 +56,8 @@ public class ShowActionTest { public void verify_definition() { WebService.Action action = ws.getDef(); assertThat(action.since()).isEqualTo("4.3"); - assertThat(action.changelog()).isEmpty(); + assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription) + .containsExactlyInAnyOrder(tuple("7.0", "'isBuiltIn' field is added to the response")); assertThat(action.params()) .extracting(Param::key, Param::isRequired) .containsExactlyInAnyOrder(tuple("id", false), tuple("name", false)); @@ -90,6 +92,7 @@ public class ShowActionTest { assertThat(response.getId()).isEqualTo(qualityGate.getId()); assertThat(response.getName()).isEqualTo(qualityGate.getName()); + assertThat(response.getIsBuiltIn()).isFalse(); assertThat(response.getConditionsList()).hasSize(2); assertThat(response.getConditionsList()) .extracting(Condition::getId, Condition::getMetric, Condition::hasPeriod, Condition::getPeriod, Condition::getOp, Condition::getError, Condition::getWarning) @@ -98,6 +101,16 @@ public class ShowActionTest { tuple(condition2.getId(), metric.getKey(), true, 1, "LT", condition2.getErrorThreshold(), condition2.getWarningThreshold())); } + @Test + public void show_built_in() { + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true)); + + ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName()) + .executeProtobuf(ShowWsResponse.class); + + assertThat(response.getIsBuiltIn()).isTrue(); + } + @Test public void show_by_id() { QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); diff --git a/sonar-ws/src/main/protobuf/ws-qualitygates.proto b/sonar-ws/src/main/protobuf/ws-qualitygates.proto index ef51ba14a39..36998c537ff 100644 --- a/sonar-ws/src/main/protobuf/ws-qualitygates.proto +++ b/sonar-ws/src/main/protobuf/ws-qualitygates.proto @@ -123,6 +123,7 @@ message ShowWsResponse { optional int64 id = 1; optional string name = 2; repeated Condition conditions = 3; + optional bool isBuiltIn = 4; message Condition { optional int64 id = 1; -- 2.39.5