From 63302f1ca5371687d2994b0602726bfe08359602 Mon Sep 17 00:00:00 2001 From: Janos Gyerik Date: Tue, 22 Aug 2017 15:51:37 +0200 Subject: [PATCH] SONAR-9616 propagate visibility to branches (#2392) --- .../sonar/db/component/ComponentTesting.java | 4 ++++ .../project/ws/UpdateVisibilityAction.java | 13 ++++++++++++- .../ws/UpdateVisibilityActionTest.java | 19 +++++++++++++++++-- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTesting.java b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTesting.java index 7695b2d77ea..0321d19dbd0 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTesting.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/component/ComponentTesting.java @@ -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(); diff --git a/server/sonar-server/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java b/server/sonar-server/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java index 709ae53305f..a1531c9f032 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/project/ws/UpdateVisibilityAction.java @@ -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(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java index ff7f4bd875a..0931fa75cea 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/project/ws/UpdateVisibilityActionTest.java @@ -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); -- 2.39.5