]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-9616 propagate visibility to branches (#2392)
authorJanos Gyerik <janos.gyerik@sonarsource.com>
Tue, 22 Aug 2017 13:51:37 +0000 (15:51 +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/ComponentTesting.java
server/sonar-server/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java
server/sonar-server/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java

index 7695b2d77ea2d963a95c7547de377622e61350d4..0321d19dbd09cf0db93ff9eeba7736bd5bd5090d 100644 (file)
@@ -213,6 +213,10 @@ public class ComponentTesting {
       .setBranchType(branchType);
   }
 
+  public static BranchDto newBranchDto(ComponentDto project) {
+    return newBranchDto(project.projectUuid(), BranchType.LONG);
+  }
+
   public static BranchDto newBranchDto(ComponentDto branchComponent, BranchType branchType) {
     boolean isMain = branchComponent.getMainBranchProjectUuid() == null;
     String projectUuid = isMain ? branchComponent.uuid() : branchComponent.getMainBranchProjectUuid();
index 709ae53305fca0a9e5009dd1e8a42fdc0f710964..a1531c9f032ec5f6073a91e52bf738d837050245 100644 (file)
@@ -26,7 +26,9 @@ import org.sonar.api.server.ws.WebService;
 import org.sonar.api.web.UserRole;
 import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
+import org.sonar.db.component.BranchMapper;
 import org.sonar.db.component.ComponentDto;
+import org.sonar.db.component.ComponentMapper;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.GroupPermissionDto;
 import org.sonar.db.permission.UserPermissionDto;
@@ -97,7 +99,7 @@ public class UpdateVisibilityAction implements ProjectsWsAction {
         OrganizationDto organization = dbClient.organizationDao().selectByUuid(dbSession, component.getOrganizationUuid())
           .orElseThrow(() -> new IllegalStateException(format("Could not find organization with uuid '%s' of project '%s'", component.getOrganizationUuid(), projectKey)));
         projectsWsSupport.checkCanUpdateProjectsVisibility(organization, changeToPrivate);
-        dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, component.uuid(), changeToPrivate);
+        setPrivateForRootComponentUuid(dbSession, component.uuid(), changeToPrivate);
         if (changeToPrivate) {
           updatePermissionsToPrivate(dbSession, component);
         } else {
@@ -108,6 +110,15 @@ public class UpdateVisibilityAction implements ProjectsWsAction {
     }
   }
 
+  private void setPrivateForRootComponentUuid(DbSession dbSession, String uuid, boolean isPrivate) {
+    dbClient.componentDao().setPrivateForRootComponentUuid(dbSession, uuid, isPrivate);
+    ComponentMapper mapper = dbSession.getMapper(ComponentMapper.class);
+    dbSession.getMapper(BranchMapper.class).selectByProjectUuid(uuid)
+      .stream()
+      .filter(branch -> !uuid.equals(branch.getUuid()))
+      .forEach(branch -> mapper.setPrivateForRootComponentUuid(branch.getUuid(), isPrivate));
+  }
+
   private boolean noPendingTask(DbSession dbSession, ComponentDto rootComponent) {
     return dbClient.ceQueueDao().selectByComponentUuid(dbSession, rootComponent.uuid()).isEmpty();
   }
index ff7f4bd875ad0952bba2f4d0d3549119a57483e4..0931fa75cea7866b4a67c1e96bdb25833520d319 100644 (file)
@@ -35,6 +35,7 @@ import org.sonar.db.DbClient;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
 import org.sonar.db.ce.CeQueueDto;
+import org.sonar.db.component.BranchDto;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.organization.OrganizationDto;
@@ -281,10 +282,16 @@ public class UpdateVisibilityActionTest {
   public void execute_changes_private_flag_of_specified_project_and_all_children_to_specified_new_visibility() {
     ComponentDto project = randomPublicOrPrivateProject();
     boolean initiallyPrivate = project.isPrivate();
+
+    BranchDto branchDto = ComponentTesting.newBranchDto(project);
+    dbClient.branchDao().insert(dbSession, branchDto);
+
+    ComponentDto branch = ComponentTesting.newProjectBranch(project, branchDto);
     ComponentDto module = ComponentTesting.newModuleDto(project);
     ComponentDto dir = ComponentTesting.newDirectory(project, "path");
     ComponentDto file = ComponentTesting.newFileDto(project);
-    dbTester.components().insertComponents(module, dir, file);
+
+    dbTester.components().insertComponents(branch, module, dir, file);
     userSessionRule.addProjectPermission(UserRole.ADMIN, project);
 
     request.setParam(PARAM_PROJECT, project.getDbKey())
@@ -292,6 +299,7 @@ public class UpdateVisibilityActionTest {
       .execute();
 
     assertThat(isPrivateInDb(project)).isEqualTo(!initiallyPrivate);
+    assertThat(isPrivateInDb(branch)).isEqualTo(!initiallyPrivate);
     assertThat(isPrivateInDb(module)).isEqualTo(!initiallyPrivate);
     assertThat(isPrivateInDb(dir)).isEqualTo(!initiallyPrivate);
     assertThat(isPrivateInDb(file)).isEqualTo(!initiallyPrivate);
@@ -301,6 +309,12 @@ public class UpdateVisibilityActionTest {
   public void execute_has_no_effect_if_specified_project_already_has_specified_visibility() {
     ComponentDto project = randomPublicOrPrivateProject();
     boolean initiallyPrivate = project.isPrivate();
+
+    BranchDto branchDto = ComponentTesting.newBranchDto(project);
+    dbClient.branchDao().insert(dbSession, branchDto);
+
+    ComponentDto branch = ComponentTesting.newProjectBranch(project, branchDto)
+      .setPrivate(initiallyPrivate);
     ComponentDto module = ComponentTesting.newModuleDto(project)
       .setPrivate(initiallyPrivate);
     ComponentDto dir = ComponentTesting.newDirectory(project, "path")
@@ -308,7 +322,7 @@ public class UpdateVisibilityActionTest {
       .setPrivate(!initiallyPrivate);
     ComponentDto file = ComponentTesting.newFileDto(project)
       .setPrivate(initiallyPrivate);
-    dbTester.components().insertComponents(module, dir, file);
+    dbTester.components().insertComponents(branch, module, dir, file);
     userSessionRule.addProjectPermission(UserRole.ADMIN, project);
 
     request.setParam(PARAM_PROJECT, project.getDbKey())
@@ -316,6 +330,7 @@ public class UpdateVisibilityActionTest {
       .execute();
 
     assertThat(isPrivateInDb(project)).isEqualTo(initiallyPrivate);
+    assertThat(isPrivateInDb(branch)).isEqualTo(initiallyPrivate);
     assertThat(isPrivateInDb(module)).isEqualTo(initiallyPrivate);
     assertThat(isPrivateInDb(dir)).isEqualTo(!initiallyPrivate);
     assertThat(isPrivateInDb(file)).isEqualTo(initiallyPrivate);