From 7453617939eff3e8d3f5cc4c84f21b38c5c50701 Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Thu, 21 Dec 2023 11:15:22 +0100 Subject: [PATCH] SONAR-21244 Clean github_orgs_groups table when disabling GitHub auto provisioning. --- .../sonar/auth/github/GitHubSettingsIT.java} | 20 +++++++++++++------ .../org/sonar/auth/github/GitHubSettings.java | 1 + .../GithubOrganizationGroupDaoIT.java | 18 +++++++++++++++-- .../GithubOrganizationGroupDao.java | 8 ++++++++ .../GithubOrganizationGroupMapper.java | 2 ++ .../GithubOrganizationGroupMapper.xml | 4 ++++ 6 files changed, 45 insertions(+), 8 deletions(-) rename server/sonar-auth-github/src/{test/java/org/sonar/auth/github/GitHubSettingsTest.java => it/java/org/sonar/auth/github/GitHubSettingsIT.java} (94%) diff --git a/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubSettingsTest.java b/server/sonar-auth-github/src/it/java/org/sonar/auth/github/GitHubSettingsIT.java similarity index 94% rename from server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubSettingsTest.java rename to server/sonar-auth-github/src/it/java/org/sonar/auth/github/GitHubSettingsIT.java index 5641897330f..b88b8929fa7 100644 --- a/server/sonar-auth-github/src/test/java/org/sonar/auth/github/GitHubSettingsTest.java +++ b/server/sonar-auth-github/src/it/java/org/sonar/auth/github/GitHubSettingsIT.java @@ -28,6 +28,7 @@ import org.sonar.api.config.PropertyDefinitions; import org.sonar.api.config.internal.MapSettings; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.db.provisioning.GithubOrganizationGroupDto; import org.sonar.db.user.GroupDto; import org.sonar.server.property.InternalProperties; @@ -36,10 +37,10 @@ import static org.assertj.core.api.Assertions.assertThatIllegalStateException; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import static org.sonar.auth.github.GitHubSettings.USER_CONSENT_FOR_PERMISSIONS_REQUIRED_AFTER_UPGRADE; import static org.sonar.auth.github.GitHubSettings.PROVISION_VISIBILITY; +import static org.sonar.auth.github.GitHubSettings.USER_CONSENT_FOR_PERMISSIONS_REQUIRED_AFTER_UPGRADE; -public class GitHubSettingsTest { +public class GitHubSettingsIT { @Rule public DbTester db = DbTester.create(System2.INSTANCE); @@ -124,6 +125,7 @@ public class GitHubSettingsTest { settings.setProperty(PROVISION_VISIBILITY, "false"); assertThat(underTest.isProjectVisibilitySynchronizationActivated()).isFalse(); } + @Test public void isProjectVisibilitySynchronizationActivated_whenPropertyIsSetToTrue_returnsTrue() { settings.setProperty(PROVISION_VISIBILITY, "true"); @@ -169,20 +171,26 @@ public class GitHubSettingsTest { @Test public void setProvisioning_whenPassedFalse_delegatesToInternalPropertiesWriteAndCleansUpExternalGroups() { - GroupDto groupDto = createGithubManagedGroup(); + createGithubManagedGroup(); + createGitHubOrganizationGroup(); underTest.setProvisioning(false); verify(internalProperties).write(GitHubSettings.PROVISIONING, Boolean.FALSE.toString()); - assertThat(db.getDbClient().externalGroupDao().selectByGroupUuid(db.getSession(), groupDto.getUuid())).isEmpty(); + assertThat(db.getDbClient().externalGroupDao().selectByIdentityProvider(db.getSession(), GitHubIdentityProvider.KEY)).isEmpty(); + assertThat(db.getDbClient().githubOrganizationGroupDao().findAll(db.getSession())).isEmpty(); } - private GroupDto createGithubManagedGroup() { + private void createGithubManagedGroup() { GroupDto groupDto = db.users().insertGroup(); db.users().markGroupAsGithubManaged(groupDto.getUuid()); - return groupDto; } + private void createGitHubOrganizationGroup() { + GroupDto groupDto = db.users().insertGroup(); + db.getDbClient().githubOrganizationGroupDao().insert(db.getSession(), new GithubOrganizationGroupDto(groupDto.getUuid(), "org1")); + db.commit(); + } @Test public void return_client_id() { diff --git a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java index 5a50ee8980d..03093f98104 100644 --- a/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java +++ b/server/sonar-auth-github/src/main/java/org/sonar/auth/github/GitHubSettings.java @@ -147,6 +147,7 @@ public class GitHubSettings implements DevOpsPlatformSettings { private void removeExternalGroupsForGithub() { try (DbSession dbSession = dbClient.openSession(false)) { dbClient.externalGroupDao().deleteByExternalIdentityProvider(dbSession, GitHubIdentityProvider.KEY); + dbClient.githubOrganizationGroupDao().deleteAll(dbSession); dbSession.commit(); } } diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubOrganizationGroupDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubOrganizationGroupDaoIT.java index 4502fe32c5d..96c77a8782d 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubOrganizationGroupDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/provisioning/GithubOrganizationGroupDaoIT.java @@ -84,8 +84,7 @@ public class GithubOrganizationGroupDaoIT { assertThat(all).hasSize(2) .containsExactlyInAnyOrder( new GithubOrganizationGroupDto(githubOrganizationGroupDto.groupUuid(), githubOrganizationGroupDto.organizationName(), groupDto.getName()), - new GithubOrganizationGroupDto(githubOrganizationGroupDto2.groupUuid(), githubOrganizationGroupDto2.organizationName(), groupDto2.getName()) - ); + new GithubOrganizationGroupDto(githubOrganizationGroupDto2.groupUuid(), githubOrganizationGroupDto2.organizationName(), groupDto2.getName())); } @Test @@ -103,6 +102,21 @@ public class GithubOrganizationGroupDaoIT { assertThat(underTest.selectByGroupUuid(dbSession, githubOrganizationGroupDto2.groupUuid())).isPresent(); } + @Test + public void deleteAll_shouldDeleteEverything() { + GroupDto groupDto = insertGroup(GROUP_UUID); + GithubOrganizationGroupDto githubOrganizationGroupDto = createGithubOrganizationGroupDto(groupDto.getUuid(), ORG_NAME); + underTest.insert(dbSession, githubOrganizationGroupDto); + + GroupDto groupDto2 = insertGroup(GROUP_UUID + "2"); + GithubOrganizationGroupDto githubOrganizationGroupDto2 = createGithubOrganizationGroupDto(groupDto2.getUuid(), ORG_NAME + "2"); + underTest.insert(dbSession, githubOrganizationGroupDto2); + + underTest.deleteAll(dbSession); + + assertThat(underTest.findAll(dbSession)).isEmpty(); + } + private GroupDto insertGroup(String groupUuid) { GroupDto group = new GroupDto(); group.setUuid(groupUuid); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupDao.java index 4c9d13cbf42..ae4d699e2ae 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupDao.java @@ -29,15 +29,23 @@ public class GithubOrganizationGroupDao implements Dao { public Set findAll(DbSession dbSession) { return mapper(dbSession).selectAll(); } + public void insert(DbSession dbSession, GithubOrganizationGroupDto githubOrganizationGroupDto) { mapper(dbSession).insert(githubOrganizationGroupDto); } + public Optional selectByGroupUuid(DbSession dbSession, String groupUuid) { return mapper(dbSession).selectByGroupUuid(groupUuid); } + public void deleteByGroupUuid(DbSession dbSession, String groupUuid) { mapper(dbSession).deleteByGroupUuid(groupUuid); } + + public void deleteAll(DbSession dbSession) { + mapper(dbSession).deleteAll(); + } + private static GithubOrganizationGroupMapper mapper(DbSession session) { return session.getMapper(GithubOrganizationGroupMapper.class); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupMapper.java index 0392f2f76ae..af47b2ed28e 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/provisioning/GithubOrganizationGroupMapper.java @@ -33,4 +33,6 @@ public interface GithubOrganizationGroupMapper { void deleteByGroupUuid(@Param("groupUuid") String groupUuid); + void deleteAll(); + } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubOrganizationGroupMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubOrganizationGroupMapper.xml index e5a98b3c2ba..ec6bbd91bf1 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubOrganizationGroupMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/provisioning/GithubOrganizationGroupMapper.xml @@ -40,4 +40,8 @@ delete from github_orgs_groups where group_uuid = #{groupUuid, jdbcType=VARCHAR} + + delete from github_orgs_groups + + -- 2.39.5