use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.
the class NotificationManager method notifyIssue.
/**
* Notify users that a new issue was created for a given entity. Will notify mentioned users as well.
*
* @param issue an issue to notify about
* @param entity an entity for wich issue was created
*/
@Transactional(propagation = Propagation.REQUIRED)
public void notifyIssue(Issue issue, AbstractSecuredEntity entity, String htmlText) {
NotificationSettings newIssueSettings = notificationSettingsManager.load(NotificationType.NEW_ISSUE);
if (newIssueSettings == null || !newIssueSettings.isEnabled()) {
LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_NOTIFICATION_TEMPLATE_NOT_CONFIGURED, "new issue"));
return;
}
NotificationMessage message = new NotificationMessage();
message.setTemplate(new NotificationTemplate(newIssueSettings.getTemplateId()));
message.setCopyUserIds(getMentionedUsers(issue.getText()));
Issue copyWithHtml = issue.toBuilder().text(htmlText).build();
message.setTemplateParameters(jsonMapper.convertValue(copyWithHtml, new TypeReference<Map<String, Object>>() {
}));
if (newIssueSettings.isKeepInformedOwner()) {
PipelineUser owner = userManager.loadUserByName(entity.getOwner());
message.setToUserId(owner.getId());
}
monitoringNotificationDao.createMonitoringNotification(message);
}
use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.
the class NotificationManager method notifyRunStatusChanged.
@Transactional(propagation = Propagation.REQUIRED)
public void notifyRunStatusChanged(PipelineRun pipelineRun) {
NotificationSettings runStatusSettings = notificationSettingsManager.load(NotificationType.PIPELINE_RUN_STATUS);
if (runStatusSettings == null || !runStatusSettings.isEnabled()) {
LOGGER.info("No template configured for pipeline run status changes notifications or it was disabled!");
return;
}
NotificationMessage message = new NotificationMessage();
message.setTemplate(new NotificationTemplate(runStatusSettings.getTemplateId()));
message.setTemplateParameters(PipelineRunMapper.map(pipelineRun, null));
List<Long> ccUserIds = getKeepInformedUserIds(runStatusSettings);
if (runStatusSettings.isKeepInformedAdmins()) {
ExtendedRole extendedRole = roleManager.loadRoleWithUsers(DefaultRoles.ROLE_ADMIN.getId());
ccUserIds.addAll(extendedRole.getUsers().stream().map(PipelineUser::getId).collect(Collectors.toList()));
}
message.setCopyUserIds(ccUserIds);
if (runStatusSettings.isKeepInformedOwner()) {
PipelineUser pipelineOwner = userManager.loadUserByName(pipelineRun.getOwner());
message.setToUserId(pipelineOwner.getId());
}
monitoringNotificationDao.createMonitoringNotification(message);
}
use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.
the class MonitoringNotificationDaoTest method testCreateMonitoringNotifications.
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testCreateMonitoringNotifications() {
NotificationTemplate notificationTemplate = notificationTemplateDao.createNotificationTemplate(template);
PipelineUser pipelineUser = userDao.loadUserByName(TEST_USER1);
List<NotificationMessage> messages = IntStream.range(0, 5).mapToObj(i -> {
NotificationMessage notificationMessage = new NotificationMessage();
notificationMessage.setTemplate(notificationTemplate);
notificationMessage.setToUserId(pipelineUser.getId());
notificationMessage.setCopyUserIds(Collections.singletonList(pipelineUser.getId()));
notificationMessage.setTemplateParameters(Collections.singletonMap("test", i));
return notificationMessage;
}).collect(Collectors.toList());
monitoringNotificationDao.createMonitoringNotifications(messages);
List<NotificationMessage> loaded = monitoringNotificationDao.loadAllNotifications();
assertTrue(messages.size() <= loaded.size());
assertTrue(loaded.stream().allMatch(l -> messages.stream().anyMatch(m -> m.getTemplateParameters().equals(l.getTemplateParameters()))));
}
use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.
the class MonitoringNotificationDaoTest method testLoadMonitoringNotification.
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testLoadMonitoringNotification() {
PipelineRun run1 = createTestPipelineRun();
List<PipelineRun> pipelineRuns = pipelineRunDao.loadPipelineRuns(Collections.singletonList(run1.getId()));
NotificationTemplate notificationTemplate = notificationTemplateDao.createNotificationTemplate(template);
notificationMessage = new NotificationMessage();
notificationMessage.setTemplate(notificationTemplate);
PipelineUser pipelineUser = userDao.loadUserByName(TEST_USER1);
notificationMessage.setToUserId(pipelineUser.getId());
notificationMessage.setCopyUserIds(Collections.singletonList(pipelineUser.getId()));
notificationMessage.setTemplateParameters(PipelineRunMapper.map(pipelineRuns.get(0), TEST_THRESHOLD));
monitoringNotificationDao.createMonitoringNotification(notificationMessage);
NotificationMessage actualMessage = monitoringNotificationDao.loadMonitoringNotification(notificationMessage.getId());
assertEquals(notificationMessage.getId(), actualMessage.getId());
assertEquals(notificationMessage.getTemplate().getId(), actualMessage.getTemplate().getId());
assertEquals(user.getId(), notificationMessage.getToUserId());
assertFalse(notificationMessage.getCopyUserIds().isEmpty());
notificationMessage.getTemplateParameters().values().removeIf(Objects::isNull);
notificationMessage.getTemplateParameters().forEach((k, v) -> {
assertTrue(actualMessage.getTemplateParameters().containsKey(k));
assertEquals(v.toString(), actualMessage.getTemplateParameters().get(k).toString());
});
}
use of com.epam.pipeline.entity.notification.NotificationMessage in project cloud-pipeline by epam.
the class MonitoringNotificationDaoTest method testCreateAndLoadCustomMonitoringNotifications.
@Test
@Transactional(propagation = Propagation.REQUIRES_NEW)
public void testCreateAndLoadCustomMonitoringNotifications() {
PipelineUser pipelineUser = userDao.loadUserByName(TEST_USER1);
NotificationMessage message = new NotificationMessage();
message.setBody(BODY_STRING);
message.setSubject(SUBJECT_STRING);
message.setToUserId(pipelineUser.getId());
message.setCopyUserIds(Collections.emptyList());
message.setTemplateParameters(Collections.singletonMap(TEMPLATE_PARAMETER, TEMPLATE_PARAMETER_VALUE));
monitoringNotificationDao.createMonitoringNotification(message);
NotificationMessage singleLoadedMessage = monitoringNotificationDao.loadMonitoringNotification(message.getId());
assertEquals(singleLoadedMessage.getBody(), BODY_STRING);
assertEquals(singleLoadedMessage.getSubject(), SUBJECT_STRING);
assertEquals(singleLoadedMessage.getToUserId(), pipelineUser.getId());
assertEquals(singleLoadedMessage.getCopyUserIds(), Collections.emptyList());
assertNull(singleLoadedMessage.getTemplate());
NotificationMessage batchLoadedMessage = monitoringNotificationDao.loadAllNotifications().stream().filter(m -> m.getId().equals(message.getId())).findFirst().get();
assertEquals(singleLoadedMessage, batchLoadedMessage);
}
Aggregations