From 15727e6c67bb83b5230df53b72055daad1e78d86 Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Wed, 10 May 2023 10:37:54 +0200 Subject: [PATCH] SONAR-19085 Add debug logs when syncing groups. --- .../usergroups/ws/ExternalGroupService.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) 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 index 65ead0b66d5..dc5befd6bb2 100644 --- 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 @@ -20,6 +20,8 @@ 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; @@ -27,6 +29,8 @@ import org.sonar.db.user.GroupDto; public class ExternalGroupService { + private static final Logger LOG = LoggerFactory.getLogger(ExternalGroupService.class); + private final DbClient dbClient; private final GroupService groupService; @@ -36,7 +40,7 @@ public class ExternalGroupService { } public void createOrUpdateExternalGroup(DbSession dbSession, GroupRegistration groupRegistration) { - Optional groupDto = retrieveGroupByItsExternalInformation(dbSession, groupRegistration); + Optional groupDto = retrieveGroupByExternalInformation(dbSession, groupRegistration); if (groupDto.isPresent()) { updateExternalGroup(dbSession, groupDto.get(), groupRegistration.name()); } else { @@ -44,13 +48,14 @@ public class ExternalGroupService { } } - private Optional retrieveGroupByItsExternalInformation(DbSession dbSession, GroupRegistration groupRegistration) { - Optional externalGroupDto = - dbClient.externalGroupDao().selectByExternalIdAndIdentityProvider(dbSession, groupRegistration.externalId(), groupRegistration.externalIdentityProvider()); + 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); } @@ -61,7 +66,13 @@ public class ExternalGroupService { private GroupDto findOrCreateLocalGroup(DbSession dbSession, GroupRegistration groupRegistration) { Optional groupDto = groupService.findGroup(dbSession, groupRegistration.name()); - return groupDto.orElseGet(() -> groupService.createGroup(dbSession, groupRegistration.name(), null)); + 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); + } } private void createExternalGroup(DbSession dbSession, String groupUuid, GroupRegistration groupRegistration) { -- 2.39.5