diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-08-22 15:10:53 +0200 |
---|---|---|
committer | Janos Gyerik <janos.gyerik@sonarsource.com> | 2017-09-12 11:34:48 +0200 |
commit | 185d5ffdf83dd15fe0f578bbc3b23fdeea2044db (patch) | |
tree | 1a4b29c1491b9e211d9dee7866b566eff5ef03d8 | |
parent | 63302f1ca5371687d2994b0602726bfe08359602 (diff) | |
download | sonarqube-185d5ffdf83dd15fe0f578bbc3b23fdeea2044db.tar.gz sonarqube-185d5ffdf83dd15fe0f578bbc3b23fdeea2044db.zip |
SONAR-9616 Allow to return main branch in api/projectbranches/show
3 files changed, 53 insertions, 16 deletions
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java index 172cad17f47..bda32e32bca 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java @@ -216,6 +216,17 @@ public class ComponentDbTester { } @SafeVarargs + public final ComponentDto insertMainBranch(OrganizationDto organization, String mainBranchName, Consumer<ComponentDto>... dtoPopulators) { + ComponentDto project = newPrivateProjectDto(organization); + BranchDto branchDto = newBranchDto(project, LONG).setKey(mainBranchName); + Arrays.stream(dtoPopulators).forEach(dtoPopulator -> dtoPopulator.accept(project)); + insertComponent(project); + dbClient.branchDao().insert(dbSession, branchDto); + db.commit(); + return project; + } + + @SafeVarargs public final ComponentDto insertProjectBranch(ComponentDto project, Consumer<BranchDto>... dtoPopulators) { // MainBranchProjectUuid will be null if it's a main branch BranchDto branchDto = newBranchDto(firstNonNull(project.getMainBranchProjectUuid(), project.projectUuid()), LONG); diff --git a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java index a8a68b16053..37051948365 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java @@ -92,17 +92,16 @@ public class ShowAction implements BranchWsAction { action .createParam(PARAM_BRANCH) .setDescription("Branch key") - .setExampleValue(KEY_BRANCH_EXAMPLE_001) - .setRequired(true); + .setExampleValue(KEY_BRANCH_EXAMPLE_001); } @Override public void handle(Request request, Response response) throws Exception { String projectKey = request.mandatoryParam(PARAM_COMPONENT); - String branchName = request.mandatoryParam(PARAM_BRANCH); + String branchName = request.param(PARAM_BRANCH); try (DbSession dbSession = dbClient.openSession(false)) { - ComponentDto component = componentFinder.getByKeyAndBranch(dbSession, projectKey, branchName); + ComponentDto component = loadComponent(dbSession, projectKey, branchName); userSession.checkComponentPermission(UserRole.USER, component); List<MetricDto> metrics = dbClient.metricDao().selectByKeys(dbSession, asList(ALERT_STATUS_KEY, BUGS_KEY, VULNERABILITIES_KEY, CODE_SMELLS_KEY)); @@ -121,6 +120,13 @@ public class ShowAction implements BranchWsAction { } } + private ComponentDto loadComponent(DbSession dbSession, String projectKey, @Nullable String branchName){ + if (branchName == null) { + return componentFinder.getByKey(dbSession, projectKey); + } + return componentFinder.getByKeyAndBranch(dbSession, projectKey, branchName); + } + private BranchDto getBranch(DbSession dbSession, String uuid) { Optional<BranchDto> branch = dbClient.branchDao().selectByUuid(dbSession, uuid); checkState(branch.isPresent(), "Branch uuid '%s' not found", uuid); diff --git a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java index 1dd53096a25..08122976441 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java @@ -91,6 +91,36 @@ public class ShowActionTest { } @Test + public void main_branch_when_no_branch_parameter() { + ComponentDto project = db.components().insertMainBranch(); + userSession.logIn().addProjectPermission(UserRole.USER, project); + + ShowWsResponse response = ws.newRequest() + .setParam("component", project.getKey()) + .executeProtobuf(ShowWsResponse.class); + + assertThat(response.getBranch()) + .extracting(Branch::hasName, Branch::getType, Branch::getIsMain, Branch::hasMergeBranch) + .containsExactlyInAnyOrder(false, Common.BranchType.LONG, true, false); + } + + @Test + public void main_branch_with_name() { + OrganizationDto organizationD = db.organizations().insert(); + ComponentDto project = db.components().insertMainBranch(organizationD, "head"); + + userSession.logIn().addProjectPermission(UserRole.USER, project); + + ShowWsResponse response = ws.newRequest() + .setParam("component", project.getKey()) + .executeProtobuf(ShowWsResponse.class); + + assertThat(response.getBranch()) + .extracting(Branch::getName, Branch::getType, Branch::getIsMain) + .containsExactlyInAnyOrder("head", Common.BranchType.LONG, true); + } + + @Test public void long_living_branch() { ComponentDto project = db.components().insertMainBranch(); userSession.logIn().addProjectPermission(UserRole.USER, project); @@ -161,9 +191,9 @@ public class ShowActionTest { .setParam("branch", shortLivingBranch.getBranch()) .executeProtobuf(ShowWsResponse.class); - assertThat(response.getBranch().getStatus()) - .extracting(Branch.Status::hasBugs, Branch.Status::getBugs, Branch.Status::hasVulnerabilities, Branch.Status::getVulnerabilities, Branch.Status::hasCodeSmells, Branch.Status::getCodeSmells) + .extracting(Branch.Status::hasBugs, Branch.Status::getBugs, Branch.Status::hasVulnerabilities, Branch.Status::getVulnerabilities, Branch.Status::hasCodeSmells, + Branch.Status::getCodeSmells) .containsExactlyInAnyOrder(true, 1, true, 2, true, 3); } @@ -195,16 +225,6 @@ public class ShowActionTest { } @Test - public void fail_if_missing_branch_parameter() { - expectedException.expect(IllegalArgumentException.class); - expectedException.expectMessage("The 'branch' parameter is missing"); - - ws.newRequest() - .setParam("component", "my_project") - .execute(); - } - - @Test public void fail_if_branch_does_not_exist() { ComponentDto project = db.components().insertPrivateProject(); ComponentDto file = db.components().insertComponent(newFileDto(project)); |