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;
@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()
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;
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 {
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();
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();
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"));
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();
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);