diff options
2 files changed, 17 insertions, 1 deletions
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 23b731b25c2..b539fdca79f 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 @@ -213,7 +213,8 @@ public class NewViolationsDecorator implements Decorator { if (lastAnalysisPeriodIndex != null) { PastSnapshot pastSnapshot = timeMachineConfiguration.getProjectPastSnapshots().get(lastAnalysisPeriodIndex - 1); Double newViolationsCount = context.getMeasure(CoreMetrics.NEW_VIOLATIONS).getVariation(lastAnalysisPeriodIndex); - if (newViolationsCount != null && newViolationsCount > 0) { + // Do not send notification if this is the first analysis or if there's no violation + if (pastSnapshot.getTargetDate() != null && 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") 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 bb60b3cf32a..3e77564a3c6 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 @@ -216,6 +216,21 @@ public class NewViolationsDecoratorTest { } @Test + public void shouldNotNotifyUserIfFirstAnalysis() throws Exception { + Project project = new Project("key").setName("LongName"); + project.setId(45); + when(timeMachineConfiguration.getLastAnalysisPeriodIndex()).thenReturn(1); + // PastSnaptho with targetDate==null means first analysis + PastSnapshot pastSnapshot = new PastSnapshot("", null); + when(timeMachineConfiguration.getProjectPastSnapshots()).thenReturn(Lists.newArrayList(pastSnapshot)); + Measure m = new Measure(CoreMetrics.NEW_VIOLATIONS).setVariation1(0.0); + when(context.getMeasure(CoreMetrics.NEW_VIOLATIONS)).thenReturn(m); + + decorator.decorate(project, context); + verify(notificationManager, never()).scheduleForSending(any(Notification.class)); + } + + @Test public void shouldNotifyUserAboutNewViolations() throws Exception { Project project = new Project("key").setName("LongName"); project.setId(45); |