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;
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(
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>" +
@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) {
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))
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;
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"
*/
}
@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
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();
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);
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())
call(
new PostRequest(path("cancel"))
.setParam("id", request.getId())
- .setMediaType(MediaTypes.JSON)
- ).content();
+ .setMediaType(MediaTypes.JSON)).content();
}
/**
public void cancelAll() {
call(
new PostRequest(path("cancel_all"))
- .setMediaType(MediaTypes.JSON)
- ).content();
+ .setMediaType(MediaTypes.JSON)).content();
}
/**
public void pause() {
call(
new PostRequest(path("pause"))
- .setMediaType(MediaTypes.JSON)
- ).content();
+ .setMediaType(MediaTypes.JSON)).content();
}
/**
public void resume() {
call(
new PostRequest(path("resume"))
- .setMediaType(MediaTypes.JSON)
- ).content();
+ .setMediaType(MediaTypes.JSON)).content();
}
/**