aboutsummaryrefslogtreecommitdiffstats
path: root/plugins
diff options
context:
space:
mode:
authorJulien Lancelot <julien.lancelot@gmail.com>2013-09-17 16:19:26 +0200
committerJulien Lancelot <julien.lancelot@gmail.com>2013-09-17 16:19:51 +0200
commitb4b3b848a8e671a08ecb0fd9941714696efc822b (patch)
treef635f9b746d1b1d78acb805285f03fb40f596fed /plugins
parent7c47cc7aacd917de86511d5f8c7933a92999d943 (diff)
downloadsonarqube-b4b3b848a8e671a08ecb0fd9941714696efc822b.tar.gz
sonarqube-b4b3b848a8e671a08ecb0fd9941714696efc822b.zip
SONAR-4625 New issues email should contain total issues for each issue type
Diffstat (limited to 'plugins')
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java29
-rw-r--r--plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJob.java7
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java26
-rw-r--r--plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJobTest.java17
4 files changed, 63 insertions, 16 deletions
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java
index 6d4c99f7b18..37d74602045 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplate.java
@@ -19,8 +19,11 @@
*/
package org.sonar.plugins.core.issue.notification;
+import com.google.common.collect.Lists;
import org.sonar.api.config.EmailSettings;
+import org.sonar.api.i18n.I18n;
import org.sonar.api.notifications.Notification;
+import org.sonar.api.rule.Severity;
import org.sonar.api.utils.DateUtils;
import org.sonar.plugins.emailnotifications.api.EmailMessage;
import org.sonar.plugins.emailnotifications.api.EmailTemplate;
@@ -28,6 +31,8 @@ import org.sonar.plugins.emailnotifications.api.EmailTemplate;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Date;
+import java.util.Iterator;
+import java.util.Locale;
/**
* Creates email message for notification "new-issues".
@@ -37,9 +42,11 @@ import java.util.Date;
public class NewIssuesEmailTemplate extends EmailTemplate {
private final EmailSettings settings;
+ private final I18n i18n;
- public NewIssuesEmailTemplate(EmailSettings settings) {
+ public NewIssuesEmailTemplate(EmailSettings settings, I18n i18n) {
this.settings = settings;
+ this.i18n = i18n;
}
@Override
@@ -48,11 +55,21 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
return null;
}
String projectName = notification.getFieldValue("projectName");
- String violationsCount = notification.getFieldValue("count");
StringBuilder sb = new StringBuilder();
- sb.append("Project: ").append(projectName).append('\n');
- sb.append(violationsCount).append(" new issues").append('\n');
+ sb.append("Project: ").append(projectName).append("\n\n");
+ sb.append(notification.getFieldValue("count")).append(" new issues").append("\n\n");
+ sb.append(" ");
+ for (Iterator<String> severityIterator = Lists.reverse(Severity.ALL).iterator(); severityIterator.hasNext(); ) {
+ String severity = severityIterator.next();
+ String severityLabel = i18n.message(getLocale(), "severity."+ severity, severity);
+ sb.append(severityLabel).append(": ").append(notification.getFieldValue("count-"+ severity));
+ if (severityIterator.hasNext()) {
+ sb.append(" ");
+ }
+ }
+ sb.append('\n');
+
appendFooter(sb, notification);
EmailMessage message = new EmailMessage()
@@ -82,4 +99,8 @@ public class NewIssuesEmailTemplate extends EmailTemplate {
}
}
+ private Locale getLocale() {
+ return Locale.ENGLISH;
+ }
+
}
diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJob.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJob.java
index 0cbe7456c6e..6f79d1bf85a 100644
--- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJob.java
+++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJob.java
@@ -29,6 +29,7 @@ import org.sonar.api.rules.RuleFinder;
import org.sonar.batch.issue.IssueCache;
import org.sonar.core.DryRunIncompatible;
import org.sonar.core.issue.IssueNotifications;
+import org.sonar.core.issue.IssuesBySeverity;
import java.util.LinkedHashMap;
import java.util.Map;
@@ -55,12 +56,12 @@ public class SendIssueNotificationsPostJob implements PostJob {
}
private void sendNotifications(Project project) {
- int newIssues = 0;
+ IssuesBySeverity newIssues = new IssuesBySeverity();
IssueChangeContext context = IssueChangeContext.createScan(project.getAnalysisDate());
Map<DefaultIssue, Rule> shouldSentNotification = new LinkedHashMap<DefaultIssue, Rule>();
for (DefaultIssue issue : issueCache.all()) {
if (issue.isNew() && issue.resolution() == null) {
- newIssues++;
+ newIssues.add(issue);
}
if (!issue.isNew() && issue.isChanged() && issue.mustSendNotifications()) {
Rule rule = ruleFinder.findByKey(issue.ruleKey());
@@ -73,7 +74,7 @@ public class SendIssueNotificationsPostJob implements PostJob {
if (!shouldSentNotification.isEmpty()) {
notifications.sendChanges(shouldSentNotification, context, project, null);
}
- if (newIssues > 0) {
+ if (newIssues.size() > 0) {
notifications.sendNewIssues(project, newIssues);
}
}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java
index 9b3b1deb392..35d0468f1ef 100644
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/NewIssuesEmailTemplateTest.java
@@ -22,26 +22,36 @@ package org.sonar.plugins.core.issue.notification;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.config.EmailSettings;
import org.sonar.api.notifications.Notification;
+import org.sonar.core.i18n.I18nManager;
import org.sonar.plugins.emailnotifications.api.EmailMessage;
+import java.util.Locale;
import java.util.TimeZone;
import static org.fest.assertions.Assertions.assertThat;
+import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
+@RunWith(MockitoJUnitRunner.class)
public class NewIssuesEmailTemplateTest {
NewIssuesEmailTemplate template;
TimeZone initialTimeZone = TimeZone.getDefault();
+ @Mock
+ I18nManager i18n;
+
@Before
public void setUp() {
EmailSettings settings = mock(EmailSettings.class);
when(settings.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");
- template = new NewIssuesEmailTemplate(settings);
+ template = new NewIssuesEmailTemplate(settings, i18n);
TimeZone.setDefault(TimeZone.getTimeZone("GMT"));
}
@@ -73,17 +83,31 @@ public class NewIssuesEmailTemplateTest {
public void shouldFormatCommentAdded() {
Notification notification = new Notification("new-issues")
.setFieldValue("count", "32")
+ .setFieldValue("count-INFO", "1")
+ .setFieldValue("count-MINOR", "3")
+ .setFieldValue("count-MAJOR", "10")
+ .setFieldValue("count-CRITICAL", "5")
+ .setFieldValue("count-BLOCKER", "0")
.setFieldValue("projectName", "Struts")
.setFieldValue("projectKey", "org.apache:struts")
.setFieldValue("projectDate", "2010-05-18T14:50:45+0000");
+ when(i18n.message(any(Locale.class), eq("severity.BLOCKER"), anyString())).thenReturn("Blocker");
+ when(i18n.message(any(Locale.class), eq("severity.CRITICAL"), anyString())).thenReturn("Critical");
+ when(i18n.message(any(Locale.class), eq("severity.MAJOR"), anyString())).thenReturn("Major");
+ when(i18n.message(any(Locale.class), eq("severity.MINOR"), anyString())).thenReturn("Minor");
+ when(i18n.message(any(Locale.class), eq("severity.INFO"), anyString())).thenReturn("Info");
+
EmailMessage message = template.format(notification);
assertThat(message.getMessageId()).isEqualTo("new-issues/org.apache:struts");
assertThat(message.getSubject()).isEqualTo("Struts: new issues");
assertThat(message.getMessage()).isEqualTo("" +
"Project: Struts\n" +
+ "\n" +
"32 new issues\n" +
"\n" +
+ " Blocker: 0 Critical: 5 Major: 10 Minor: 3 Info: 1\n" +
+ "\n" +
"See it in SonarQube: http://nemo.sonarsource.org/issues/search?componentRoots=org.apache%3Astruts&createdAt=2010-05-18T14%3A50%3A45%2B0000\n");
}
diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJobTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJobTest.java
index 82a1fe70ff4..6c612f78635 100644
--- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJobTest.java
+++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/issue/notification/SendIssueNotificationsPostJobTest.java
@@ -21,6 +21,7 @@ package org.sonar.plugins.core.issue.notification;
import org.junit.Test;
import org.junit.runner.RunWith;
+import org.mockito.ArgumentCaptor;
import org.mockito.ArgumentMatcher;
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
@@ -35,19 +36,17 @@ import org.sonar.api.rules.RuleFinder;
import org.sonar.api.utils.DateUtils;
import org.sonar.batch.issue.IssueCache;
import org.sonar.core.issue.IssueNotifications;
+import org.sonar.core.issue.IssuesBySeverity;
import java.util.Arrays;
import java.util.Map;
+import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.argThat;
import static org.mockito.Matchers.eq;
import static org.mockito.Matchers.isNull;
-import static org.mockito.Mockito.mock;
-import static org.mockito.Mockito.never;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.verifyZeroInteractions;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class SendIssueNotificationsPostJobTest {
@@ -70,14 +69,16 @@ public class SendIssueNotificationsPostJobTest {
public void should_send_notif_if_new_issues() throws Exception {
when(project.getAnalysisDate()).thenReturn(DateUtils.parseDate("2013-05-18"));
when(issueCache.all()).thenReturn(Arrays.asList(
- new DefaultIssue().setNew(true),
- new DefaultIssue().setNew(false)
+ new DefaultIssue().setNew(true).setSeverity("MAJOR"),
+ new DefaultIssue().setNew(false).setSeverity("MINOR")
));
SendIssueNotificationsPostJob job = new SendIssueNotificationsPostJob(issueCache, notifications, ruleFinder);
job.executeOn(project, sensorContext);
- verify(notifications).sendNewIssues(project, 1);
+ ArgumentCaptor<IssuesBySeverity> argument = ArgumentCaptor.forClass(IssuesBySeverity.class);
+ verify(notifications).sendNewIssues(eq(project), argument.capture());
+ assertThat(argument.getValue().size()).isEqualTo(1);
}
@Test