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;
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;
"<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()
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);
}
}
+ 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());
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);
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))
.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);
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;
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.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;
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 {
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);
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");
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);
}
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");
}
}
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);
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);
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);
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);
+ }
+ }
}