From 5c7946ee113486391b8944b64329a694ae66db4a Mon Sep 17 00:00:00 2001 From: Aurelien Poscia Date: Wed, 16 Aug 2023 09:28:33 +0200 Subject: [PATCH] SONAR-19783 Adapt synchronization steps to handle default organization group --- .../GithubOrganizationGroupDaoIT.java | 19 +++++++++++++++++++ .../org/sonar/db/user/ExternalGroupDaoIT.java | 8 ++++++-- .../org/sonar/db/user/ExternalGroupDao.java | 7 ++++++- .../java/org/sonar/db/user/UserDbTester.java | 3 ++- ...ateGithubOrganizationsGroupsTableTest.java | 19 +++++++++++++++++++ 5 files changed, 52 insertions(+), 4 deletions(-) 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 7cc9edb221a..4502fe32c5d 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 @@ -1,3 +1,22 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.sonar.db.provisioning; import java.util.Set; diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java index 57c31bc2ccf..a1a93e24fbd 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/user/ExternalGroupDaoIT.java @@ -119,13 +119,17 @@ public class ExternalGroupDaoIT { @Test public void getManagedGroupsSqlFilter_whenFilterByManagedIsTrue_returnsCorrectQuery() { String filterManagedUser = underTest.getManagedGroupSqlFilter(true); - assertThat(filterManagedUser).isEqualTo(" exists (select group_uuid from external_groups eg where eg.group_uuid = uuid)"); + assertThat(filterManagedUser).isEqualTo( + "(exists (select group_uuid from external_groups eg where eg.group_uuid = uuid) " + + "or exists (select group_uuid from github_orgs_groups gog eg where gog.group_uuid = uuid))"); } @Test public void getManagedGroupsSqlFilter_whenFilterByManagedIsFalse_returnsCorrectQuery() { String filterNonManagedUser = underTest.getManagedGroupSqlFilter(false); - assertThat(filterNonManagedUser).isEqualTo("not exists (select group_uuid from external_groups eg where eg.group_uuid = uuid)"); + assertThat(filterNonManagedUser).isEqualTo( + "(not exists (select group_uuid from external_groups eg where eg.group_uuid = uuid) " + + "and not exists (select group_uuid from github_orgs_groups gog eg where gog.group_uuid = uuid))"); } private List createAndInsertExternalGroupDtos(String provider, int numberOfGroups) { diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java index 748260d9ae1..4275912b1b5 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ExternalGroupDao.java @@ -51,7 +51,12 @@ public class ExternalGroupDao implements Dao { } public String getManagedGroupSqlFilter(boolean filterByManaged) { - return String.format("%s exists (select group_uuid from external_groups eg where eg.group_uuid = uuid)", filterByManaged ? "" : "not"); + if (filterByManaged) { + return "(exists (select group_uuid from external_groups eg where eg.group_uuid = uuid) " + + "or exists (select group_uuid from github_orgs_groups gog eg where gog.group_uuid = uuid))"; + } + return "(not exists (select group_uuid from external_groups eg where eg.group_uuid = uuid) " + + "and not exists (select group_uuid from github_orgs_groups gog eg where gog.group_uuid = uuid))"; } public void deleteByExternalIdentityProvider(DbSession dbSession, String externalIdentityProvider) { diff --git a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java index 369975176f5..db69a16773f 100644 --- a/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java +++ b/server/sonar-db-dao/src/testFixtures/java/org/sonar/db/user/UserDbTester.java @@ -166,10 +166,11 @@ public class UserDbTester { db.commit(); } - public void markGroupAsGithubOrganizationGroup(String groupUuid, String organizationName) { + public GithubOrganizationGroupDto markGroupAsGithubOrganizationGroup(String groupUuid, String organizationName) { GithubOrganizationGroupDto githubOrganizationGroupDto = new GithubOrganizationGroupDto(groupUuid, organizationName); db.getDbClient().githubOrganizationGroupDao().insert(db.getSession(), githubOrganizationGroupDto); db.commit(); + return githubOrganizationGroupDto; } public ExternalGroupDto insertExternalGroup(ExternalGroupDto dto) { diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateGithubOrganizationsGroupsTableTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateGithubOrganizationsGroupsTableTest.java index dc44a594654..e4e900d43c7 100644 --- a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateGithubOrganizationsGroupsTableTest.java +++ b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v102/CreateGithubOrganizationsGroupsTableTest.java @@ -1,3 +1,22 @@ +/* + * SonarQube + * Copyright (C) 2009-2023 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ package org.sonar.server.platform.db.migration.version.v102; import java.sql.SQLException; -- 2.39.5