]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7138 WS api/ce/activity filter background tasks by task uuid
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 17 Feb 2016 12:53:47 +0000 (13:53 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Wed, 17 Feb 2016 14:10:35 +0000 (15:10 +0100)
server/sonar-server/src/main/java/org/sonar/server/computation/ws/ActivityAction.java
server/sonar-server/src/test/java/org/sonar/server/computation/ws/ActivityActionTest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/ce/ActivityWsRequest.java
sonar-ws/src/main/java/org/sonarqube/ws/client/ce/CeService.java
sonar-ws/src/test/java/org/sonarqube/ws/client/ce/CeServiceTest.java

index a75efc58f58fa4eb31d518ba1249d177ff6921bf..19c1aee5b18487730c2f71e3a1768af6c44a4f28 100644 (file)
 package org.sonar.server.computation.ws;
 
 import com.google.common.base.Joiner;
+import com.google.common.base.Optional;
 import com.google.common.collect.ImmutableList;
 import com.google.common.collect.Lists;
+import java.util.Collections;
 import java.util.Date;
 import java.util.LinkedHashSet;
 import java.util.List;
@@ -57,6 +59,8 @@ import org.sonarqube.ws.WsCe.ActivityResponse;
 import org.sonarqube.ws.client.ce.ActivityWsRequest;
 
 import static java.lang.String.format;
+import static java.util.Collections.singletonList;
+import static org.apache.commons.lang.StringUtils.defaultString;
 import static org.sonar.api.utils.DateUtils.parseDateQuietly;
 import static org.sonar.api.utils.DateUtils.parseDateTimeQuietly;
 import static org.sonar.api.utils.Paging.offset;
@@ -107,8 +111,19 @@ public class ActivityAction implements CeWsAction {
         "<li>component names that contain the supplied string</li>" +
         "<li>component keys that are exactly the same as the supplied string</li>" +
         "</ul>" +
+        "Must not be set together with %s.<br />" +
+        "Deprecated and replaced by '%s'", PARAM_COMPONENT_ID, Param.TEXT_QUERY))
+      .setExampleValue("Apache")
+      .setDeprecatedSince("5.5");
+    action.createParam(Param.TEXT_QUERY)
+      .setDescription(format("Limit search to: <ul>" +
+        "<li>component names that contain the supplied string</li>" +
+        "<li>component keys that are exactly the same as the supplied string</li>" +
+        "<li>task ids that are exactly the same as the supplied string</li>" +
+        "</ul>" +
         "Must not be set together with %s", PARAM_COMPONENT_ID))
-      .setExampleValue("Apache");
+      .setExampleValue("Apache")
+      .setSince("5.5");
     action.createParam(PARAM_STATUS)
       .setDescription("Comma separated list of task statuses")
       .setPossibleValues(ImmutableList.builder()
@@ -144,6 +159,15 @@ public class ActivityAction implements CeWsAction {
   private ActivityResponse doHandle(ActivityWsRequest request) {
     DbSession dbSession = dbClient.openSession(false);
     try {
+      // if a task searched by uuid is found all other parameters are ignored
+      Optional<WsCe.Task> taskSearchedById = searchTaskByUuid(dbSession, request);
+      if (taskSearchedById.isPresent()) {
+        return buildResponse(
+          singletonList(taskSearchedById.get()),
+          Collections.<WsCe.Task>emptyList(),
+          Paging.forPageIndex(1).withPageSize(request.getPageSize()).andTotal(1));
+      }
+
       CeTaskQuery query = buildQuery(dbSession, request);
       checkPermissions(query);
       TaskResult queuedTasks = loadQueuedTasks(dbSession, request, query);
@@ -161,6 +185,25 @@ public class ActivityAction implements CeWsAction {
     }
   }
 
+  private Optional<WsCe.Task> searchTaskByUuid(DbSession dbSession, ActivityWsRequest request) {
+    String textQuery = request.getQuery();
+    if (textQuery == null) {
+      return Optional.absent();
+    }
+
+    Optional<CeQueueDto> queue = dbClient.ceQueueDao().selectByUuid(dbSession, textQuery);
+    if (queue.isPresent()) {
+      return Optional.of(formatter.formatQueue(dbSession, queue.get()));
+    }
+
+    Optional<CeActivityDto> activity = dbClient.ceActivityDao().selectByUuid(dbSession, textQuery);
+    if (activity.isPresent()) {
+      return Optional.of(formatter.formatActivity(dbSession, activity.get()));
+    }
+
+    return Optional.absent();
+  }
+
   private CeTaskQuery buildQuery(DbSession dbSession, ActivityWsRequest request) {
     CeTaskQuery query = new CeTaskQuery();
     query.setType(request.getType());
@@ -179,7 +222,7 @@ public class ActivityAction implements CeWsAction {
 
   private void loadComponentUuids(DbSession dbSession, ActivityWsRequest request, CeTaskQuery query) {
     String componentUuid = request.getComponentId();
-    String componentQuery = request.getComponentQuery();
+    String componentQuery = request.getQuery();
 
     if (componentUuid != null) {
       query.setComponentUuid(componentUuid);
@@ -271,7 +314,7 @@ public class ActivityAction implements CeWsAction {
   private static ActivityWsRequest toSearchWsRequest(Request request) {
     ActivityWsRequest activityWsRequest = new ActivityWsRequest()
       .setComponentId(request.param(PARAM_COMPONENT_ID))
-      .setComponentQuery(request.param(PARAM_COMPONENT_QUERY))
+      .setQuery(defaultString(request.param(Param.TEXT_QUERY), request.param(PARAM_COMPONENT_QUERY)))
       .setStatus(request.paramAsStrings(PARAM_STATUS))
       .setType(request.param(PARAM_TYPE))
       .setMinSubmittedAt(request.param(PARAM_MIN_SUBMITTED_AT))
@@ -280,7 +323,7 @@ public class ActivityAction implements CeWsAction {
       .setPage(request.mandatoryParamAsInt(Param.PAGE))
       .setPageSize(request.mandatoryParamAsInt(Param.PAGE_SIZE));
 
-    checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getComponentQuery() == null, "%s and %s must not be set at the same time",
+    checkRequest(activityWsRequest.getComponentId() == null || activityWsRequest.getQuery() == null, "%s and %s must not be set at the same time",
       PARAM_COMPONENT_ID, PARAM_COMPONENT_QUERY);
     checkRequest(activityWsRequest.getPageSize() <= MAX_PAGE_SIZE, "The '%s' parameter must be less than %d", Param.PAGE_SIZE, MAX_PAGE_SIZE);
 
index d6b0b310c654db1e2f75bef706f41bdf7003869b..f51761372e526a03d92696d654aa9baaf0e98979 100644 (file)
@@ -21,6 +21,7 @@
 package org.sonar.server.computation.ws;
 
 import com.google.common.base.Optional;
+import com.google.common.base.Throwables;
 import java.io.File;
 import java.io.IOException;
 import java.util.Collections;
@@ -33,7 +34,6 @@ import org.sonar.api.server.ws.WebService.Param;
 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;
@@ -44,11 +44,13 @@ import org.sonar.server.computation.log.LogFileRef;
 import org.sonar.server.computation.taskprocessor.CeTaskProcessor;
 import org.sonar.server.exceptions.BadRequestException;
 import org.sonar.server.tester.UserSessionRule;
+import org.sonar.server.ws.TestRequest;
 import org.sonar.server.ws.TestResponse;
 import org.sonar.server.ws.WsActionTester;
 import org.sonar.test.JsonAssert;
 import org.sonarqube.ws.MediaTypes;
 import org.sonarqube.ws.WsCe;
+import org.sonarqube.ws.WsCe.ActivityResponse;
 import org.sonarqube.ws.client.ce.CeWsParameters;
 
 import static java.util.Arrays.asList;
@@ -58,6 +60,7 @@ import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.sonar.api.utils.DateUtils.formatDateTime;
 import static org.sonar.db.component.ComponentTesting.newProjectDto;
+import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_STATUS;
 
 public class ActivityActionTest {
@@ -88,15 +91,10 @@ public class ActivityActionTest {
     insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
     insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
 
-    TestResponse wsResponse = ws.newRequest()
-      .setParam(CeWsParameters.PARAM_MAX_EXECUTED_AT, formatDateTime(EXECUTED_AT + 2_000))
-      .setMediaType(MediaTypes.PROTOBUF)
-      .execute();
+    ActivityResponse activityResponse = call(ws.newRequest()
+      .setParam(CeWsParameters.PARAM_MAX_EXECUTED_AT, formatDateTime(EXECUTED_AT + 2_000)));
 
-    // verify the protobuf response
-    WsCe.ActivityResponse activityResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.ActivityResponse.parser());
     assertThat(activityResponse.getTasksCount()).isEqualTo(2);
-
     // chronological order, from newest to oldest
     assertThat(activityResponse.getTasks(0).getId()).isEqualTo("T2");
     assertThat(activityResponse.getTasks(0).getStatus()).isEqualTo(WsCe.TaskStatus.FAILED);
@@ -117,12 +115,9 @@ public class ActivityActionTest {
     insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
     insertQueue("T3", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS);
 
-    TestResponse wsResponse = ws.newRequest()
-      .setParam("status", "FAILED,IN_PROGRESS")
-      .setMediaType(MediaTypes.PROTOBUF)
-      .execute();
+    ActivityResponse activityResponse = call(ws.newRequest()
+      .setParam("status", "FAILED,IN_PROGRESS"));
 
-    WsCe.ActivityResponse activityResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.ActivityResponse.parser());
     assertThat(activityResponse.getTasksCount()).isEqualTo(2);
     assertThat(activityResponse.getTasks(0).getId()).isEqualTo("T3");
     assertThat(activityResponse.getTasks(1).getId()).isEqualTo("T2");
@@ -135,13 +130,10 @@ public class ActivityActionTest {
     insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
     insertQueue("T3", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS);
 
-    TestResponse wsResponse = ws.newRequest()
+    ActivityResponse activityResponse = call(ws.newRequest()
       .setParam("status", "FAILED,IN_PROGRESS,SUCCESS")
-      .setParam(CeWsParameters.PARAM_MAX_EXECUTED_AT, "2016-02-15")
-      .setMediaType(MediaTypes.PROTOBUF)
-      .execute();
+      .setParam(CeWsParameters.PARAM_MAX_EXECUTED_AT, "2016-02-15"));
 
-    WsCe.ActivityResponse activityResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.ActivityResponse.parser());
     assertThat(activityResponse.getTasksCount()).isEqualTo(0);
   }
 
@@ -153,12 +145,10 @@ public class ActivityActionTest {
     insertActivity("T2", "PROJECT_1", CeActivityDto.Status.FAILED);
     insertQueue("T3", "PROJECT_1", CeQueueDto.Status.PENDING);
 
-    TestResponse wsResponse = ws.newRequest()
-      .setParam("onlyCurrents", "true")
-      .setMediaType(MediaTypes.PROTOBUF)
-      .execute();
+    ActivityResponse activityResponse = call(
+      ws.newRequest()
+        .setParam("onlyCurrents", "true"));
 
-    WsCe.ActivityResponse activityResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.ActivityResponse.parser());
     assertThat(activityResponse.getTasksCount()).isEqualTo(1);
     assertThat(activityResponse.getTasks(0).getId()).isEqualTo("T2");
   }
@@ -177,14 +167,11 @@ public class ActivityActionTest {
   }
 
   private void assertPage(int pageIndex, int pageSize, int expectedTotal, List<String> expectedOrderedTaskIds) {
-    TestResponse wsResponse = ws.newRequest()
-      .setMediaType(MediaTypes.PROTOBUF)
+    ActivityResponse activityResponse = call(ws.newRequest()
       .setParam(Param.PAGE, Integer.toString(pageIndex))
       .setParam(Param.PAGE_SIZE, Integer.toString(pageSize))
-      .setParam(PARAM_STATUS, "SUCCESS,FAILED,CANCELED,IN_PROGRESS,PENDING")
-      .execute();
+      .setParam(PARAM_STATUS, "SUCCESS,FAILED,CANCELED,IN_PROGRESS,PENDING"));
 
-    WsCe.ActivityResponse activityResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.ActivityResponse.parser());
     assertThat(activityResponse.getPaging().getPageIndex()).isEqualTo(pageIndex);
     assertThat(activityResponse.getPaging().getPageSize()).isEqualTo(pageSize);
     assertThat(activityResponse.getPaging().getTotal()).isEqualTo(expectedTotal);
@@ -203,12 +190,8 @@ public class ActivityActionTest {
     insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
     insertActivity("T2", "PROJECT_2", CeActivityDto.Status.FAILED);
 
-    TestResponse wsResponse = ws.newRequest()
-      .setParam("componentId", "PROJECT_1")
-      .setMediaType(MediaTypes.PROTOBUF)
-      .execute();
+    ActivityResponse activityResponse = call(ws.newRequest().setParam("componentId", "PROJECT_1"));
 
-    WsCe.ActivityResponse activityResponse = Protobuf.read(wsResponse.getInputStream(), WsCe.ActivityResponse.parser());
     assertThat(activityResponse.getTasksCount()).isEqualTo(1);
     assertThat(activityResponse.getTasks(0).getId()).isEqualTo("T1");
     assertThat(activityResponse.getTasks(0).getStatus()).isEqualTo(WsCe.TaskStatus.SUCCESS);
@@ -227,15 +210,34 @@ public class ActivityActionTest {
     insertActivity("T2", "P2", CeActivityDto.Status.SUCCESS);
     insertActivity("T3", "P3", CeActivityDto.Status.SUCCESS);
 
-    TestResponse wsResponse = ws.newRequest()
-      .setParam("componentQuery", "apac")
-      .setMediaType(MediaTypes.PROTOBUF)
-      .execute();
+    ActivityResponse activityResponse = call(ws.newRequest().setParam(PARAM_COMPONENT_QUERY, "apac"));
 
-    WsCe.ActivityResponse activityResponse = WsCe.ActivityResponse.parseFrom(wsResponse.getInputStream());
     assertThat(activityResponse.getTasksList()).extracting("id").containsOnly("T1", "T2");
   }
 
+  @Test
+  public void search_task_id_in_queue_ignoring_other_parameters() throws IOException {
+    insertQueue("T1", "PROJECT_1", CeQueueDto.Status.IN_PROGRESS);
+
+    ActivityResponse result = call(
+      ws.newRequest()
+        .setParam(Param.TEXT_QUERY, "T1")
+        .setParam(PARAM_STATUS, CeQueueDto.Status.PENDING.name()));
+
+    assertThat(result.getTasksCount()).isEqualTo(1);
+    assertThat(result.getTasks(0).getId()).isEqualTo("T1");
+  }
+
+  @Test
+  public void search_task_id_in_activity() {
+    insertActivity("T1", "PROJECT_1", CeActivityDto.Status.SUCCESS);
+
+    ActivityResponse result = call(ws.newRequest().setParam(Param.TEXT_QUERY, "T1"));
+
+    assertThat(result.getTasksCount()).isEqualTo(1);
+    assertThat(result.getTasks(0).getId()).isEqualTo("T1");
+  }
+
   @Test
   public void fail_if_both_filters_on_component_id_and_name() {
     expectedException.expect(BadRequestException.class);
@@ -303,4 +305,15 @@ public class ActivityActionTest {
     dbTester.commit();
     return activityDto;
   }
+
+  private static ActivityResponse call(TestRequest request) {
+    try {
+      return ActivityResponse.parseFrom(
+        request
+          .setMediaType(MediaTypes.PROTOBUF)
+          .execute().getInputStream());
+    } catch (IOException e) {
+      throw Throwables.propagate(e);
+    }
+  }
 }
index a0e53ace0295b6a2d7680a96611a8a11d07e4b23..9701f06480d0369b018567f8e0d35f5a9e8a4922 100644 (file)
@@ -26,7 +26,7 @@ import javax.annotation.Nullable;
 
 public class ActivityWsRequest {
   private String componentId;
-  private String componentQuery;
+  private String query;
   private List<String> status;
   private String type;
   private Boolean onlyCurrents;
@@ -46,12 +46,12 @@ public class ActivityWsRequest {
   }
 
   @CheckForNull
-  public String getComponentQuery() {
-    return componentQuery;
+  public String getQuery() {
+    return query;
   }
 
-  public ActivityWsRequest setComponentQuery(@Nullable String componentQuery) {
-    this.componentQuery = componentQuery;
+  public ActivityWsRequest setQuery(@Nullable String query) {
+    this.query = query;
     return this;
   }
 
index 66aa65812ed8c276d1c8b78e7afbd08a1c37fc23..a05d383c67d2fb64669329a37343d328c7385133 100644 (file)
@@ -27,7 +27,6 @@ import org.sonarqube.ws.client.GetRequest;
 import org.sonarqube.ws.client.WsConnector;
 
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID;
-import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS;
@@ -44,7 +43,7 @@ public class CeService extends BaseService {
     return call(
       new GetRequest(path("activity"))
         .setParam(PARAM_COMPONENT_ID, request.getComponentId())
-        .setParam(PARAM_COMPONENT_QUERY, request.getComponentQuery())
+        .setParam("q", request.getQuery())
         .setParam(PARAM_STATUS, inlineMultipleParamValue(request.getStatus()))
         .setParam(PARAM_TYPE, request.getType())
         .setParam(PARAM_MAX_EXECUTED_AT, request.getMaxExecutedAt())
index f93004ffcbc21bc9d003d51900ae3f5c0bee812a..1125072b933411a956f081872dfa554134363e19 100644 (file)
@@ -32,7 +32,6 @@ import org.sonarqube.ws.client.WsConnector;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.mockito.Mockito.mock;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_ID;
-import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_COMPONENT_QUERY;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MAX_EXECUTED_AT;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_MIN_SUBMITTED_AT;
 import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_ONLY_CURRENTS;
@@ -41,7 +40,7 @@ import static org.sonarqube.ws.client.ce.CeWsParameters.PARAM_TYPE;
 
 public class CeServiceTest {
   private static final String VALUE_COMPONENT_ID = "component-uuid";
-  private static final String VALUE_COMPONENT_QUERY = "component-query";
+  private static final String VALUE_QUERY = "component-query";
   private static final String VALUE_TASK_STATUS_1 = "task-status";
   private static final String VALUE_TASK_STATUS_2 = "task-status-2";
   private static final String VALUE_TASK_TYPE = "task-type";
@@ -60,7 +59,7 @@ public class CeServiceTest {
   public void activity() {
     ActivityWsRequest request = new ActivityWsRequest()
       .setComponentId(VALUE_COMPONENT_ID)
-      .setComponentQuery(VALUE_COMPONENT_QUERY)
+      .setQuery(VALUE_QUERY)
       .setStatus(ImmutableList.of(VALUE_TASK_STATUS_1, VALUE_TASK_STATUS_2))
       .setType(VALUE_TASK_TYPE)
       .setPage(VALUE_PAGE)
@@ -78,7 +77,7 @@ public class CeServiceTest {
     serviceTester.assertThat(result)
       .hasPath("activity")
       .hasParam(PARAM_COMPONENT_ID, VALUE_COMPONENT_ID)
-      .hasParam(PARAM_COMPONENT_QUERY, VALUE_COMPONENT_QUERY)
+      .hasParam("q", VALUE_QUERY)
       .hasParam(PARAM_STATUS, VALUE_TASK_STATUS_1 + "," + VALUE_TASK_STATUS_2)
       .hasParam(PARAM_TYPE, VALUE_TASK_TYPE)
       .hasParam(PARAM_MAX_EXECUTED_AT, VALUE_MAX_EXECUTED_AT)