From e39aff8913bf3c8c4fda2784ad7c190a0d1768ae Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Tue, 7 Feb 2012 16:32:00 +0100 Subject: [PATCH] SONAR-2747 Improve email rendering --- .../core/timemachine/NewViolationsDecorator.java | 8 +++++++- .../core/timemachine/NewViolationsDecoratorTest.java | 12 +++++++++++- .../newviolations/NewViolationsEmailTemplate.java | 11 ++++++++--- ...Test.java => NewViolationsEmailTemplateTest.java} | 12 ++++++------ 4 files changed, 32 insertions(+), 11 deletions(-) rename plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/{NewViolationsTemplateTest.java => NewViolationsEmailTemplateTest.java} (88%) diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java index 96c4e6aa589..23b731b25c2 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/timemachine/NewViolationsDecorator.java @@ -19,6 +19,8 @@ */ package org.sonar.plugins.core.timemachine; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Arrays; import java.util.Collection; import java.util.Date; @@ -209,15 +211,19 @@ public class NewViolationsDecorator implements Decorator { protected void notifyNewViolations(Project project, DecoratorContext context) { Integer lastAnalysisPeriodIndex = timeMachineConfiguration.getLastAnalysisPeriodIndex(); if (lastAnalysisPeriodIndex != null) { + PastSnapshot pastSnapshot = timeMachineConfiguration.getProjectPastSnapshots().get(lastAnalysisPeriodIndex - 1); Double newViolationsCount = context.getMeasure(CoreMetrics.NEW_VIOLATIONS).getVariation(lastAnalysisPeriodIndex); if (newViolationsCount != null && newViolationsCount > 0) { // Maybe we should check if this is the first analysis or not? + DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); Notification notification = new Notification("new-violations") .setFieldValue("count", String.valueOf(newViolationsCount.intValue())) .setFieldValue("projectName", project.getLongName()) .setFieldValue("projectKey", project.getKey()) .setFieldValue("projectId", String.valueOf(project.getId())) - .setFieldValue("period", lastAnalysisPeriodIndex.toString()); + .setFieldValue("period", lastAnalysisPeriodIndex.toString()) + .setFieldValue("fromDate", dateformat.format(pastSnapshot.getTargetDate())) + .setFieldValue("toDate", dateformat.format(new Date())); notificationManager.scheduleForSending(notification); } } diff --git a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java index d38f59e30eb..bb60b3cf32a 100644 --- a/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java +++ b/plugins/sonar-core-plugin/src/test/java/org/sonar/plugins/core/timemachine/NewViolationsDecoratorTest.java @@ -30,8 +30,12 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; +import java.text.DateFormat; +import java.text.SimpleDateFormat; import java.util.Arrays; +import java.util.Calendar; import java.util.Date; +import java.util.GregorianCalendar; import java.util.List; import org.apache.commons.lang.ObjectUtils; @@ -216,17 +220,23 @@ public class NewViolationsDecoratorTest { Project project = new Project("key").setName("LongName"); project.setId(45); when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(2); + Calendar pastDate = new GregorianCalendar(2011, 10, 25); + PastSnapshot pastSnapshot = new PastSnapshot("", pastDate.getTime()); + when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Lists.newArrayList(pastSnapshot, pastSnapshot)); Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation2(32.0); when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m); decorator.decorate(project, context); + DateFormat dateformat = new SimpleDateFormat("yyyy-MM-dd"); Notification notification = new Notification("new-violations") .setFieldValue("count", "32") .setFieldValue("projectName", "LongName") .setFieldValue("projectKey", "key") .setFieldValue("projectId", "45") - .setFieldValue("period", "2"); + .setFieldValue("period", "2") + .setFieldValue("fromDate", dateformat.format(pastDate.getTime())) + .setFieldValue("toDate", dateformat.format(new Date())); verify(notificationManager, times(1)).scheduleForSending(eq(notification)); } diff --git a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplate.java b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplate.java index d0a874f943f..b719c4e336d 100644 --- a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplate.java +++ b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplate.java @@ -45,8 +45,13 @@ public class NewViolationsEmailTemplate extends EmailTemplate { StringBuilder sb = new StringBuilder(); String projectName = notification.getFieldValue("projectName"); - appendLine(sb, "Project", projectName); - appendLine(sb, "New violations on last analysis", notification.getFieldValue("count")); + String violationsCount = notification.getFieldValue("count"); + String fromDate = notification.getFieldValue("fromDate"); + String toDate = notification.getFieldValue("toDate"); + + sb.append("Project: ").append(projectName).append('\n'); + sb.append(violationsCount).append(" new violations on last analysis"); + sb.append(" (introduced between ").append(fromDate).append(" and ").append(toDate).append(")").append('\n'); appendFooter(sb, notification); EmailMessage message = new EmailMessage() @@ -64,7 +69,7 @@ public class NewViolationsEmailTemplate extends EmailTemplate { private void appendFooter(StringBuilder sb, Notification notification) { String projectKey = notification.getFieldValue("projectKey"); String period = notification.getFieldValue("period"); - sb.append("\n--\n") + sb.append("\n") .append("See it in Sonar: ").append(configuration.getServerBaseURL()).append("/drilldown/measures/").append(projectKey) .append("?metric=new_violations&period=").append(period).append('\n'); } diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsTemplateTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplateTest.java similarity index 88% rename from plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsTemplateTest.java rename to plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplateTest.java index 3e462762982..c5c46712a94 100644 --- a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsTemplateTest.java +++ b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/newviolations/NewViolationsEmailTemplateTest.java @@ -31,7 +31,7 @@ import org.sonar.api.notifications.Notification; import org.sonar.plugins.emailnotifications.EmailConfiguration; import org.sonar.plugins.emailnotifications.api.EmailMessage; -public class NewViolationsTemplateTest { +public class NewViolationsEmailTemplateTest { private NewViolationsEmailTemplate template; @@ -55,9 +55,8 @@ public class NewViolationsTemplateTest { * From: Sonar * * Project: Foo - * New violations on last analysis: 32 + * 32 new violations on last analysis (introduced between 2012-01-02 and 2012-01-15) * - * -- * See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=2 * */ @@ -68,16 +67,17 @@ public class NewViolationsTemplateTest { .setFieldValue("projectName", "Foo") .setFieldValue("projectKey", "org.sonar.foo:foo") .setFieldValue("projectId", "45") - .setFieldValue("period", "2"); + .setFieldValue("period", "2") + .setFieldValue("fromDate", "2012-01-02") + .setFieldValue("toDate", "2012-01-15"); EmailMessage message = template.format(notification); assertThat(message.getMessageId(), is("new-violations/45")); assertThat(message.getSubject(), is("New violations for project Foo")); assertThat(message.getMessage(), is("" + "Project: Foo\n" + - "New violations on last analysis: 32\n" + + "32 new violations on last analysis (introduced between 2012-01-02 and 2012-01-15)\n" + "\n" + - "--\n" + "See it in Sonar: http://nemo.sonarsource.org/drilldown/measures/org.sonar.foo:foo?metric=new_violations&period=2\n")); } -- 2.39.5