diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2024-11-22 17:06:53 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-11-26 20:02:51 +0000 |
commit | 7137379d9a849403ce1079a90ed1e152cf56f597 (patch) | |
tree | 82d444210e95596f5b8a8e4d2daa21be1bae606b /server/sonar-server-common/src | |
parent | 76635fedaa16506415ebe436ad1dd5d93083ea7a (diff) | |
download | sonarqube-7137379d9a849403ce1079a90ed1e152cf56f597.tar.gz sonarqube-7137379d9a849403ce1079a90ed1e152cf56f597.zip |
SONAR-23649 Conditions mismatch notification should be enabled by default for Quality gate administrators
Diffstat (limited to 'server/sonar-server-common/src')
5 files changed, 35 insertions, 25 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandler.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandler.java index 8508662fddb..ffd0bae69fe 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandler.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandler.java @@ -25,6 +25,7 @@ import java.util.Set; import org.sonar.db.DbClient; import org.sonar.db.DbSession; import org.sonar.db.EmailSubscriberDto; +import org.sonar.db.permission.GlobalPermission; import org.sonar.server.notification.EmailNotificationHandler; import org.sonar.server.notification.NotificationDispatcherMetadata; import org.sonar.server.notification.email.EmailNotificationChannel; @@ -35,7 +36,9 @@ public class QualityGateMetricsUpdateNotificationHandler extends EmailNotificati static final String KEY = "QualityGateConditionsMismatch"; private static final NotificationDispatcherMetadata METADATA = NotificationDispatcherMetadata.create(KEY) .setProperty(NotificationDispatcherMetadata.GLOBAL_NOTIFICATION, String.valueOf(true)) - .setProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION, String.valueOf(false)); + .setProperty(NotificationDispatcherMetadata.PER_PROJECT_NOTIFICATION, String.valueOf(false)) + .setProperty(NotificationDispatcherMetadata.ENABLED_BY_DEFAULT_NOTIFICATION, String.valueOf(true)) + .setProperty(NotificationDispatcherMetadata.PERMISSION_RESTRICTION, GlobalPermission.ADMINISTER_QUALITY_GATES.getKey()); private final DbClient dbClient; @@ -48,21 +51,24 @@ public class QualityGateMetricsUpdateNotificationHandler extends EmailNotificati protected Set<EmailNotificationChannel.EmailDeliveryRequest> toEmailDeliveryRequests(Collection<QualityGateMetricsUpdateNotification> notifications) { try (DbSession session = dbClient.openSession(false)) { - Set<String> logins = dbClient.authorizationDao() - .selectQualityGateAdministratorLogins(session).stream() - .map(EmailSubscriberDto::getLogin) - .collect(toSet()); + Set<EmailSubscriberDto> subscriptions = dbClient.authorizationDao() + .selectQualityGateAdministratorLogins(session); - if (logins.isEmpty()) { + if (subscriptions.isEmpty()) { return Set.of(); } - Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification( - session, KEY, EmailNotificationChannel.class.getSimpleName(), null, logins); + Set<String> disabledLogins = dbClient.propertiesDao().findDisabledEmailSubscribersForNotification( + session, KEY, EmailNotificationChannel.class.getSimpleName(), null, + subscriptions.stream().map(EmailSubscriberDto::getLogin).collect(toSet())) + .stream() + .map(EmailSubscriberDto::getLogin) + .collect(toSet()); - return emailSubscribers + return subscriptions .stream() - .flatMap(t -> notifications.stream().map(notification -> new EmailNotificationChannel.EmailDeliveryRequest(t.getEmail(), notification))) + .filter(s -> !disabledLogins.contains(s.getLogin())) + .flatMap(s -> notifications.stream().map(notification -> new EmailNotificationChannel.EmailDeliveryRequest(s.getEmail(), notification))) .collect(toSet()); } } diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java index 191c2ff3eb2..50e4a7f9f76 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/DefaultNotificationManager.java @@ -115,7 +115,7 @@ public class DefaultNotificationManager implements NotificationManager { verifyProjectKey(projectKey); try (DbSession dbSession = dbClient.openSession(false)) { - Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification( + Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEnabledEmailSubscribersForNotification( dbSession, dispatcherKey, EmailNotificationChannel.class.getSimpleName(), projectKey); return keepAuthorizedEmailSubscribers(dbSession, projectKey, subscriberPermissionsOnProject, emailSubscribers); @@ -132,7 +132,7 @@ public class DefaultNotificationManager implements NotificationManager { } try (DbSession dbSession = dbClient.openSession(false)) { - Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification( + Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEnabledEmailSubscribersForNotification( dbSession, dispatcherKey, EmailNotificationChannel.class.getSimpleName(), projectKey, logins); return keepAuthorizedEmailSubscribers(dbSession, projectKey, subscriberPermissionsOnProject, emailSubscribers); diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationDispatcherMetadata.java b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationDispatcherMetadata.java index a087d349070..5bc5afe88ce 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationDispatcherMetadata.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/notification/NotificationDispatcherMetadata.java @@ -37,6 +37,8 @@ public final class NotificationDispatcherMetadata { public static final String GLOBAL_NOTIFICATION = "globalNotification"; public static final String PER_PROJECT_NOTIFICATION = "perProjectNotification"; + public static final String ENABLED_BY_DEFAULT_NOTIFICATION = "enabledByDefaultNotification"; + public static final String PERMISSION_RESTRICTION = "permissionRestriction"; private String dispatcherKey; private Map<String, String> properties; diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandlerTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandlerTest.java index bb691f638dd..305bb9b4aeb 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandlerTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandlerTest.java @@ -60,8 +60,10 @@ class QualityGateMetricsUpdateNotificationHandlerTest { when(authorizationDao.selectQualityGateAdministratorLogins(dbSession)) .thenReturn(Set.of(new EmailSubscriberDto().setLogin("login1").setEmail("email@email.com"), new EmailSubscriberDto().setLogin("login2").setEmail("email2@email.com"))); - when(propertiesDao.findEmailSubscribersForNotification(eq(dbSession), eq(QualityGateMetricsUpdateNotificationHandler.KEY), any(), isNull(), eq(Set.of("login1", "login2")))) - .thenReturn(Set.of(new EmailSubscriberDto().setLogin("login1").setEmail("email@email.com"), new EmailSubscriberDto().setLogin("login2").setEmail("email2@email.com"))); + when( + propertiesDao.findDisabledEmailSubscribersForNotification(eq(dbSession), eq(QualityGateMetricsUpdateNotificationHandler.KEY), any(), isNull(), + eq(Set.of("login1", "login2")))) + .thenReturn(Set.of()); Assertions.assertThat(underTest.toEmailDeliveryRequests(List.of(new QualityGateMetricsUpdateNotification(true)))) .extracting(EmailNotificationChannel.EmailDeliveryRequest::recipientEmail, EmailNotificationChannel.EmailDeliveryRequest::notification) @@ -70,12 +72,12 @@ class QualityGateMetricsUpdateNotificationHandlerTest { } @Test - void toEmailDeliveryRequests_whenHasAdminsButNotSubscribed_shouldNotSendExpectedNotification() { + void toEmailDeliveryRequests_whenHasAdminsButHasUnsubscribe_shouldNotSendExpectedNotification() { when(authorizationDao.selectQualityGateAdministratorLogins(dbSession)) .thenReturn(Set.of(new EmailSubscriberDto().setLogin("login1").setEmail("email@email.com"))); - when(propertiesDao.findEmailSubscribersForNotification(eq(dbSession), eq(QualityGateMetricsUpdateNotificationHandler.KEY), any(), isNull(), eq(Set.of("login1")))) - .thenReturn(Set.of()); + when(propertiesDao.findDisabledEmailSubscribersForNotification(eq(dbSession), eq(QualityGateMetricsUpdateNotificationHandler.KEY), any(), isNull(), eq(Set.of("login1")))) + .thenReturn(Set.of(new EmailSubscriberDto().setLogin("login1").setEmail("email@email.com"))); Assertions.assertThat(underTest.toEmailDeliveryRequests(List.of(new QualityGateMetricsUpdateNotification(true)))) .isEmpty(); diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/notification/DefaultNotificationManagerTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/notification/DefaultNotificationManagerTest.java index f9b0ebe070b..b687dc0e7cb 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/notification/DefaultNotificationManagerTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/notification/DefaultNotificationManagerTest.java @@ -178,7 +178,7 @@ public class DefaultNotificationManagerTest { String globalPermission = secure().nextAlphanumeric(4); String projectPermission = secure().nextAlphanumeric(5); String projectKey = secure().nextAlphabetic(6); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) .thenReturn(Collections.emptySet()); Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, @@ -197,7 +197,7 @@ public class DefaultNotificationManagerTest { Set<String> logins = IntStream.range(0, 1 + new Random().nextInt(10)) .mapToObj(i -> "login_" + i) .collect(Collectors.toSet()); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) .thenReturn(Collections.emptySet()); Set<EmailRecipient> emailRecipients = underTest.findSubscribedEmailRecipients(dispatcherKey, projectKey, logins, @@ -213,7 +213,7 @@ public class DefaultNotificationManagerTest { String globalPermission = secure().nextAlphanumeric(4); String projectPermission = secure().nextAlphanumeric(5); String projectKey = secure().nextAlphabetic(6); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) .thenReturn( newHashSet(EmailSubscriberDto.create("user1", false, "user1@foo"), EmailSubscriberDto.create("user3", false, "user3@foo"), EmailSubscriberDto.create("user3", true, "user3@foo"))); @@ -239,7 +239,7 @@ public class DefaultNotificationManagerTest { String projectPermission = secure().nextAlphanumeric(5); String projectKey = secure().nextAlphabetic(6); Set<String> logins = ImmutableSet.of("user1", "user2", "user3"); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) .thenReturn( newHashSet(EmailSubscriberDto.create("user1", false, "user1@foo"), EmailSubscriberDto.create("user3", false, "user3@foo"), EmailSubscriberDto.create("user3", true, "user3@foo"))); @@ -268,7 +268,7 @@ public class DefaultNotificationManagerTest { .mapToObj(i -> EmailSubscriberDto.create("user" + i, true, "user" + i + "@sonarsource.com")) .collect(Collectors.toSet()); Set<String> logins = subscribers.stream().map(EmailSubscriberDto::getLogin).collect(Collectors.toSet()); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) .thenReturn(subscribers); when(authorizationDao.keepAuthorizedLoginsOnEntity(dbSession, logins, projectKey, globalPermission)) .thenReturn(logins); @@ -293,7 +293,7 @@ public class DefaultNotificationManagerTest { .mapToObj(i -> EmailSubscriberDto.create("user" + i, true, "user" + i + "@sonarsource.com")) .collect(Collectors.toSet()); Set<String> logins = subscribers.stream().map(EmailSubscriberDto::getLogin).collect(Collectors.toSet()); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) .thenReturn(subscribers); when(authorizationDao.keepAuthorizedLoginsOnEntity(dbSession, logins, projectKey, globalPermission)) .thenReturn(logins); @@ -318,7 +318,7 @@ public class DefaultNotificationManagerTest { .mapToObj(i -> EmailSubscriberDto.create("user" + i, false, "user" + i + "@sonarsource.com")) .collect(Collectors.toSet()); Set<String> logins = subscribers.stream().map(EmailSubscriberDto::getLogin).collect(Collectors.toSet()); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey)) .thenReturn(subscribers); when(authorizationDao.keepAuthorizedLoginsOnEntity(dbSession, logins, projectKey, projectPermission)) .thenReturn(logins); @@ -343,7 +343,7 @@ public class DefaultNotificationManagerTest { .mapToObj(i -> EmailSubscriberDto.create("user" + i, false, "user" + i + "@sonarsource.com")) .collect(Collectors.toSet()); Set<String> logins = subscribers.stream().map(EmailSubscriberDto::getLogin).collect(Collectors.toSet()); - when(propertiesDao.findEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) + when(propertiesDao.findEnabledEmailSubscribersForNotification(dbSession, dispatcherKey, "EmailNotificationChannel", projectKey, logins)) .thenReturn(subscribers); when(authorizationDao.keepAuthorizedLoginsOnEntity(dbSession, logins, projectKey, projectPermission)) .thenReturn(logins); |