From: Julien Lancelot Date: Tue, 22 Aug 2017 13:10:53 +0000 (+0200) Subject: SONAR-9616 Allow to return main branch in api/projectbranches/show X-Git-Tag: 6.6-RC1~380^2~74 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=185d5ffdf83dd15fe0f578bbc3b23fdeea2044db;p=sonarqube.git SONAR-9616 Allow to return main branch in api/projectbranches/show --- 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 @@ -215,6 +215,17 @@ public class ComponentDbTester { return project; } + @SafeVarargs + public final ComponentDto insertMainBranch(OrganizationDto organization, String mainBranchName, Consumer... 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... dtoPopulators) { // MainBranchProjectUuid will be null if it's a main branch 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 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 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 @@ -90,6 +90,36 @@ public class ShowActionTest { assertThat(definition.since()).isEqualTo("6.6"); } + @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(); @@ -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); } @@ -194,16 +224,6 @@ public class ShowActionTest { .execute(); } - @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();