}
@Test
- public void should_send_notification_if_issue_change() throws Exception {
+ public void should_send_notification() throws Exception {
when(project.getAnalysisDate()).thenReturn(DateUtils.parseDate("2013-05-18"));
RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
Rule rule = new Rule("squid", "AvoidCycles");
DefaultIssue issue = new DefaultIssue()
.setNew(false)
.setChanged(true)
+ .setSendNotifications(true)
.setFieldChange(mock(IssueChangeContext.class), "severity", "MINOR", "BLOCKER")
.setRuleKey(ruleKey);
when(issueCache.all()).thenReturn(Arrays.asList(issue));
verify(notifications, never()).sendChanges(eq(issue), eq(changeContext), any(Rule.class), any(Component.class), any(Component.class));
}
+
+ @Test
+ public void should_not_send_notification_on_any_change() throws Exception {
+ IssueChangeContext changeContext = mock(IssueChangeContext.class);
+
+ when(project.getAnalysisDate()).thenReturn(DateUtils.parseDate("2013-05-18"));
+ RuleKey ruleKey = RuleKey.of("squid", "AvoidCycles");
+ DefaultIssue issue = new DefaultIssue()
+ .setChanged(true)
+ .setSendNotifications(false)
+ .setFieldChange(changeContext, "severity", "MINOR", "BLOCKER")
+ .setRuleKey(ruleKey);
+ when(issueCache.all()).thenReturn(Arrays.asList(issue));
+ when(ruleFinder.findByKey(ruleKey)).thenReturn(null);
+
+ SendIssueNotificationsPostJob job = new SendIssueNotificationsPostJob(issueCache, notifications, ruleFinder);
+ job.executeOn(project, sensorContext);
+
+ verify(notifications, never()).sendChanges(eq(issue), eq(changeContext), any(Rule.class), any(Component.class), any(Component.class));
+ }
}
@CheckForNull
private Notification createChangeNotification(DefaultIssue issue, IssueChangeContext context, Rule rule, Component project,
@Nullable Component component, @Nullable String comment) {
- FieldDiffs currentChange = issue.currentChange();
- if (comment == null && (currentChange == null || currentChange.diffs().isEmpty())) {
- return null;
- }
- Notification notification = newNotification(project, "issue-changes");
- notification.setFieldValue("key", issue.key());
- notification.setFieldValue("changeAuthor", context.login());
- notification.setFieldValue("reporter", issue.reporter());
- notification.setFieldValue("assignee", issue.assignee());
- notification.setFieldValue("message", issue.message());
- notification.setFieldValue("ruleName", ruleName(rule));
- notification.setFieldValue("componentKey", issue.componentKey());
- if (component != null) {
- notification.setFieldValue("componentName", component.longName());
- }
- if (comment != null) {
- notification.setFieldValue("comment", comment);
- }
+ Notification notification = null;
+ if (comment != null || issue.mustSendNotifications()) {
+ FieldDiffs currentChange = issue.currentChange();
+ notification = newNotification(project, "issue-changes");
+ notification.setFieldValue("key", issue.key());
+ notification.setFieldValue("changeAuthor", context.login());
+ notification.setFieldValue("reporter", issue.reporter());
+ notification.setFieldValue("assignee", issue.assignee());
+ notification.setFieldValue("message", issue.message());
+ notification.setFieldValue("ruleName", ruleName(rule));
+ notification.setFieldValue("componentKey", issue.componentKey());
+ if (component != null) {
+ notification.setFieldValue("componentName", component.longName());
+ }
+ if (comment != null) {
+ notification.setFieldValue("comment", comment);
+ }
- if (currentChange != null) {
- for (Map.Entry<String, FieldDiffs.Diff> entry : currentChange.diffs().entrySet()) {
- String type = entry.getKey();
- FieldDiffs.Diff diff = entry.getValue();
- notification.setFieldValue("old." + type, diff.oldValue() != null ? diff.oldValue().toString() : null);
- notification.setFieldValue("new." + type, diff.newValue() != null ? diff.newValue().toString() : null);
+ if (currentChange != null) {
+ for (Map.Entry<String, FieldDiffs.Diff> entry : currentChange.diffs().entrySet()) {
+ String type = entry.getKey();
+ FieldDiffs.Diff diff = entry.getValue();
+ notification.setFieldValue("old." + type, diff.oldValue() != null ? diff.oldValue().toString() : null);
+ notification.setFieldValue("new." + type, diff.newValue() != null ? diff.newValue().toString() : null);
+ }
}
}
return notification;
boolean updated = updater.assign(issue, "emmerik", context);
assertThat(updated).isTrue();
assertThat(issue.assignee()).isEqualTo("emmerik");
+ assertThat(issue.mustSendNotifications()).isTrue();
FieldDiffs.Diff diff = issue.currentChange().get("assignee");
assertThat(diff.oldValue()).isNull();
assertThat(diff.newValue()).isEqualTo("emmerik");
boolean updated = updater.assign(issue, null, context);
assertThat(updated).isTrue();
assertThat(issue.assignee()).isNull();
+ assertThat(issue.mustSendNotifications()).isTrue();
FieldDiffs.Diff diff = issue.currentChange().get("assignee");
assertThat(diff.oldValue()).isEqualTo("morgan");
assertThat(diff.newValue()).isNull();
boolean updated = updater.assign(issue, "emmerik", context);
assertThat(updated).isTrue();
assertThat(issue.assignee()).isEqualTo("emmerik");
+ assertThat(issue.mustSendNotifications()).isTrue();
FieldDiffs.Diff diff = issue.currentChange().get("assignee");
assertThat(diff.oldValue()).isEqualTo("morgan");
assertThat(diff.newValue()).isEqualTo("emmerik");
boolean updated = updater.assign(issue, "morgan", context);
assertThat(updated).isFalse();
assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
assertThat(updated).isTrue();
assertThat(issue.severity()).isEqualTo("BLOCKER");
assertThat(issue.manualSeverity()).isFalse();
+ assertThat(issue.mustSendNotifications()).isFalse();
FieldDiffs.Diff diff = issue.currentChange().get("severity");
assertThat(diff.oldValue()).isNull();
boolean updated = updater.setPastSeverity(issue, "INFO", context);
assertThat(updated).isTrue();
assertThat(issue.severity()).isEqualTo("BLOCKER");
+ assertThat(issue.mustSendNotifications()).isFalse();
FieldDiffs.Diff diff = issue.currentChange().get("severity");
assertThat(diff.oldValue()).isEqualTo("INFO");
assertThat(updated).isTrue();
assertThat(issue.severity()).isEqualTo("MINOR");
+ assertThat(issue.mustSendNotifications()).isFalse();
FieldDiffs.Diff diff = issue.currentChange().get("severity");
assertThat(diff.oldValue()).isEqualTo("BLOCKER");
assertThat(diff.newValue()).isEqualTo("MINOR");
issue.setSeverity("MINOR");
boolean updated = updater.setSeverity(issue, "MINOR", context);
assertThat(updated).isFalse();
+ assertThat(issue.mustSendNotifications()).isFalse();
assertThat(issue.currentChange()).isNull();
}
assertThat(updated).isTrue();
assertThat(issue.severity()).isEqualTo("MINOR");
assertThat(issue.manualSeverity()).isTrue();
+ assertThat(issue.mustSendNotifications()).isTrue();
FieldDiffs.Diff diff = issue.currentChange().get("severity");
assertThat(diff.oldValue()).isEqualTo("BLOCKER");
assertThat(diff.newValue()).isEqualTo("MINOR");
boolean updated = updater.setManualSeverity(issue, "MINOR", context);
assertThat(updated).isFalse();
assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
boolean updated = updater.setLine(issue, 123);
assertThat(updated).isTrue();
assertThat(issue.line()).isEqualTo(123);
-
+ assertThat(issue.mustSendNotifications()).isFalse();
// do not save change
assertThat(issue.currentChange()).isNull();
}
boolean updated = updater.setPastLine(issue, 123);
assertThat(updated).isTrue();
assertThat(issue.line()).isEqualTo(42);
+ assertThat(issue.mustSendNotifications()).isFalse();
// do not save change
assertThat(issue.currentChange()).isNull();
assertThat(updated).isFalse();
assertThat(issue.line()).isEqualTo(123);
assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
FieldDiffs.Diff diff = issue.currentChange().get("resolution");
assertThat(diff.oldValue()).isNull();
assertThat(diff.newValue()).isEqualTo("OPEN");
+ assertThat(issue.mustSendNotifications()).isTrue();
}
@Test
assertThat(updated).isFalse();
assertThat(issue.resolution()).isEqualTo("FIXED");
assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
FieldDiffs.Diff diff = issue.currentChange().get("status");
assertThat(diff.oldValue()).isNull();
assertThat(diff.newValue()).isEqualTo("OPEN");
+ assertThat(issue.mustSendNotifications()).isTrue();
}
@Test
assertThat(updated).isFalse();
assertThat(issue.status()).isEqualTo("CLOSED");
assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
assertThat(issue.currentChange().diffs()).hasSize(1);
assertThat(issue.currentChange().get("JIRA").oldValue()).isNull();
assertThat(issue.currentChange().get("JIRA").newValue()).isEqualTo("FOO-123");
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
assertThat(issue.currentChange().diffs()).hasSize(1);
assertThat(issue.currentChange().get("JIRA").oldValue()).isEqualTo("FOO-123");
assertThat(issue.currentChange().get("JIRA").newValue()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
issue.setAttribute("JIRA", "FOO-123");
boolean updated = updater.setAttribute(issue, "JIRA", "FOO-123", context);
assertThat(updated).isFalse();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
FieldDiffs.Diff diff = issue.currentChange().get("actionPlanKey");
assertThat(diff.oldValue()).isNull();
assertThat(diff.newValue()).isEqualTo("ABCD");
+ assertThat(issue.mustSendNotifications()).isTrue();
}
@Test
assertThat(updated).isTrue();
assertThat(issue.isChanged()).isTrue();
assertThat(issue.effortToFix()).isEqualTo(3.14);
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
assertThat(updated).isFalse();
assertThat(issue.isChanged()).isFalse();
assertThat(issue.effortToFix()).isEqualTo(3.14);
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
// do not save change
assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
assertThat(updated).isTrue();
assertThat(issue.isChanged()).isTrue();
assertThat(issue.message()).isEqualTo("the message");
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
// do not save change
assertThat(issue.currentChange()).isNull();
+ assertThat(issue.mustSendNotifications()).isFalse();
}
@Test
FieldDiffs.Diff diff = issue.currentChange().get("author");
assertThat(diff.oldValue()).isNull();
assertThat(diff.newValue()).isEqualTo("eric");
+ assertThat(issue.mustSendNotifications()).isFalse();
}
}