From 6997a3ff9b348ca9147ebe83ef33bc8b008a97a1 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Thu, 24 Aug 2017 14:00:45 +0200 Subject: [PATCH] SONAR-9616 Handle branch in api/components/app --- .../sonar/server/component/ws/AppAction.java | 24 +++++-- .../server/component/ws/AppActionTest.java | 69 ++++++++++++++++++- 2 files changed, 85 insertions(+), 8 deletions(-) diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java b/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java index 8088c04a4f7..56d4f4aa89f 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java @@ -43,7 +43,6 @@ import org.sonar.db.metric.MetricDto; import org.sonar.db.property.PropertyDto; import org.sonar.db.property.PropertyQuery; import org.sonar.server.component.ComponentFinder; -import org.sonar.server.component.ComponentFinder.ParamNames; import org.sonar.server.user.UserSession; import static com.google.common.base.Preconditions.checkArgument; @@ -58,7 +57,6 @@ import static org.sonar.api.measures.CoreMetrics.TESTS_KEY; import static org.sonar.api.measures.CoreMetrics.VIOLATIONS; import static org.sonar.api.measures.CoreMetrics.VIOLATIONS_KEY; import static org.sonar.core.util.Uuids.UUID_EXAMPLE_01; -import static org.sonar.server.component.ComponentFinder.ParamNames.*; import static org.sonar.server.component.ComponentFinder.ParamNames.COMPONENT_ID_AND_COMPONENT; import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001; import static org.sonar.server.ws.KeyExamples.KEY_PROJECT_EXAMPLE_001; @@ -68,7 +66,6 @@ public class AppAction implements ComponentsWsAction { private static final String PARAM_COMPONENT_ID = "componentId"; private static final String PARAM_COMPONENT = "component"; - private static final String PARAM_PERIOD = "period"; private static final List METRIC_KEYS = ImmutableList.of( LINES_KEY, VIOLATIONS_KEY, @@ -108,15 +105,18 @@ public class AppAction implements ComponentsWsAction { .setDescription("Component key") .setExampleValue(KEY_PROJECT_EXAMPLE_001) .setSince("6.4"); + + action.createParam(PARAM_BRANCH) + .setDescription("Branch key") + .setSince("6.6") + .setInternal(true) + .setExampleValue(KEY_BRANCH_EXAMPLE_001); } @Override public void handle(Request request, Response response) { try (DbSession session = dbClient.openSession(false)) { - ComponentDto component = componentFinder.getByUuidOrKey(session, - request.param(PARAM_COMPONENT_ID), - request.param(PARAM_COMPONENT), - COMPONENT_ID_AND_COMPONENT); + ComponentDto component = loadComponent(session, request); userSession.checkComponentPermission(UserRole.USER, component); JsonWriter json = response.newJsonWriter(); @@ -130,6 +130,16 @@ public class AppAction implements ComponentsWsAction { } } + private ComponentDto loadComponent(DbSession dbSession, Request request) { + String componentUuid = request.param(PARAM_COMPONENT_ID); + String branch = request.param("branch"); + checkArgument(componentUuid == null || branch == null, "'%s' and '%s' parameters cannot be used at the same time", PARAM_COMPONENT_ID, PARAM_BRANCH); + if (branch == null) { + return componentFinder.getByUuidOrKey(dbSession, componentUuid, request.param(PARAM_COMPONENT), COMPONENT_ID_AND_COMPONENT); + } + return componentFinder.getByKeyAndOptionalBranch(dbSession, request.mandatoryParam(PARAM_COMPONENT), branch); + } + private void appendComponent(JsonWriter json, ComponentDto component, UserSession userSession, DbSession session) { List propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder() .setKey("favourite") diff --git a/server/sonar-server/src/test/java/org/sonar/server/component/ws/AppActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/component/ws/AppActionTest.java index 82a262b2aa6..8eaf43d42c3 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/component/ws/AppActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/component/ws/AppActionTest.java @@ -250,6 +250,44 @@ public class AppActionTest { "}\n"); } + @Test + public void branch() { + ComponentDto project = db.components().insertMainBranch(); + userSession.logIn("john").addProjectPermission(USER, project); + ComponentDto branch = db.components().insertProjectBranch(project); + ComponentDto module = db.components().insertComponent(newModuleDto(branch)); + ComponentDto directory = db.components().insertComponent(newDirectory(module, "src")); + ComponentDto file = db.components().insertComponent(newFileDto(module, directory)); + SnapshotDto analysis = db.components().insertSnapshot(branch); + MetricDto coverage = db.measures().insertMetric(m -> m.setKey(COVERAGE_KEY)); + db.measures().insertMeasure(file, analysis, coverage, m -> m.setValue(95.4d)); + + String result = ws.newRequest() + .setParam("component", file.getKey()) + .setParam("branch", file.getBranch()) + .execute() + .getInput(); + + assertJson(result).isSimilarTo("{\n" + + " \"key\": \"" + file.getKey() + "\",\n" + + " \"branch\": \"" + file.getBranch() + "\",\n" + + " \"uuid\": \"" + file.uuid() + "\",\n" + + " \"path\": \"" + file.path() + "\",\n" + + " \"name\": \"" + file.name() + "\",\n" + + " \"longName\": \"" + file.longName() + "\",\n" + + " \"q\": \"" + file.qualifier() + "\",\n" + + " \"subProject\": \"" + module.getKey() + "\",\n" + + " \"subProjectName\": \"" + module.longName() + "\",\n" + + " \"project\": \"" + project.getKey() + "\",\n" + + " \"projectName\": \"" + project.longName() + "\",\n" + + " \"fav\": false,\n" + + " \"canMarkAsFavorite\": true,\n" + + " \"measures\": {\n" + + " \"coverage\": \"95.4\"\n" + + " }\n" + + "}\n"); + } + @Test public void fail_if_no_parameter_provided() { expectedException.expect(IllegalArgumentException.class); @@ -258,6 +296,21 @@ public class AppActionTest { ws.newRequest().execute(); } + @Test + public void fail_if_both_componentId_and_branch_parameters_provided() { + ComponentDto project = db.components().insertMainBranch(); + ComponentDto branch = db.components().insertProjectBranch(project); + ComponentDto file = db.components().insertComponent(newFileDto(branch)); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage("'componentId' and 'branch' parameters cannot be used at the same time"); + + ws.newRequest() + .setParam("uuid", file.uuid()) + .setParam("branch", file.getBranch()) + .execute(); + } + @Test public void fail_when_component_not_found() throws Exception { ComponentDto project = db.components().insertPrivateProject(); @@ -270,6 +323,20 @@ public class AppActionTest { .execute(); } + @Test + public void fail_when_branch_not_found() throws Exception { + ComponentDto project = db.components().insertPrivateProject(); + ComponentDto branch = db.components().insertProjectBranch(project); + ComponentDto file = db.components().insertComponent(newFileDto(branch)); + + expectedException.expect(NotFoundException.class); + + ws.newRequest() + .setParam("component", file.getKey()) + .setParam("branch", "unknown") + .execute(); + } + @Test public void fail_when_missing_permission() throws Exception { ComponentDto project = db.components().insertPrivateProject(); @@ -289,7 +356,7 @@ public class AppActionTest { assertThat(action.isInternal()).isTrue(); assertThat(action.isPost()).isFalse(); assertThat(action.handler()).isNotNull(); - assertThat(action.params()).hasSize(2); + assertThat(action.params()).hasSize(3); } } -- 2.39.5