]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7950 WS ce/component supports componentKey
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Thu, 15 Sep 2016 15:24:27 +0000 (17:24 +0200)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Fri, 16 Sep 2016 15:08:12 +0000 (17:08 +0200)
server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java
server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java

index 7b1d92eab46d7001777a7dd8da6809b67f411068..71223759e8bac2ebf92191654d15344693724144 100644 (file)
@@ -33,13 +33,16 @@ import org.sonar.db.ce.CeTaskQuery;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.user.UserSession;
+import org.sonar.server.ws.KeyExamples;
 import org.sonar.server.ws.WsUtils;
 
+import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_KEY;
 import static org.sonarqube.ws.WsCe.ProjectResponse;
 
 public class ComponentAction implements CeWsAction {
 
-  public static final String PARAM_COMPONENT_UUID = "componentId";
+  public static final String PARAM_COMPONENT_ID = "componentId";
+  public static final String PARAM_COMPONENT_KEY = "componentKey";
 
   private final UserSession userSession;
   private final DbClient dbClient;
@@ -56,23 +59,32 @@ public class ComponentAction implements CeWsAction {
   @Override
   public void define(WebService.NewController controller) {
     WebService.NewAction action = controller.createAction("component")
-      .setDescription("Get the pending tasks, in-progress tasks and the last executed task of a given component " +
-        "(usually a project). Requires the administration permission on the component.<br/>" +
+      .setDescription("Get the pending tasks, in-progress tasks and the last executed task of a given component (usually a project).<br>" +
+        "Requires one of the following permissions:" +
+        "<ul>" +
+        "<li>'Administer System'</li>" +
+        "<li>'Administer' rights on the specified component</li>" +
+        "</ul>" +
+        "Either '%s' or '%s' must be provided, not both.<br>" +
         "Since 6.1, field \"logs\" is deprecated and its value is always false.")
       .setSince("5.2")
       .setResponseExample(getClass().getResource("component-example.json"))
       .setHandler(this);
 
-    action.createParam(PARAM_COMPONENT_UUID)
-      .setRequired(true)
+    action.createParam(PARAM_COMPONENT_ID)
+      .setRequired(false)
       .setExampleValue(Uuids.UUID_EXAMPLE_01);
+
+    action.createParam(PARAM_COMPONENT_KEY)
+      .setRequired(false)
+      .setExampleValue(KeyExamples.KEY_PROJECT_EXAMPLE_001);
   }
 
   @Override
   public void handle(Request wsRequest, Response wsResponse) throws Exception {
     DbSession dbSession = dbClient.openSession(false);
     try {
-      ComponentDto component = componentFinder.getByUuid(dbSession, wsRequest.mandatoryParam(PARAM_COMPONENT_UUID));
+      ComponentDto component = componentFinder.getByUuidOrKey(dbSession, wsRequest.param(PARAM_COMPONENT_ID), wsRequest.param(PARAM_COMPONENT_KEY), COMPONENT_ID_AND_KEY);
       userSession.checkComponentUuidPermission(UserRole.USER, component.uuid());
       List<CeQueueDto> queueDtos = dbClient.ceQueueDao().selectByComponentUuid(dbSession, component.uuid());
       CeTaskQuery activityQuery = new CeTaskQuery()
index 23bcf43debd4c04948c635e13fe79b1f6ed640d5..cd7c6b81f75c0e0da55ca5c994da845478809ef9 100644 (file)
@@ -24,13 +24,16 @@ import org.junit.Test;
 import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
+import org.sonar.core.permission.GlobalPermissions;
 import org.sonar.core.util.Protobuf;
 import org.sonar.db.DbTester;
 import org.sonar.db.ce.CeActivityDto;
 import org.sonar.db.ce.CeQueueDto;
 import org.sonar.db.ce.CeTaskTypes;
 import org.sonar.db.component.ComponentDbTester;
+import org.sonar.db.component.ComponentDto;
 import org.sonar.server.component.ComponentFinder;
+import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.NotFoundException;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.TestResponse;
@@ -40,6 +43,8 @@ import org.sonarqube.ws.WsCe;
 
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonar.server.ce.ws.ComponentAction.PARAM_COMPONENT_ID;
+import static org.sonar.server.ce.ws.ComponentAction.PARAM_COMPONENT_KEY;
 
 public class ComponentActionTest {
 
@@ -55,14 +60,14 @@ public class ComponentActionTest {
   ComponentDbTester componentDbTester = new ComponentDbTester(dbTester);
   TaskFormatter formatter = new TaskFormatter(dbTester.getDbClient(), System2.INSTANCE);
   ComponentAction underTest = new ComponentAction(userSession, dbTester.getDbClient(), formatter, new ComponentFinder(dbTester.getDbClient()));
-  WsActionTester tester = new WsActionTester(underTest);
+  WsActionTester ws = new WsActionTester(underTest);
 
   @Test
   public void empty_queue_and_empty_activity() {
     componentDbTester.insertComponent(newProjectDto("PROJECT_1"));
     userSession.addComponentUuidPermission(UserRole.USER, "PROJECT_1", "PROJECT_1");
 
-    TestResponse wsResponse = tester.newRequest()
+    TestResponse wsResponse = ws.newRequest()
       .setParam("componentId", "PROJECT_1")
       .setMediaType(MediaTypes.PROTOBUF)
       .execute();
@@ -82,7 +87,7 @@ public class ComponentActionTest {
     insertQueue("T4", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS);
     insertQueue("T5", "PROJECT_1", CeQueueDto.Status.PENDING);
 
-    TestResponse wsResponse = tester.newRequest()
+    TestResponse wsResponse = ws.newRequest()
       .setParam("componentId", "PROJECT_1")
       .setMediaType(MediaTypes.PROTOBUF)
       .execute();
@@ -96,6 +101,21 @@ public class ComponentActionTest {
     assertThat(response.getCurrent().getId()).isEqualTo("T3");
   }
 
+  @Test
+  public void search_tasks_by_component_key() {
+    ComponentDto project = componentDbTester.insertProject();
+    setUserWithBrowsePermission(project);
+    insertActivity("T1", project.uuid(), CeActivityDto.Status.SUCCESS);
+
+    TestResponse wsResponse = ws.newRequest()
+      .setParam(PARAM_COMPONENT_KEY, project.key())
+      .setMediaType(MediaTypes.PROTOBUF)
+      .execute();
+
+    WsCe.ProjectResponse response = Protobuf.read(wsResponse.getInputStream(), WsCe.ProjectResponse.parser());
+    assertThat(response.hasCurrent()).isTrue();
+  }
+
   @Test
   public void canceled_tasks_must_not_be_picked_as_current_analysis() {
     componentDbTester.insertComponent(newProjectDto("PROJECT_1"));
@@ -106,7 +126,7 @@ public class ComponentActionTest {
     insertActivity("T4", "PROJECT_1", CeActivityDto.Status.CANCELED);
     insertActivity("T5", "PROJECT_1", CeActivityDto.Status.CANCELED);
 
-    TestResponse wsResponse = tester.newRequest()
+    TestResponse wsResponse = ws.newRequest()
       .setParam("componentId", "PROJECT_1")
       .setMediaType(MediaTypes.PROTOBUF)
       .execute();
@@ -123,12 +143,36 @@ public class ComponentActionTest {
     userSession.addComponentUuidPermission(UserRole.USER, "PROJECT_1", "PROJECT_1");
 
     expectedException.expect(NotFoundException.class);
-    tester.newRequest()
+    ws.newRequest()
       .setParam("componentId", "UNKNOWN")
       .setMediaType(MediaTypes.PROTOBUF)
       .execute();
   }
 
+  @Test
+  public void fail_when_insufficient_permissions() {
+    ComponentDto project = componentDbTester.insertProject();
+    userSession.setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
+
+    expectedException.expect(ForbiddenException.class);
+
+    ws.newRequest()
+      .setParam(PARAM_COMPONENT_ID, project.uuid())
+      .execute();
+  }
+
+  @Test
+  public void fail_when_no_component_parameter() {
+    expectedException.expect(IllegalArgumentException.class);
+    setUserWithBrowsePermission(componentDbTester.insertProject());
+
+    ws.newRequest().execute();
+  }
+
+  private void setUserWithBrowsePermission(ComponentDto project) {
+    userSession.addProjectUuidPermissions(UserRole.USER, project.uuid());
+  }
+
   private CeQueueDto insertQueue(String taskUuid, String componentUuid, CeQueueDto.Status status) {
     CeQueueDto queueDto = new CeQueueDto();
     queueDto.setTaskType(CeTaskTypes.REPORT);