]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-21244 Sync groups in Gitlab provisioning
authorAntoine Vigneau <antoine.vigneau@sonarsource.com>
Wed, 13 Dec 2023 13:20:30 +0000 (14:20 +0100)
committersonartech <sonartech@sonarsource.com>
Fri, 22 Dec 2023 20:03:02 +0000 (20:03 +0000)
server/sonar-alm-client/src/test/java/org/sonar/alm/client/gitlab/GitlabApplicationClientTest.java
server/sonar-auth-gitlab/src/main/java/org/sonar/auth/gitlab/GsonGroup.java
server/sonar-auth-gitlab/src/test/java/org/sonar/auth/gitlab/GsonGroupTest.java
server/sonar-webserver-webapi/src/it/java/org/sonar/server/usergroups/ws/ExternalGroupServiceIT.java [deleted file]
server/sonar-webserver-webapi/src/main/java/org/sonar/server/usergroups/ws/ExternalGroupService.java [deleted file]

index 8311abf26f7b933bb1a6d3c92d22e904f7ef7b4f..52177179879f1bb335e2a9302824df4cdbf8bf50 100644 (file)
@@ -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;
   }
 
index 2fe325c2ab4de80c96c50472a989c797f1c6c523..75dfaf80b434ddf1310c58d9287f2f098d78f1bd 100644 (file)
@@ -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<GsonGroup> parse(String json) {
-    Type collectionType = new TypeToken<Collection<GsonGroup>>() {
-    }.getType();
+    Type collectionType = new TypeToken<Collection<GsonGroup>>() {}.getType();
     Gson gson = new Gson();
     return gson.fromJson(json, collectionType);
   }
index b72f834c16eb5c711d2049ed5f8a519700965ac6..6c354d650701267ed3746bccbd75e7d75322de43 100644 (file)
@@ -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 (file)
index 0a050d7..0000000
+++ /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> groupDto = dbTester.users().selectGroup(updatedGroupName);
-    assertThat(groupDto)
-      .isPresent().get()
-      .extracting(GroupDto::getName)
-      .isEqualTo(updatedGroupName);
-  }
-
-  private void assertGroupAndExternalGroup() {
-    Optional<GroupDto> 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 (file)
index 1424979..0000000
+++ /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> groupDto = retrieveGroupByExternalInformation(dbSession, groupRegistration);
-    if (groupDto.isPresent()) {
-      updateExternalGroup(dbSession, groupDto.get(), groupRegistration.name());
-    } else {
-      createOrMatchExistingLocalGroup(dbSession, groupRegistration);
-    }
-  }
-
-  private Optional<GroupDto> retrieveGroupByExternalInformation(DbSession dbSession, GroupRegistration groupRegistration) {
-    Optional<ExternalGroupDto> 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> 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()));
-  }
-
-}