From: Julien Lancelot Date: Wed, 23 Aug 2017 15:55:34 +0000 (+0200) Subject: SONAR-9616 Handle branch in api/sources/raw X-Git-Tag: 6.6-RC1~380^2~60 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=9fd029a804c6bd761ef282bc2188a93bc463c48a;p=sonarqube.git SONAR-9616 Handle branch in api/sources/raw --- diff --git a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java index 15ca2b9ebda..cb191f65287 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java +++ b/server/sonar-server/src/main/java/org/sonar/server/component/ComponentFinder.java @@ -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"), diff --git a/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java b/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java index dc5f6618f2f..a551eb9dc81 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/source/ws/RawAction.java @@ -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> lines = sourceService.getLinesAsRawText(dbSession, file.uuid(), 1, Integer.MAX_VALUE); diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java index 32d52f34ebc..5e06d4f46fe 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/RawActionTest.java @@ -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); diff --git a/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java index 7b1c601b397..70e1ab3c125 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/source/ws/SourcesWsTest.java @@ -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();