aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-server-common
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2024-11-20 17:55:57 +0100
committersonartech <sonartech@sonarsource.com>2024-11-26 20:02:50 +0000
commitbbadf94575df13966c7f28281160e88528464774 (patch)
treed33975e6b77d275b51366d6e38d9a11e361c1514 /server/sonar-server-common
parent8d96098e30837f5fb8ee7b614dfe39b9486bef26 (diff)
downloadsonarqube-bbadf94575df13966c7f28281160e88528464774.tar.gz
sonarqube-bbadf94575df13966c7f28281160e88528464774.zip
SONAR-23649 QG admins can opt out from conditions mismatch notification
Diffstat (limited to 'server/sonar-server-common')
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/NewModesNotificationsModule.java1
-rw-r--r--server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandler.java28
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewModesNotificationsModuleTest.java2
-rw-r--r--server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/QualityGateMetricsUpdateNotificationHandlerTest.java23
4 files changed, 49 insertions, 5 deletions
diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/NewModesNotificationsModule.java b/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/NewModesNotificationsModule.java
index 9c830e03af0..14ecc81a5f2 100644
--- a/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/NewModesNotificationsModule.java
+++ b/server/sonar-server-common/src/main/java/org/sonar/server/issue/notification/NewModesNotificationsModule.java
@@ -33,6 +33,7 @@ public class NewModesNotificationsModule extends Module {
QualityGateMetricsUpdateNotification.class,
QualityGateMetricsUpdateNotificationHandler.class,
+ QualityGateMetricsUpdateNotificationHandler.newMetadata(),
QualityGateMetricsUpdateTemplate.class);
}
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 81033f72762..8508662fddb 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
@@ -24,6 +24,7 @@ import java.util.Optional;
import java.util.Set;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
+import org.sonar.db.EmailSubscriberDto;
import org.sonar.server.notification.EmailNotificationHandler;
import org.sonar.server.notification.NotificationDispatcherMetadata;
import org.sonar.server.notification.email.EmailNotificationChannel;
@@ -31,6 +32,11 @@ import org.sonar.server.notification.email.EmailNotificationChannel;
import static java.util.stream.Collectors.toSet;
public class QualityGateMetricsUpdateNotificationHandler extends EmailNotificationHandler<QualityGateMetricsUpdateNotification> {
+ 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));
+
private final DbClient dbClient;
protected QualityGateMetricsUpdateNotificationHandler(DbClient dbClient, EmailNotificationChannel emailChannel) {
@@ -41,8 +47,20 @@ public class QualityGateMetricsUpdateNotificationHandler extends EmailNotificati
@Override
protected Set<EmailNotificationChannel.EmailDeliveryRequest> toEmailDeliveryRequests(Collection<QualityGateMetricsUpdateNotification> notifications) {
try (DbSession session = dbClient.openSession(false)) {
- return dbClient.authorizationDao()
- .selectQualityGateAdministratorLogins(session)
+
+ Set<String> logins = dbClient.authorizationDao()
+ .selectQualityGateAdministratorLogins(session).stream()
+ .map(EmailSubscriberDto::getLogin)
+ .collect(toSet());
+
+ if (logins.isEmpty()) {
+ return Set.of();
+ }
+
+ Set<EmailSubscriberDto> emailSubscribers = dbClient.propertiesDao().findEmailSubscribersForNotification(
+ session, KEY, EmailNotificationChannel.class.getSimpleName(), null, logins);
+
+ return emailSubscribers
.stream()
.flatMap(t -> notifications.stream().map(notification -> new EmailNotificationChannel.EmailDeliveryRequest(t.getEmail(), notification)))
.collect(toSet());
@@ -51,7 +69,11 @@ public class QualityGateMetricsUpdateNotificationHandler extends EmailNotificati
@Override
public Optional<NotificationDispatcherMetadata> getMetadata() {
- return Optional.empty();
+ return Optional.of(METADATA);
+ }
+
+ public static NotificationDispatcherMetadata newMetadata() {
+ return METADATA;
}
@Override
diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewModesNotificationsModuleTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewModesNotificationsModuleTest.java
index 4ac667ea5cc..b8fa479f8fd 100644
--- a/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewModesNotificationsModuleTest.java
+++ b/server/sonar-server-common/src/test/java/org/sonar/server/issue/notification/NewModesNotificationsModuleTest.java
@@ -30,6 +30,6 @@ class NewModesNotificationsModuleTest {
void configure_shouldReturnExpectedNumberOfComponents() {
ListContainer container = new ListContainer();
new NewModesNotificationsModule().configure(container);
- assertThat(container.getAddedObjects()).hasSize(7);
+ assertThat(container.getAddedObjects()).hasSize(8);
}
}
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 6dda1019c1a..bb691f638dd 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
@@ -28,9 +28,13 @@ import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.EmailSubscriberDto;
import org.sonar.db.permission.AuthorizationDao;
+import org.sonar.db.property.PropertiesDao;
import org.sonar.server.notification.email.EmailNotificationChannel;
import static org.assertj.core.groups.Tuple.tuple;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.isNull;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@@ -38,6 +42,7 @@ class QualityGateMetricsUpdateNotificationHandlerTest {
private final DbClient dbClient = mock(DbClient.class);
private final DbSession dbSession = mock(DbSession.class);
private final AuthorizationDao authorizationDao = mock(AuthorizationDao.class);
+ private final PropertiesDao propertiesDao = mock(PropertiesDao.class);
private final EmailNotificationChannel emailNotificationChannel = mock(EmailNotificationChannel.class);
private final QualityGateMetricsUpdateNotificationHandler underTest = new QualityGateMetricsUpdateNotificationHandler(dbClient,
@@ -47,12 +52,16 @@ class QualityGateMetricsUpdateNotificationHandlerTest {
public void wire_mocks() {
when(dbClient.openSession(false)).thenReturn(dbSession);
when(dbClient.authorizationDao()).thenReturn(authorizationDao);
+ when(dbClient.propertiesDao()).thenReturn(propertiesDao);
}
@Test
void toEmailDeliveryRequests_whenHasAdmins_shouldSendExpectedNotification() {
when(authorizationDao.selectQualityGateAdministratorLogins(dbSession))
- .thenReturn(Set.of(new EmailSubscriberDto().setEmail("email@email.com"), new EmailSubscriberDto().setEmail("email2@email.com")));
+ .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")));
Assertions.assertThat(underTest.toEmailDeliveryRequests(List.of(new QualityGateMetricsUpdateNotification(true))))
.extracting(EmailNotificationChannel.EmailDeliveryRequest::recipientEmail, EmailNotificationChannel.EmailDeliveryRequest::notification)
@@ -61,6 +70,18 @@ class QualityGateMetricsUpdateNotificationHandlerTest {
}
@Test
+ void toEmailDeliveryRequests_whenHasAdminsButNotSubscribed_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());
+
+ Assertions.assertThat(underTest.toEmailDeliveryRequests(List.of(new QualityGateMetricsUpdateNotification(true))))
+ .isEmpty();
+ }
+
+ @Test
void toEmailDeliveryRequests_whenHasNoAdmins_shouldNotSendNotification() {
when(authorizationDao.selectQualityGateAdministratorLogins(dbSession))
.thenReturn(Set.of());