]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10088 Return available actions in api/qualitygates/show
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 23 Nov 2017 10:46:30 +0000 (11:46 +0100)
committerEric Hartmann <hartmann.eric@gmail.Com>
Mon, 4 Dec 2017 12:44:55 +0000 (13:44 +0100)
server/sonar-server/src/main/java/org/sonar/server/qualitygate/ws/ShowAction.java
server/sonar-server/src/main/resources/org/sonar/server/qualitygate/ws/show-example.json
server/sonar-server/src/test/java/org/sonar/server/qualitygate/ws/ShowActionTest.java
sonar-ws/src/main/protobuf/ws-qualitygates.proto

index 53eb74529be3789821702d153a3b74a6f3e3b198..0f01909002b8a9ead37b0e011cf329390e03b6db 100644 (file)
@@ -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<QualityGateConditionDto> conditions = getConditions(dbSession, qualityGate);
       Map<Integer, MetricDto> 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<QualityGateConditionDto> conditions, Map<Integer, MetricDto> metricsById) {
+  private ShowWsResponse buildResponse(QualityGateDto qualityGate, @Nullable QualityGateDto defaultQualityGate, Collection<QualityGateConditionDto> conditions,
+    Map<Integer, MetricDto> 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();
   }
 
index f01a2079fe6262d0fe56789cbe27ec9f9fee19f2..6f0a06b2700858ad31ae349937cd44835dfc8f19 100644 (file)
       "op": "LT",
       "warning": "0"
     }
-  ]
+  ],
+  "actions": {
+    "edit": true,
+    "setAsDefault": true,
+    "copy": true,
+    "associateProjects": true
+  }
 }
index ddb3b8b57a2c10c50e5a6e4b7fd3c6377a3bbdfb..4036de490ae510b653b700fce8eea775323f4e58 100644 (file)
@@ -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);
index 15e2ab342e385b1732ccca1d0ca2850111268c5d..16b4d7e96717273f3ebb9b468fbc05038fa9b975 100644 (file)
@@ -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;