]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 Allow to return main branch in api/projectbranches/show
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Tue, 22 Aug 2017 13:10:53 +0000 (15:10 +0200)
committerJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 12 Sep 2017 09:34:48 +0000 (11:34 +0200)
server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentDbTester.java
server/sonar-server/src/main/java/org/sonar/server/projectbranch/ws/ShowAction.java
server/sonar-server/src/test/java/org/sonar/server/projectbranch/ws/ShowActionTest.java

index 172cad17f474cdb26b34399791a63655b8976aa7..bda32e32bca458d6ec9fd38869c4bcec686d785f 100644 (file)
@@ -215,6 +215,17 @@ public class ComponentDbTester {
     return project;
   }
 
+  @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
index a8a68b1605305ee7ea03d555fc2336c1b2c213ce..3705194836589b341899dfb3de844828fb024eb0 100644 (file)
@@ -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);
index 1dd53096a2597b8eaf7595a88283fbdd3f1e4d67..081229764411693116854172de7e744843292030 100644 (file)
@@ -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();