]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 Handle branch in api/components/app
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Thu, 24 Aug 2017 12:00:45 +0000 (14:00 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 09:34:51 +0000 (11:34 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/ws/AppAction.java
server/sonar-server/src/test/java/org/sonar/server/component/ws/AppActionTest.java

index 8088c04a4f7e207448641b7bfa212116e1fe12b1..56d4f4aa89fe1cb9c42e658427d968d8b0cbc2bd 100644 (file)
@@ -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<String> 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<PropertyDto> propertyDtos = dbClient.propertiesDao().selectByQuery(PropertyQuery.builder()
       .setKey("favourite")
index 82a262b2aa65b293615b2f13e199d3c2dafc5ed1..8eaf43d42c3bed90e6bf7b44247e87e54d59db5a 100644 (file)
@@ -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);
   }
 
 }