Search in sources :

Example 6 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class NotificationManagerTest method testWontNotifyAdminsIfConfigured.

@Test
@Transactional(propagation = Propagation.REQUIRES_NEW, rollbackFor = Throwable.class)
public void testWontNotifyAdminsIfConfigured() {
    NotificationSettings settings = notificationSettingsDao.loadNotificationSettings(1L);
    settings.setKeepInformedAdmins(false);
    notificationSettingsDao.updateNotificationSettings(settings);
    notificationManager.notifyLongRunningTask(longRunnging, settings);
    List<NotificationMessage> messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertEquals(1, messages.size());
    Assert.assertEquals(admin.getId(), messages.get(0).getToUserId());
    Assert.assertTrue(messages.get(0).getCopyUserIds().isEmpty());
    Assert.assertEquals(longRunningTemplate.getId(), messages.get(0).getTemplate().getId());
    settings.setKeepInformedAdmins(false);
    settings.setInformedUserIds(Collections.singletonList(userDao.loadUserByName("admin").getId()));
    notificationSettingsDao.updateNotificationSettings(settings);
    notificationManager.notifyLongRunningTask(longRunnging, settings);
    messages = monitoringNotificationDao.loadAllNotifications();
    Assert.assertTrue(messages.get(messages.size() - 1).getCopyUserIds().contains(admin.getId()));
}
Also used : NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) AbstractManagerTest(com.epam.pipeline.manager.AbstractManagerTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 7 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class NotificationManager method notifyIssueComment.

@Transactional(propagation = Propagation.REQUIRED)
public void notifyIssueComment(IssueComment comment, Issue issue, String htmlText) {
    NotificationSettings newIssueCommentSettings = notificationSettingsManager.load(NotificationType.NEW_ISSUE_COMMENT);
    if (newIssueCommentSettings == null || !newIssueCommentSettings.isEnabled()) {
        LOGGER.info(messageHelper.getMessage(MessageConstants.INFO_NOTIFICATION_TEMPLATE_NOT_CONFIGURED, "new issue"));
        return;
    }
    NotificationMessage message = new NotificationMessage();
    message.setTemplate(new NotificationTemplate(newIssueCommentSettings.getTemplateId()));
    AbstractSecuredEntity entity = entityManager.load(issue.getEntity().getEntityClass(), issue.getEntity().getEntityId());
    List<PipelineUser> referencedUsers = userManager.loadUsersByNames(Arrays.asList(entity.getOwner(), issue.getAuthor()));
    List<Long> ccUserIds = getMentionedUsers(comment.getText());
    referencedUsers.stream().filter(u -> u.getUserName().equals(entity.getOwner())).findFirst().ifPresent(owner -> ccUserIds.add(owner.getId()));
    message.setCopyUserIds(ccUserIds);
    if (newIssueCommentSettings.isKeepInformedOwner()) {
        PipelineUser author = referencedUsers.stream().filter(u -> u.getUserName().equals(issue.getAuthor())).findFirst().orElseThrow(() -> new IllegalArgumentException("No issue author was found"));
        message.setToUserId(author.getId());
    }
    IssueComment copyWithHtml = comment.toBuilder().text(htmlText).build();
    Map<String, Object> commentParams = jsonMapper.convertValue(copyWithHtml, new TypeReference<Map<String, Object>>() {
    });
    commentParams.put("issue", jsonMapper.convertValue(issue, new TypeReference<Map<String, Object>>() {
    }));
    message.setTemplateParameters(commentParams);
    monitoringNotificationDao.createMonitoringNotification(message);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) AbstractSecuredEntity(com.epam.pipeline.entity.AbstractSecuredEntity) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) IssueComment(com.epam.pipeline.entity.issue.IssueComment) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Map(java.util.Map) Transactional(org.springframework.transaction.annotation.Transactional)

Example 8 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings 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);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) Issue(com.epam.pipeline.entity.issue.Issue) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) TypeReference(com.fasterxml.jackson.core.type.TypeReference) Transactional(org.springframework.transaction.annotation.Transactional)

Example 9 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings 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);
}
Also used : ExtendedRole(com.epam.pipeline.entity.user.ExtendedRole) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Transactional(org.springframework.transaction.annotation.Transactional)

Example 10 with NotificationSettings

use of com.epam.pipeline.entity.notification.NotificationSettings in project cloud-pipeline by epam.

the class NotificationAspectTest method setUp.

@Before
public void setUp() throws Exception {
    statusTemplate = new NotificationTemplate(5L);
    statusTemplate.setBody("///");
    statusTemplate.setSubject("//");
    statusTemplate.setName("testTemplate");
    notificationTemplateDao.createNotificationTemplate(statusTemplate);
    NotificationSettings settings = new NotificationSettings();
    settings.setType(NotificationSettings.NotificationType.PIPELINE_RUN_STATUS);
    settings.setKeepInformedAdmins(true);
    settings.setInformedUserIds(Collections.emptyList());
    settings.setTemplateId(statusTemplate.getId());
    settings.setThreshold(null);
    settings.setEnabled(true);
    settings.setResendDelay(null);
    settings.setKeepInformedOwner(true);
    notificationSettingsDao.createNotificationSettings(settings);
    testOwner = new PipelineUser("testOwner");
    userDao.createUser(testOwner, Collections.emptyList());
    pipeline = new Pipeline();
    pipeline.setName("TestPipeline");
    Mockito.when(pipelineManager.load(Mockito.anyLong())).thenReturn(pipeline);
}
Also used : PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationSettings(com.epam.pipeline.entity.notification.NotificationSettings) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Pipeline(com.epam.pipeline.entity.pipeline.Pipeline) Before(org.junit.Before)

Aggregations

NotificationSettings (com.epam.pipeline.entity.notification.NotificationSettings)13 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)8 Transactional (org.springframework.transaction.annotation.Transactional)8 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)5 PipelineUser (com.epam.pipeline.entity.user.PipelineUser)5 Test (org.junit.Test)4 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)3 TypeReference (com.fasterxml.jackson.core.type.TypeReference)3 AbstractSecuredEntity (com.epam.pipeline.entity.AbstractSecuredEntity)2 Issue (com.epam.pipeline.entity.issue.Issue)2 IssueComment (com.epam.pipeline.entity.issue.IssueComment)2 NotificationType (com.epam.pipeline.entity.notification.NotificationSettings.NotificationType)2 ExtendedRole (com.epam.pipeline.entity.user.ExtendedRole)2 Map (java.util.Map)2 MessageConstants (com.epam.pipeline.common.MessageConstants)1 MessageHelper (com.epam.pipeline.common.MessageHelper)1 JsonMapper (com.epam.pipeline.config.JsonMapper)1 NotificationMessageVO (com.epam.pipeline.controller.vo.notification.NotificationMessageVO)1 MonitoringNotificationDao (com.epam.pipeline.dao.notification.MonitoringNotificationDao)1 Pipeline (com.epam.pipeline.entity.pipeline.Pipeline)1