]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 Handle branch in api/sources/raw
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 23 Aug 2017 15:55:34 +0000 (17:55 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 09:34:50 +0000 (11:34 +0200)
server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java
server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java
server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java

index 15ca2b9ebdae0d745f224a8c8798232af3161202..cb191f6528736f364cbd8bd2848ddae95968fb07 100644 (file)
@@ -154,6 +154,10 @@ public class ComponentFinder {
     throw new NotFoundException(format("Component '%s' on branch '%s' not found", key, branch));
   }
 
+  public ComponentDto getByKeyAndOptionalBranch(DbSession dbSession, String key, @Nullable String branch) {
+    return branch == null ? getByKey(dbSession, key) : getByKeyAndBranch(dbSession, key, branch);
+  }
+
   public enum ParamNames {
     PROJECT_ID_AND_KEY("projectId", "projectKey"),
     PROJECT_UUID_AND_KEY("projectUuid", "projectKey"),
index dc5f6618f2f2aa3656fe48fbd060ef0d672b409f..a551eb9dc81ccdc33089455f19c9b6337e2c5f62 100644 (file)
@@ -35,6 +35,8 @@ import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.source.SourceService;
 import org.sonar.server.user.UserSession;
 
+import static org.sonar.server.ws.KeyExamples.KEY_BRANCH_EXAMPLE_001;
+
 public class RawAction implements SourcesWsAction {
 
   private final DbClient dbClient;
@@ -62,14 +64,20 @@ public class RawAction implements SourcesWsAction {
       .setRequired(true)
       .setDescription("File key")
       .setExampleValue("my_project:src/foo/Bar.php");
+
+    action
+      .createParam("branch")
+      .setDescription("Branch key")
+      .setInternal(true)
+      .setExampleValue(KEY_BRANCH_EXAMPLE_001);
   }
 
   @Override
   public void handle(Request request, Response response) {
     String fileKey = request.mandatoryParam("key");
-
+    String branch = request.param("branch");
     try (DbSession dbSession = dbClient.openSession(false)) {
-      ComponentDto file = componentFinder.getByKey(dbSession, fileKey);
+      ComponentDto file = componentFinder.getByKeyAndOptionalBranch(dbSession, fileKey, branch);
       userSession.checkComponentPermission(UserRole.CODEVIEWER, file);
 
       Optional<Iterable<String>> lines = sourceService.getLinesAsRawText(dbSession, file.uuid(), 1, Integer.MAX_VALUE);
index 32d52f34ebc7df9a12ff40ce4b315ad14f384464..5e06d4f46fe2b28cbdd60ed7462ffabed3951a85 100644 (file)
@@ -27,7 +27,6 @@ import org.sonar.api.utils.System2;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbTester;
 import org.sonar.db.component.ComponentDto;
-import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.component.ResourceTypesRule;
 import org.sonar.server.component.ComponentFinder;
 import org.sonar.server.exceptions.ForbiddenException;
@@ -36,7 +35,9 @@ import org.sonar.server.source.SourceService;
 import org.sonar.server.tester.UserSessionRule;
 import org.sonar.server.ws.WsActionTester;
 
+import static java.lang.String.format;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.component.ComponentTesting.newFileDto;
 import static org.sonar.db.protobuf.DbFileSources.Data;
 import static org.sonar.db.protobuf.DbFileSources.Line;
 
@@ -59,7 +60,7 @@ public class RawActionTest {
   public void raw_from_file() throws Exception {
     ComponentDto project = db.components().insertPrivateProject();
     userSession.addProjectPermission(UserRole.CODEVIEWER, project);
-    ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
+    ComponentDto file = db.components().insertComponent(newFileDto(project));
     db.fileSources().insertFileSource(file, s -> s.setSourceData(
       Data.newBuilder()
         .addLines(Line.newBuilder().setLine(1).setSource("public class HelloWorld {").build())
@@ -73,6 +74,26 @@ public class RawActionTest {
     assertThat(result).isEqualTo("public class HelloWorld {\n}\n");
   }
 
+  @Test
+  public void raw_from_branch_file() throws Exception {
+    ComponentDto project = db.components().insertMainBranch();
+    userSession.addProjectPermission(UserRole.CODEVIEWER, project);
+    ComponentDto branch = db.components().insertProjectBranch(project);
+    ComponentDto file = db.components().insertComponent(newFileDto(branch));
+    db.fileSources().insertFileSource(file, s -> s.setSourceData(
+      Data.newBuilder()
+        .addLines(Line.newBuilder().setLine(1).setSource("public class HelloWorld {").build())
+        .addLines(Line.newBuilder().setLine(2).setSource("}").build())
+        .build()));
+
+    String result = ws.newRequest()
+      .setParam("key", file.getKey())
+      .setParam("branch", file.getBranch())
+      .execute().getInput();
+
+    assertThat(result).isEqualTo("public class HelloWorld {\n}\n");
+  }
+
   @Test
   public void fail_on_unknown_file() throws Exception {
     expectedException.expect(NotFoundException.class);
@@ -83,11 +104,44 @@ public class RawActionTest {
       .execute();
   }
 
+  @Test
+  public void fail_on_unknown_branch() throws Exception {
+    ComponentDto project = db.components().insertMainBranch();
+    userSession.addProjectPermission(UserRole.CODEVIEWER, project);
+    ComponentDto branch = db.components().insertProjectBranch(project);
+    ComponentDto file = db.components().insertComponent(newFileDto(branch));
+    db.fileSources().insertFileSource(file);
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage(format("Component '%s' on branch 'unknown' not found", file.getKey()));
+
+    ws.newRequest()
+      .setParam("key", file.getKey())
+      .setParam("branch", "unknown")
+      .execute();
+  }
+
+  @Test
+  public void fail_when_using_branch_db_key() throws Exception {
+    ComponentDto project = db.components().insertMainBranch();
+    userSession.addProjectPermission(UserRole.CODEVIEWER, project);
+    ComponentDto branch = db.components().insertProjectBranch(project);
+    ComponentDto file = db.components().insertComponent(newFileDto(branch));
+    db.fileSources().insertFileSource(file);
+
+    expectedException.expect(NotFoundException.class);
+    expectedException.expectMessage(format("Component key '%s' not found", file.getDbKey()));
+
+    ws.newRequest()
+      .setParam("key", file.getDbKey())
+      .execute();
+  }
+
   @Test
   public void fail_when_wrong_permission() throws Exception {
     ComponentDto project = db.components().insertPrivateProject();
     userSession.addProjectPermission(UserRole.ISSUE_ADMIN, project);
-    ComponentDto file = db.components().insertComponent(ComponentTesting.newFileDto(project));
+    ComponentDto file = db.components().insertComponent(newFileDto(project));
 
     expectedException.expect(ForbiddenException.class);
 
index 7b1c601b397c7b4f833b959f78f388a4fc1e8990..70e1ab3c125e38706f7a0daba49955d06e0b6064 100644 (file)
@@ -64,7 +64,7 @@ public class SourcesWsTest {
     assertThat(raw.since()).isEqualTo("5.0");
     assertThat(raw.isInternal()).isFalse();
     assertThat(raw.responseExampleAsString()).isNotEmpty();
-    assertThat(raw.params()).hasSize(1);
+    assertThat(raw.params()).hasSize(2);
 
     WebService.Action lines = controller.action("lines");
     assertThat(lines).isNotNull();