From 8ca93afc90cd2d3ccd149f270b6b887cb1668d2a Mon Sep 17 00:00:00 2001 From: Antoine Vigneau Date: Wed, 13 Dec 2023 14:20:30 +0100 Subject: [PATCH] SONAR-21244 Sync groups in Gitlab provisioning --- .../gitlab/GitlabApplicationClientTest.java | 1 - .../java/org/sonar/auth/gitlab/GsonGroup.java | 14 +-- .../org/sonar/auth/gitlab/GsonGroupTest.java | 1 - .../usergroups/ws/ExternalGroupServiceIT.java | 107 ------------------ .../usergroups/ws/ExternalGroupService.java | 83 -------------- 5 files changed, 3 insertions(+), 203 deletions(-) delete mode 100644 server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/ExternalGroupServiceIT.java delete mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/ExternalGroupService.java diff --git a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabApplicationClientTest.java b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabApplicationClientTest.java index 8311abf26f7..52177179879 100644 --- a/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabApplicationClientTest.java +++ b/server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabApplicationClientTest.java @@ -579,7 +579,6 @@ public class GitlabApplicationClientTest { GsonGroup gsonGroup = mock(GsonGroup.class); when(gsonGroup.getId()).thenReturn(number); when(gsonGroup.getFullPath()).thenReturn(fullPath); - when(gsonGroup.getDescription()).thenReturn(description); return gsonGroup; } diff --git a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java index 2fe325c2ab4..75dfaf80b43 100644 --- a/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java +++ b/server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java @@ -35,18 +35,15 @@ public class GsonGroup { private String id; @SerializedName("full_path") private String fullPath; - @SerializedName("description") - private String description; public GsonGroup() { // http://stackoverflow.com/a/18645370/229031 - this("", "", ""); + this("", ""); } - private GsonGroup(String id, String fullPath, String description) { + private GsonGroup(String id, String fullPath) { this.id = id; this.fullPath = fullPath; - this.description = description; } public String getId() { @@ -57,13 +54,8 @@ public class GsonGroup { return fullPath; } - public String getDescription() { - return description; - } - static List parse(String json) { - Type collectionType = new TypeToken>() { - }.getType(); + Type collectionType = new TypeToken>() {}.getType(); Gson gson = new Gson(); return gson.fromJson(json, collectionType); } diff --git a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GsonGroupTest.java b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GsonGroupTest.java index b72f834c16e..6c354d65070 100644 --- a/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GsonGroupTest.java +++ b/server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GsonGroupTest.java @@ -49,6 +49,5 @@ public class GsonGroupTest { assertThat(groups.size()).isOne(); assertThat(groups.get(0).getId()).isEqualTo("123456789"); assertThat(groups.get(0).getFullPath()).isEqualTo("my-awesome-group/my-project"); - assertThat(groups.get(0).getDescription()).isEqualTo("toto"); } } diff --git a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/ExternalGroupServiceIT.java b/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/ExternalGroupServiceIT.java deleted file mode 100644 index 0a050d70027..00000000000 --- a/server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/ExternalGroupServiceIT.java +++ /dev/null @@ -1,107 +0,0 @@ -/* - * 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.usergroups.ws; - -import java.util.Optional; -import org.junit.Before; -import org.junit.Rule; -import org.junit.Test; -import org.sonar.api.utils.System2; -import org.sonar.core.util.UuidFactoryFast; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.DbTester; -import org.sonar.db.user.ExternalGroupDto; -import org.sonar.db.user.GroupDto; -import org.sonar.server.common.group.service.GroupService; -import org.sonar.server.management.ManagedInstanceService; -import org.sonar.server.usergroups.DefaultGroupFinder; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; - -public class ExternalGroupServiceIT { - - private static final String GROUP_NAME = "GROUP_NAME"; - private static final String EXTERNAL_ID = "EXTERNAL_ID"; - private static final String EXTERNAL_IDENTITY_PROVIDER = "EXTERNAL_IDENTITY_PROVIDER"; - - @Rule - public DbTester dbTester = DbTester.create(System2.INSTANCE); - private final DbClient dbClient = dbTester.getDbClient(); - private final DbSession dbSession = dbTester.getSession(); - - private final DefaultGroupFinder defaultGroupFinder = new DefaultGroupFinder(dbClient); - - private final ManagedInstanceService managedInstanceService = mock(); - private final GroupService groupService = new GroupService(dbClient, UuidFactoryFast.getInstance(), defaultGroupFinder, managedInstanceService); - - private final ExternalGroupService externalGroupService = new ExternalGroupService(dbClient, groupService); - - @Before - public void setUp() { - dbTester.users().insertDefaultGroup(); - } - - @Test - public void createOrUpdateExternalGroup_whenNewGroup_shouldCreateIt() { - externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, GROUP_NAME)); - - assertGroupAndExternalGroup(); - } - - @Test - public void createOrUpdateExternalGroup_whenExistingLocalGroup_shouldMatchAndMakeItExternal() { - dbTester.users().insertGroup(GROUP_NAME); - - externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, GROUP_NAME)); - - assertThat(dbTester.users().countAllGroups()).isEqualTo(2); - assertGroupAndExternalGroup(); - } - - @Test - public void createOrUpdateExternalGroup_whenExistingExternalGroup_shouldUpdate() { - GroupDto existingGroupDto = dbTester.users().insertGroup(GROUP_NAME); - dbTester.users().insertExternalGroup(new ExternalGroupDto(existingGroupDto.getUuid(), EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER)); - - String updatedGroupName = "updated_" + GROUP_NAME; - externalGroupService.createOrUpdateExternalGroup(dbSession, new GroupRegistration(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER, updatedGroupName)); - - Optional groupDto = dbTester.users().selectGroup(updatedGroupName); - assertThat(groupDto) - .isPresent().get() - .extracting(GroupDto::getName) - .isEqualTo(updatedGroupName); - } - - private void assertGroupAndExternalGroup() { - Optional groupDto = dbTester.users().selectGroup(GROUP_NAME); - assertThat(groupDto) - .isPresent().get() - .extracting(GroupDto::getName).isEqualTo(GROUP_NAME); - - assertThat((dbTester.users().selectExternalGroupByGroupUuid(groupDto.get().getUuid()))) - .isPresent().get() - .extracting(ExternalGroupDto::externalId, ExternalGroupDto::externalIdentityProvider) - .containsExactly(EXTERNAL_ID, EXTERNAL_IDENTITY_PROVIDER); - } - -} diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/ExternalGroupService.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/ExternalGroupService.java deleted file mode 100644 index 14249798ad3..00000000000 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/ExternalGroupService.java +++ /dev/null @@ -1,83 +0,0 @@ -/* - * 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.usergroups.ws; - -import java.util.Optional; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; -import org.sonar.db.DbClient; -import org.sonar.db.DbSession; -import org.sonar.db.user.ExternalGroupDto; -import org.sonar.db.user.GroupDto; -import org.sonar.server.common.group.service.GroupService; - -public class ExternalGroupService { - - private static final Logger LOG = LoggerFactory.getLogger(ExternalGroupService.class); - - private final DbClient dbClient; - private final GroupService groupService; - - public ExternalGroupService(DbClient dbClient, GroupService groupService) { - this.dbClient = dbClient; - this.groupService = groupService; - } - - public void createOrUpdateExternalGroup(DbSession dbSession, GroupRegistration groupRegistration) { - Optional groupDto = retrieveGroupByExternalInformation(dbSession, groupRegistration); - if (groupDto.isPresent()) { - updateExternalGroup(dbSession, groupDto.get(), groupRegistration.name()); - } else { - createOrMatchExistingLocalGroup(dbSession, groupRegistration); - } - } - - private Optional retrieveGroupByExternalInformation(DbSession dbSession, GroupRegistration groupRegistration) { - Optional externalGroupDto = dbClient.externalGroupDao().selectByExternalIdAndIdentityProvider(dbSession, groupRegistration.externalId(), - groupRegistration.externalIdentityProvider()); - return externalGroupDto.flatMap(existingExternalGroupDto -> Optional.ofNullable(dbClient.groupDao().selectByUuid(dbSession, existingExternalGroupDto.groupUuid()))); - } - - private void updateExternalGroup(DbSession dbSession, GroupDto groupDto, String newName) { - LOG.debug("Updating external group: {} with new name {}", groupDto.getName(), newName); - groupService.updateGroup(dbSession, groupDto, newName); - } - - private void createOrMatchExistingLocalGroup(DbSession dbSession, GroupRegistration groupRegistration) { - GroupDto groupDto = findOrCreateLocalGroup(dbSession, groupRegistration); - createExternalGroup(dbSession, groupDto.getUuid(), groupRegistration); - } - - private GroupDto findOrCreateLocalGroup(DbSession dbSession, GroupRegistration groupRegistration) { - Optional groupDto = groupService.findGroup(dbSession, groupRegistration.name()); - if (groupDto.isPresent()) { - LOG.debug("Marking existing local group {} as managed group.", groupDto.get().getName()); - return groupDto.get(); - } else { - LOG.debug("Creating new group {}", groupRegistration.name()); - return groupService.createGroup(dbSession, groupRegistration.name(), null).groupDto(); - } - } - - private void createExternalGroup(DbSession dbSession, String groupUuid, GroupRegistration groupRegistration) { - dbClient.externalGroupDao().insert(dbSession, new ExternalGroupDto(groupUuid, groupRegistration.externalId(), groupRegistration.externalIdentityProvider())); - } - -} -- 2.39.5