From 26a16f4d23254915a5c6edb96bfd85a9f011c68d Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 23 Nov 2017 11:46:30 +0100 Subject: [PATCH] SONAR-10088 Return available actions in api/qualitygates/show --- .../server/qualitygate/ws/ShowAction.java | 15 +++- .../server/qualitygate/ws/show-example.json | 8 +- .../server/qualitygate/ws/ShowActionTest.java | 76 ++++++++++++++++++- .../src/main/protobuf/ws-qualitygates.proto | 1 + 4 files changed, 93 insertions(+), 7 deletions(-) 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 53eb74529be..0f01909002b 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 @@ -51,10 +51,12 @@ public class ShowAction implements QualityGatesWsAction { private final DbClient dbClient; private final QualityGateFinder qualityGateFinder; + private final QGateWsSupport wsSupport; - public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder) { + public ShowAction(DbClient dbClient, QualityGateFinder qualityGateFinder, QGateWsSupport wsSupport) { this.dbClient = dbClient; this.qualityGateFinder = qualityGateFinder; + this.wsSupport = wsSupport; } @Override @@ -63,7 +65,9 @@ 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")) + .setChangelog( + new Change("7.0", "'isBuiltIn' field is added to the response"), + new Change("7.0", "'actions' field is added in the response")) .setHandler(this); action.createParam(PARAM_ID) @@ -85,7 +89,8 @@ public class ShowAction implements QualityGatesWsAction { QualityGateDto qualityGate = qualityGateFinder.getByNameOrId(dbSession, name, id); Collection conditions = getConditions(dbSession, qualityGate); Map metricsById = getMetricsById(dbSession, conditions); - writeProtobuf(buildResponse(qualityGate, conditions, metricsById), request, response); + QualityGateDto defaultQualityGate = wsSupport.getDefault(dbSession); + writeProtobuf(buildResponse(qualityGate, defaultQualityGate, conditions, metricsById), request, response); } } @@ -100,7 +105,8 @@ public class ShowAction implements QualityGatesWsAction { .collect(uniqueIndex(MetricDto::getId)); } - private static ShowWsResponse buildResponse(QualityGateDto qualityGate, Collection conditions, Map metricsById) { + private ShowWsResponse buildResponse(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate, Collection conditions, + Map metricsById) { return ShowWsResponse.newBuilder() .setId(qualityGate.getId()) .setName(qualityGate.getName()) @@ -108,6 +114,7 @@ public class ShowAction implements QualityGatesWsAction { .addAllConditions(conditions.stream() .map(toWsCondition(metricsById)) .collect(toList())) + .setActions(wsSupport.getActions(qualityGate, defaultQualityGate)) .build(); } 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 f01a2079fe6..6f0a06b2700 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 @@ -16,5 +16,11 @@ "op": "LT", "warning": "0" } - ] + ], + "actions": { + "edit": true, + "setAsDefault": true, + "copy": true, + "associateProjects": true + } } 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 ddb3b8b57a2..4036de490ae 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 @@ -30,6 +30,8 @@ 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.organization.DefaultOrganizationProvider; +import org.sonar.server.organization.TestDefaultOrganizationProvider; import org.sonar.server.qualitygate.QualityGateFinder; import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; @@ -39,7 +41,10 @@ import org.sonarqube.ws.Qualitygates.ShowWsResponse.Condition; import static java.lang.String.format; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.AssertionsForClassTypes.tuple; +import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_GATES; +import static org.sonar.db.permission.OrganizationPermission.ADMINISTER_QUALITY_PROFILES; import static org.sonar.test.JsonAssert.assertJson; +import static org.sonarqube.ws.Qualitygates.Actions; public class ShowActionTest { @@ -50,14 +55,19 @@ public class ShowActionTest { @Rule public DbTester db = DbTester.create(System2.INSTANCE); - private WsActionTester ws = new WsActionTester(new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()))); + private DefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(db); + + private WsActionTester ws = new WsActionTester( + new ShowAction(db.getDbClient(), new QualityGateFinder(db.getDbClient()), new QGateWsSupport(db.getDbClient(), userSession, defaultOrganizationProvider))); @Test public void verify_definition() { WebService.Action action = ws.getDef(); assertThat(action.since()).isEqualTo("4.3"); assertThat(action.changelog()).extracting(Change::getVersion, Change::getDescription) - .containsExactlyInAnyOrder(tuple("7.0", "'isBuiltIn' field is added to the response")); + .containsExactlyInAnyOrder( + tuple("7.0", "'isBuiltIn' field is added to the response"), + tuple("7.0", "'actions' field is added in the response")); assertThat(action.params()) .extracting(Param::key, Param::isRequired) .containsExactlyInAnyOrder(tuple("id", false), tuple("name", false)); @@ -65,6 +75,7 @@ public class ShowActionTest { @Test public void json_example() { + userSession.logIn("admin").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); QualityGateDto qualityGate = db.qualityGates().insertQualityGate("My Quality Gate"); MetricDto blockerViolationsMetric = db.measures().insertMetric(m -> m.setKey("blocker_violations")); MetricDto criticalViolationsMetric = db.measures().insertMetric(m -> m.setKey("critical_violations")); @@ -134,6 +145,67 @@ public class ShowActionTest { assertThat(response.getConditionsList()).isEmpty(); } + @Test + public void actions() { + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + + ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName()) + .executeProtobuf(ShowWsResponse.class); + + Actions actions = response.getActions(); + assertThat(actions.getEdit()).isTrue(); + assertThat(actions.getCopy()).isTrue(); + assertThat(actions.getSetAsDefault()).isTrue(); + assertThat(actions.getAssociateProjects()).isTrue(); + } + + @Test + public void actions_on_default() { + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(); + db.qualityGates().setDefaultQualityGate(qualityGate); + + ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName()) + .executeProtobuf(ShowWsResponse.class); + + Actions actions = response.getActions(); + assertThat(actions.getEdit()).isTrue(); + assertThat(actions.getCopy()).isTrue(); + assertThat(actions.getSetAsDefault()).isFalse(); + assertThat(actions.getAssociateProjects()).isFalse(); + } + + @Test + public void actions_on_built_in() { + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_GATES, defaultOrganizationProvider.get().getUuid()); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true)); + + ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName()) + .executeProtobuf(ShowWsResponse.class); + + Actions actions = response.getActions(); + assertThat(actions.getEdit()).isFalse(); + assertThat(actions.getCopy()).isTrue(); + assertThat(actions.getSetAsDefault()).isTrue(); + assertThat(actions.getAssociateProjects()).isTrue(); + } + + @Test + public void actions_when_not_quality_gate_administer() { + userSession.logIn("john").addPermission(ADMINISTER_QUALITY_PROFILES, defaultOrganizationProvider.get().getUuid()); + QualityGateDto qualityGate = db.qualityGates().insertQualityGate(qg -> qg.setBuiltIn(true)); + + ShowWsResponse response = ws.newRequest().setParam("name", qualityGate.getName()) + .executeProtobuf(ShowWsResponse.class); + + Actions actions = response.getActions(); + assertThat(actions.getEdit()).isFalse(); + assertThat(actions.getCopy()).isFalse(); + assertThat(actions.getSetAsDefault()).isFalse(); + assertThat(actions.getAssociateProjects()).isFalse(); + } + @Test public void fail_when_no_name_or_id() { expectedException.expect(IllegalArgumentException.class); diff --git a/sonar-ws/src/main/protobuf/ws-qualitygates.proto b/sonar-ws/src/main/protobuf/ws-qualitygates.proto index 15e2ab342e3..16b4d7e9671 100644 --- a/sonar-ws/src/main/protobuf/ws-qualitygates.proto +++ b/sonar-ws/src/main/protobuf/ws-qualitygates.proto @@ -124,6 +124,7 @@ message ShowWsResponse { optional string name = 2; repeated Condition conditions = 3; optional bool isBuiltIn = 4; + optional Actions actions = 5; message Condition { optional int64 id = 1; -- 2.39.5