]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-12275 Add 'component' param to ce/activity endpoint (#1904)
authorJacek <52388493+jacek-poreda-sonarsource@users.noreply.github.com>
Wed, 10 Jul 2019 12:49:01 +0000 (14:49 +0200)
committerSonarTech <sonartech@sonarsource.com>
Wed, 10 Jul 2019 15:22:54 +0000 (17:22 +0200)
* add 'component' param to ce/activity endpoint
* deprecate 'componentId' request param
* remove unnecessary method in IT

server/sonar-server/src/main/java/org/sonar/server/ce/ws/ActivityAction.java
server/sonar-server/src/test/java/org/sonar/server/ce/ws/ActivityActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/ce/ActivityRequest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java

index b7fc096d6b74765f40c497a9b92f7c6911815340..cefe7441f2a89a43665615a2c555fa7c08dccd94 100644 (file)
@@ -60,6 +60,7 @@ import static org.sonar.api.utils.DateUtils.parseEndingDateOrDateTime;
 import static org.sonar.api.utils.DateUtils.parseStartingDateOrDateTime;
 import static org.sonar.core.util.stream.MoreCollectors.toList;
 import static org.sonar.db.Pagination.forPage;
+import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT;
 import static org.sonar.server.ce.ws.CeWsParameters.PARAM_COMPONENT_ID;
 import static org.sonar.server.ce.ws.CeWsParameters.PARAM_MAX_EXECUTED_AT;
 import static org.sonar.server.ce.ws.CeWsParameters.PARAM_MIN_SUBMITTED_AT;
@@ -94,8 +95,10 @@ public class ActivityAction implements CeWsAction {
   public void define(WebService.NewController controller) {
     WebService.NewAction action = controller.createAction("activity")
       .setDescription(format("Search for tasks.<br> " +
+        "Either %s or %s can be provided, but not both.<br> " +
         "Requires the system administration permission, " +
-        "or project administration permission if %s is set.", PARAM_COMPONENT_ID))
+        "or project administration permission if %s or %s is set.",
+        PARAM_COMPONENT_ID, PARAM_COMPONENT, PARAM_COMPONENT_ID, PARAM_COMPONENT))
       .setResponseExample(getClass().getResource("activity-example.json"))
       .setHandler(this)
       .setChangelog(
@@ -108,7 +111,14 @@ public class ActivityAction implements CeWsAction {
 
     action.createParam(PARAM_COMPONENT_ID)
       .setDescription("Id of the component (project) to filter on")
+      .setDeprecatedSince("8.0")
       .setExampleValue(Uuids.UUID_EXAMPLE_03);
+
+    action.createParam(PARAM_COMPONENT)
+      .setDescription("Key of the component (project) to filter on")
+      .setExampleValue("projectKey")
+      .setSince("8.0");
+
     action.createParam(TEXT_QUERY)
       .setDescription(format("Limit search to: <ul>" +
         "<li>component names that contain the supplied string</li>" +
@@ -177,10 +187,19 @@ public class ActivityAction implements CeWsAction {
   @CheckForNull
   private ComponentDto loadComponent(DbSession dbSession, Request request) {
     String componentId = request.getComponentId();
-    if (componentId == null) {
-      return null;
+    String componentKey = request.getComponent();
+
+    Optional<ComponentDto> foundComponent;
+
+    if (componentId != null) {
+      foundComponent = dbClient.componentDao().selectByUuid(dbSession, componentId);
+      return checkFoundWithOptional(foundComponent, "Component '%s' does not exist", componentId);
+    } else if (componentKey != null) {
+      foundComponent = dbClient.componentDao().selectByKey(dbSession, componentKey);
+      return checkFoundWithOptional(foundComponent, "Component '%s' does not exist", componentKey);
     }
-    return checkFoundWithOptional(dbClient.componentDao().selectByUuid(dbSession, componentId), "Component '%s' does not exist", componentId);
+
+    return null;
   }
 
   private void checkPermission(@Nullable ComponentDto component) {
@@ -275,6 +294,7 @@ public class ActivityAction implements CeWsAction {
   private static Request toSearchWsRequest(org.sonar.api.server.ws.Request request) {
     Request activityWsRequest = new Request()
       .setComponentId(request.param(PARAM_COMPONENT_ID))
+      .setComponent(request.param(PARAM_COMPONENT))
       .setQ(request.param(TEXT_QUERY))
       .setStatus(request.paramAsStrings(PARAM_STATUS))
       .setType(request.param(PARAM_TYPE))
@@ -285,12 +305,20 @@ public class ActivityAction implements CeWsAction {
 
     checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getQ() == null, "%s and %s must not be set at the same time",
       PARAM_COMPONENT_ID, TEXT_QUERY);
+
+    checkRequest(activityWsRequest.getComponent() == null || activityWsRequest.getQ() == null, "%s and %s must not be set at the same time",
+      PARAM_COMPONENT, TEXT_QUERY);
+
+    checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getComponent() == null, "%s and %s must not be set at the same time",
+      PARAM_COMPONENT_ID, PARAM_COMPONENT);
+
     return activityWsRequest;
   }
 
   private static class Request {
 
     private String componentId;
+    private String component;
     private String maxExecutedAt;
     private String minSubmittedAt;
     private String onlyCurrents;
@@ -316,6 +344,19 @@ public class ActivityAction implements CeWsAction {
       return componentId;
     }
 
+    /**
+     * Example value: "sample:src/main/xoo/sample/Sample2.xoo"
+     */
+    private Request setComponent(@Nullable String component) {
+      this.component = component;
+      return this;
+    }
+
+    @CheckForNull
+    private String getComponent() {
+      return component;
+    }
+
     /**
      * Example value: "2017-10-19T13:00:00+0200"
      */
index 8df62cca4f7b3dd906e730769947fee3b54c9a86..417ff3ea812a21cf9b49e501f76806b550777cb6 100644 (file)
@@ -250,7 +250,7 @@ public class ActivityActionTest {
   }
 
   @Test
-  public void project_administrator_can_access_his_project_activity() {
+  public void project_administrator_can_access_his_project_activity_using_component_id() {
     ComponentDto project1 = db.components().insertPrivateProject();
     ComponentDto project2 = db.components().insertPrivateProject();
     // no need to be a system admin
@@ -266,6 +266,23 @@ public class ActivityActionTest {
     assertThat(activityResponse.getTasks(0).getComponentId()).isEqualTo(project1.uuid());
   }
 
+  @Test
+  public void project_administrator_can_access_his_project_activity_using_component_key() {
+    ComponentDto project1 = db.components().insertPrivateProject();
+    ComponentDto project2 = db.components().insertPrivateProject();
+    // no need to be a system admin
+    userSession.logIn().addProjectPermission(UserRole.ADMIN, project1);
+    insertActivity("T1", project1, SUCCESS);
+    insertActivity("T2", project2, FAILED);
+
+    ActivityResponse activityResponse = call(ws.newRequest().setParam("component", project1.getDbKey()));
+
+    assertThat(activityResponse.getTasksCount()).isEqualTo(1);
+    assertThat(activityResponse.getTasks(0).getId()).isEqualTo("T1");
+    assertThat(activityResponse.getTasks(0).getStatus()).isEqualTo(Ce.TaskStatus.SUCCESS);
+    assertThat(activityResponse.getTasks(0).getComponentId()).isEqualTo(project1.uuid());
+  }
+
   @Test
   public void return_401_if_user_is_not_logged_in() {
     ComponentDto project = db.components().insertPrivateProject();
@@ -456,6 +473,30 @@ public class ActivityActionTest {
         tuple("T2", branch, false, Ce.TaskStatus.PENDING));
   }
 
+  @Test
+  public void fail_if_both_component_id_and_component_key_provided() {
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("componentId and component must not be set at the same time");
+
+    ws.newRequest()
+      .setParam("componentId", "ID1")
+      .setParam("component", "apache")
+      .setMediaType(MediaTypes.PROTOBUF)
+      .execute();
+  }
+
+  @Test
+  public void fail_if_both_filters_on_component_key_and_name() {
+    expectedException.expect(BadRequestException.class);
+    expectedException.expectMessage("component and q must not be set at the same time");
+
+    ws.newRequest()
+      .setParam("q", "apache")
+      .setParam("component", "apache")
+      .setMediaType(MediaTypes.PROTOBUF)
+      .execute();
+  }
+
   @Test
   public void fail_if_both_filters_on_component_id_and_name() {
     expectedException.expect(BadRequestException.class);
index 9b4cafe70aeb785b322237553e7726c7fa76dd28..f0474853e882bbdb984fb04d68e36925c01326b1 100644 (file)
@@ -31,6 +31,7 @@ import javax.annotation.Generated;
 @Generated("sonar-ws-generator")
 public class ActivityRequest {
 
+  private String component;
   private String componentId;
   private String maxExecutedAt;
   private String minSubmittedAt;
@@ -52,6 +53,18 @@ public class ActivityRequest {
     return componentId;
   }
 
+  /**
+   * Example value: "sample:src/main/xoo/sample/Sample2.xoo"
+   */
+  public ActivityRequest setComponent(String component) {
+    this.component = component;
+    return this;
+  }
+
+  public String getComponent() {
+    return component;
+  }
+
   /**
    * Example value: "2017-10-19T13:00:00+0200"
    */
index 030f8d30c839f58d047b2193f515dcaecf2fed0b..abb2e2fcac96a5722df4b9a5a8abb1aa8ead51c2 100644 (file)
@@ -57,6 +57,7 @@ public class CeService extends BaseService {
     return call(
       new GetRequest(path("activity"))
         .setParam("componentId", request.getComponentId())
+        .setParam("component", request.getComponent())
         .setParam("maxExecutedAt", request.getMaxExecutedAt())
         .setParam("minSubmittedAt", request.getMinSubmittedAt())
         .setParam("onlyCurrents", request.getOnlyCurrents())
@@ -109,8 +110,7 @@ public class CeService extends BaseService {
     call(
       new PostRequest(path("cancel"))
         .setParam("id", request.getId())
-        .setMediaType(MediaTypes.JSON)
-      ).content();
+        .setMediaType(MediaTypes.JSON)).content();
   }
 
   /**
@@ -123,8 +123,7 @@ public class CeService extends BaseService {
   public void cancelAll() {
     call(
       new PostRequest(path("cancel_all"))
-        .setMediaType(MediaTypes.JSON)
-      ).content();
+        .setMediaType(MediaTypes.JSON)).content();
   }
 
   /**
@@ -165,8 +164,7 @@ public class CeService extends BaseService {
   public void pause() {
     call(
       new PostRequest(path("pause"))
-        .setMediaType(MediaTypes.JSON)
-      ).content();
+        .setMediaType(MediaTypes.JSON)).content();
   }
 
   /**
@@ -179,8 +177,7 @@ public class CeService extends BaseService {
   public void resume() {
     call(
       new PostRequest(path("resume"))
-        .setMediaType(MediaTypes.JSON)
-      ).content();
+        .setMediaType(MediaTypes.JSON)).content();
   }
 
   /**