From: Teryk Bellahsene Date: Thu, 15 Sep 2016 15:24:27 +0000 (+0200) Subject: SONAR-7950 WS ce/component supports componentKey X-Git-Tag: 6.1-RC1~14 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=b2d35f32ed497d06a4f2b8b33104fcc65562e2a7;p=sonarqube.git SONAR-7950 WS ce/component supports componentKey --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java index 7b1d92eab46..71223759e8b 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/ce/ws/ComponentAction.java @@ -33,13 +33,16 @@ import org.sonar.db.ce.CeTaskQuery; 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; @@ -56,23 +59,32 @@ public class ComponentAction implements CeWsAction { @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.
" + + .setDescription("Get the pending tasks, in-progress tasks and the last executed task of a given component (usually a project).
" + + "Requires one of the following permissions:" + + "" + + "Either '%s' or '%s' must be provided, not both.
" + "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 queueDtos = dbClient.ceQueueDao().selectByComponentUuid(dbSession, component.uuid()); CeTaskQuery activityQuery = new CeTaskQuery() diff --git a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java index 23bcf43debd..cd7c6b81f75 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/ce/ws/ComponentActionTest.java @@ -24,13 +24,16 @@ import org.junit.Test; 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; @@ -40,6 +43,8 @@ import org.sonarqube.ws.WsCe; 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 { @@ -55,14 +60,14 @@ 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(); @@ -82,7 +87,7 @@ public class ComponentActionTest { 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(); @@ -96,6 +101,21 @@ public class ComponentActionTest { 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")); @@ -106,7 +126,7 @@ public class ComponentActionTest { 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(); @@ -123,12 +143,36 @@ public class ComponentActionTest { 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);