Search in sources :

Example 21 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser 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 22 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser 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 23 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser in project cloud-pipeline by epam.

the class SAMLUserDetailsServiceImpl method loadUserBySAML.

@Override
public UserContext loadUserBySAML(SAMLCredential credential) {
    String userName = credential.getNameID().getValue().toUpperCase();
    List<String> groups = readAuthorities(credential);
    Map<String, String> attributes = readAttributes(credential);
    PipelineUser loadedUser = userManager.loadUserByName(userName);
    if (loadedUser == null) {
        String message = messageHelper.getMessage(MessageConstants.ERROR_USER_NAME_NOT_FOUND, userName);
        if (!autoCreateUsers) {
            throw new UsernameNotFoundException(message);
        }
        LOGGER.debug(message);
        List<Long> roles = roleManager.getDefaultRolesIds();
        PipelineUser createdUser = userManager.createUser(userName, roles, groups, attributes, null);
        UserContext userContext = new UserContext(createdUser.getId(), userName);
        userContext.setGroups(createdUser.getGroups());
        LOGGER.debug("Created user {} with groups {}", userName, groups);
        userContext.setRoles(createdUser.getRoles());
        return userContext;
    } else {
        LOGGER.debug("Found user by name {}", userName);
        loadedUser.setUserName(userName);
        List<Long> roles = loadedUser.getRoles().stream().map(Role::getId).collect(Collectors.toList());
        if (userManager.needToUpdateUser(groups, attributes, loadedUser)) {
            loadedUser = userManager.updateUserSAMLInfo(loadedUser.getId(), userName, roles, groups, attributes);
            LOGGER.debug("Updated user groups {} ", groups);
        }
        return new UserContext(loadedUser);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) UserContext(com.epam.pipeline.security.UserContext)

Example 24 with PipelineUser

use of com.epam.pipeline.entity.user.PipelineUser 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()))));
}
Also used : IntStream(java.util.stream.IntStream) PipelineRunMapper(com.epam.pipeline.mapper.PipelineRunMapper) Arrays(java.util.Arrays) PipelineRunDao(com.epam.pipeline.dao.pipeline.PipelineRunDao) Date(java.util.Date) Autowired(org.springframework.beans.factory.annotation.Autowired) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) PipelineDao(com.epam.pipeline.dao.pipeline.PipelineDao) Propagation(org.springframework.transaction.annotation.Propagation) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) Before(org.junit.Before) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) DefaultRoles(com.epam.pipeline.entity.user.DefaultRoles) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) Collectors(java.util.stream.Collectors) com.epam.pipeline.entity.pipeline(com.epam.pipeline.entity.pipeline) Objects(java.util.Objects) List(java.util.List) Assert.assertNull(org.junit.Assert.assertNull) Assert.assertFalse(org.junit.Assert.assertFalse) UserDao(com.epam.pipeline.dao.user.UserDao) Collections(java.util.Collections) TestCase.assertEquals(junit.framework.TestCase.assertEquals) Transactional(org.springframework.transaction.annotation.Transactional) PipelineUser(com.epam.pipeline.entity.user.PipelineUser) NotificationMessage(com.epam.pipeline.entity.notification.NotificationMessage) NotificationTemplate(com.epam.pipeline.entity.notification.NotificationTemplate) AbstractSpringTest(com.epam.pipeline.AbstractSpringTest) Test(org.junit.Test) Transactional(org.springframework.transaction.annotation.Transactional)

Example 25 with PipelineUser

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

Aggregations

PipelineUser (com.epam.pipeline.entity.user.PipelineUser)44 Transactional (org.springframework.transaction.annotation.Transactional)23 Test (org.junit.Test)19 NotificationMessage (com.epam.pipeline.entity.notification.NotificationMessage)13 NotificationTemplate (com.epam.pipeline.entity.notification.NotificationTemplate)13 AbstractSpringTest (com.epam.pipeline.AbstractSpringTest)12 ArrayList (java.util.ArrayList)8 DefaultRoles (com.epam.pipeline.entity.user.DefaultRoles)7 Collections (java.util.Collections)7 List (java.util.List)7 Map (java.util.Map)7 Autowired (org.springframework.beans.factory.annotation.Autowired)7 Role (com.epam.pipeline.entity.user.Role)6 AbstractSpringTest (com.epam.pipeline.notifier.AbstractSpringTest)6 Arrays (java.util.Arrays)6 S3bucketDataStorage (com.epam.pipeline.entity.datastorage.aws.S3bucketDataStorage)5 PipelineRun (com.epam.pipeline.entity.pipeline.PipelineRun)5 Collection (java.util.Collection)5 CollectionUtils (org.apache.commons.collections4.CollectionUtils)5 DataStorageDao (com.epam.pipeline.dao.datastorage.DataStorageDao)4