use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.
the class ChangesOnMyIssueNotificationHandlerTest method deliver_checks_by_projectKeys_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications.
@Test
public void deliver_checks_by_projectKeys_if_notifications_have_subscribed_assignee_to_ChangesOnMyIssues_notifications() {
when(emailNotificationChannel.isActivated()).thenReturn(true);
Set<ChangedIssue> issues = IntStream.range(0, 1 + new Random().nextInt(2)).mapToObj(i -> new ChangedIssue.Builder("issue_key_" + i).setNewStatus("foo").setAssignee(newUser("" + i)).setRule(newRule()).setProject(newProject(i + "")).build()).collect(toSet());
IssuesChangesNotificationBuilder builder = new IssuesChangesNotificationBuilder(issues, new UserChange(new Random().nextLong(), new User("user_uuid", "user_login", null)));
int deliver = underTest.deliver(ImmutableSet.of(serializer.serialize(builder)));
assertThat(deliver).isZero();
issues.stream().collect(MoreCollectors.index(ChangedIssue::getProject)).asMap().forEach((key, value) -> {
String projectKey = key.getKey();
Set<String> assigneeLogins = value.stream().map(i -> i.getAssignee().get().getLogin()).collect(toSet());
verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, projectKey, assigneeLogins, ALL_MUST_HAVE_ROLE_USER);
});
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
verifyNoMoreInteractions(emailNotificationChannel);
}
use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.
the class ChangesOnMyIssueNotificationHandlerTest method deliver_creates_a_notification_per_assignee_with_only_his_issues_on_the_single_project.
@Test
@UseDataProvider("userOrAnalysisChange")
public void deliver_creates_a_notification_per_assignee_with_only_his_issues_on_the_single_project(Change userOrAnalysisChange) {
when(emailNotificationChannel.isActivated()).thenReturn(true);
Project project = newProject();
User assignee1 = newUser("assignee_1");
User assignee2 = newUser("assignee_2");
Set<ChangedIssue> assignee1Issues = IntStream.range(0, 10).mapToObj(i -> newChangedIssue("1_issue_key_" + i, assignee1, project)).collect(toSet());
Set<ChangedIssue> assignee2Issues = IntStream.range(0, 10).mapToObj(i -> newChangedIssue("2_issue_key_" + i, assignee2, project)).collect(toSet());
Set<IssuesChangesNotification> notifications = Stream.of(// notification with only assignee1 5 notifications
new IssuesChangesNotificationBuilder(assignee1Issues.stream().limit(5).collect(toSet()), userOrAnalysisChange), // notification with only assignee2 6 notifications
new IssuesChangesNotificationBuilder(assignee2Issues.stream().limit(6).collect(toSet()), userOrAnalysisChange), // notification with 4 assignee1 and 3 assignee2 notifications
new IssuesChangesNotificationBuilder(Stream.concat(assignee1Issues.stream().skip(6), assignee2Issues.stream().skip(7)).collect(toSet()), userOrAnalysisChange)).map(t -> serializer.serialize(t)).collect(toSet());
when(notificationManager.findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project.getKey(), ImmutableSet.of(assignee1.getLogin(), assignee2.getLogin()), ALL_MUST_HAVE_ROLE_USER)).thenReturn(ImmutableSet.of(emailRecipientOf(assignee1.getLogin()), emailRecipientOf(assignee2.getLogin())));
int deliveredCount = new Random().nextInt(100);
when(emailNotificationChannel.deliverAll(anySet())).thenReturn(deliveredCount);
int deliver = underTest.deliver(notifications);
assertThat(deliver).isEqualTo(deliveredCount);
verify(notificationManager).findSubscribedEmailRecipients(CHANGE_ON_MY_ISSUES_DISPATCHER_KEY, project.getKey(), ImmutableSet.of(assignee1.getLogin(), assignee2.getLogin()), ALL_MUST_HAVE_ROLE_USER);
verifyNoMoreInteractions(notificationManager);
verify(emailNotificationChannel).isActivated();
verify(emailNotificationChannel).deliverAll(emailDeliveryRequestSetCaptor.capture());
verifyNoMoreInteractions(emailNotificationChannel);
Set<EmailDeliveryRequest> emailDeliveryRequests = emailDeliveryRequestSetCaptor.getValue();
assertThat(emailDeliveryRequests).hasSize(4);
ListMultimap<String, EmailDeliveryRequest> emailDeliveryRequestByEmail = emailDeliveryRequests.stream().collect(index(EmailDeliveryRequest::getRecipientEmail));
List<EmailDeliveryRequest> assignee1Requests = emailDeliveryRequestByEmail.get(emailOf(assignee1.getLogin()));
assertThat(assignee1Requests).hasSize(2).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChange).containsOnly(userOrAnalysisChange);
assertThat(assignee1Requests).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChangedIssues).containsOnly(assignee1Issues.stream().limit(5).collect(unorderedIndex(t -> project, t -> t)), assignee1Issues.stream().skip(6).collect(unorderedIndex(t -> project, t -> t)));
List<EmailDeliveryRequest> assignee2Requests = emailDeliveryRequestByEmail.get(emailOf(assignee2.getLogin()));
assertThat(assignee2Requests).hasSize(2).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChange).containsOnly(userOrAnalysisChange);
assertThat(assignee2Requests).extracting(t -> (ChangesOnMyIssuesNotification) t.getNotification()).extracting(ChangesOnMyIssuesNotification::getChangedIssues).containsOnly(assignee2Issues.stream().limit(6).collect(unorderedIndex(t -> project, t -> t)), assignee2Issues.stream().skip(7).collect(unorderedIndex(t -> project, t -> t)));
}
use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.
the class FpOrWontFixEmailTemplateTest method formats_returns_html_message_for_single_hotspot_on_master.
@Test
@UseDataProvider("fpOrWontFixValuesByUserOrAnalysisChange")
public void formats_returns_html_message_for_single_hotspot_on_master(Change change, FpOrWontFix fpOrWontFix) {
Project project = newProject("1");
String ruleName = randomAlphabetic(8);
String host = randomAlphabetic(15);
ChangedIssue changedIssue = newChangedIssue("key", project, ruleName, SECURITY_HOTSPOT);
when(emailSettings.getServerBaseURL()).thenReturn(host);
EmailMessage emailMessage = underTest.format(new FPOrWontFixNotification(change, ImmutableSet.of(changedIssue), fpOrWontFix));
HtmlFragmentAssert.assertThat(emailMessage.getMessage()).hasParagraph().hasParagraph().hasParagraph(project.getProjectName()).hasList("Rule " + ruleName + " - See the single hotspot").withLink("See the single hotspot", host + "/project/issues?id=" + project.getKey() + "&issues=" + changedIssue.getKey() + "&open=" + changedIssue.getKey()).hasParagraph().hasParagraph().noMoreBlock();
}
use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.
the class BulkChangeAction method sendNotification.
private void sendNotification(Collection<DefaultIssue> issues, BulkChangeData bulkChangeData, Map<String, UserDto> userDtoByUuid, UserDto author) {
if (!bulkChangeData.sendNotification) {
return;
}
Set<ChangedIssue> changedIssues = issues.stream().filter(issue -> issue.updateDate() != null).map(issue -> toNotification(bulkChangeData, userDtoByUuid, issue)).filter(Objects::nonNull).collect(toSet(issues.size()));
if (changedIssues.isEmpty()) {
return;
}
IssuesChangesNotificationBuilder builder = new IssuesChangesNotificationBuilder(changedIssues, new UserChange(oldestUpdateDate(issues), new User(author.getUuid(), author.getLogin(), author.getName())));
notificationService.scheduleForSending(notificationSerializer.serialize(builder));
}
use of org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue in project sonarqube by SonarSource.
the class BulkChangeAction method toNotification.
@CheckForNull
private ChangedIssue toNotification(BulkChangeData bulkChangeData, Map<String, UserDto> userDtoByUuid, DefaultIssue issue) {
BranchDto branchDto = bulkChangeData.branchesByProjectUuid.get(issue.projectUuid());
if (!hasNotificationSupport(branchDto)) {
return null;
}
RuleDefinitionDto ruleDefinitionDto = bulkChangeData.rulesByKey.get(issue.ruleKey());
ComponentDto projectDto = bulkChangeData.projectsByUuid.get(issue.projectUuid());
if (ruleDefinitionDto == null || projectDto == null) {
return null;
}
Optional<UserDto> assignee = Optional.ofNullable(issue.assignee()).map(userDtoByUuid::get);
return new ChangedIssue.Builder(issue.key()).setNewStatus(issue.status()).setNewResolution(issue.resolution()).setAssignee(assignee.map(u -> new User(u.getUuid(), u.getLogin(), u.getName())).orElse(null)).setRule(new IssuesChangesNotificationBuilder.Rule(ruleDefinitionDto.getKey(), RuleType.valueOfNullable(ruleDefinitionDto.getType()), ruleDefinitionDto.getName())).setProject(new Project.Builder(projectDto.uuid()).setKey(projectDto.getKey()).setProjectName(projectDto.name()).setBranchName(branchDto.isMain() ? null : branchDto.getKey()).build()).build();
}
Aggregations