diff options
author | Fabrice Bellingard <bellingard@gmail.com> | 2012-02-03 17:09:26 +0100 |
---|---|---|
committer | Fabrice Bellingard <bellingard@gmail.com> | 2012-02-03 17:19:07 +0100 |
commit | 01906e4e61dcde195d2368c092f8471c0894079f (patch) | |
tree | 2bb68d32a6fd7586c2120dd3eec4492b67b8bc29 /sonar-batch/src | |
parent | 12552a94eebf5bf80fb6fb3952a9b363d568397e (diff) | |
download | sonarqube-01906e4e61dcde195d2368c092f8471c0894079f.tar.gz sonarqube-01906e4e61dcde195d2368c092f8471c0894079f.zip |
SONAR-2747 Send email when new violations appear on favourite project
The email is sent only if:
* the user has set the project as a favourite
* this is a "last analysis" (= no 'sonar.projectDate' specified)
* 'since last analysis' period was not removed in the admin page
* there are new violations (obviously...)
Diffstat (limited to 'sonar-batch/src')
-rw-r--r-- | sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java | 40 | ||||
-rw-r--r-- | sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java | 45 |
2 files changed, 67 insertions, 18 deletions
diff --git a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java index 749e3b36de8..59a07f5eaa8 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java +++ b/sonar-batch/src/main/java/org/sonar/batch/components/TimeMachineConfiguration.java @@ -19,7 +19,11 @@ */ package org.sonar.batch.components; -import com.google.common.collect.Lists; +import java.util.Date; +import java.util.List; + +import javax.persistence.Query; + import org.apache.commons.configuration.Configuration; import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; @@ -30,12 +34,8 @@ import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Project; import org.sonar.api.resources.Qualifiers; import org.sonar.api.utils.Logs; -import org.sonar.batch.components.PastSnapshot; -import org.sonar.batch.components.PastSnapshotFinder; -import javax.persistence.Query; -import java.util.Date; -import java.util.List; +import com.google.common.collect.Lists; public class TimeMachineConfiguration implements BatchExtension { @@ -46,7 +46,8 @@ public class TimeMachineConfiguration implements BatchExtension { private List<PastSnapshot> projectPastSnapshots; private DatabaseSession session; - public TimeMachineConfiguration(DatabaseSession session, Project project, Configuration configuration, PastSnapshotFinder pastSnapshotFinder) { + public TimeMachineConfiguration(DatabaseSession session, Project project, Configuration configuration, + PastSnapshotFinder pastSnapshotFinder) { this.session = session; this.project = project; this.configuration = configuration; @@ -69,7 +70,8 @@ public class TimeMachineConfiguration implements BatchExtension { } private Snapshot buildProjectSnapshot() { - Query query = session.createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled"); + Query query = session + .createNativeQuery("select p.id from projects p where p.kee=:resourceKey and p.qualifier<>:lib and p.enabled=:enabled"); query.setParameter("resourceKey", project.getKey()); query.setParameter("lib", Qualifiers.LIBRARY); query.setParameter("enabled", Boolean.TRUE); @@ -110,4 +112,26 @@ public class TimeMachineConfiguration implements BatchExtension { public boolean isFileVariationEnabled() { return configuration.getBoolean("sonar.enableFileVariation", Boolean.FALSE); } + + /** + * Returns the index corresponding to the 'previous_analysis' period (which is '1' by default). + * + * @return the index of 'previous_analysis' period, or NULL is users have modified the periods and haven't set a 'previous_analysis' one. + */ + public Integer getLastAnalysisPeriodIndex() { + // period1 is the default for 'previous_analysis' + String period1 = configuration.getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + "1"); + if (StringUtils.isBlank(period1) || CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(period1)) { + return 1; + } + // else search for the other periods + for (int index = 2; index < 6; index++) { + if (CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS.equals(configuration + .getString(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + index))) { + return index; + } + } + // if we're here, this means that we have not found the 'previous_analysis' mode + return null; + } } diff --git a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java index ab1c621cdde..7d4be9f7802 100644 --- a/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java +++ b/sonar-batch/src/test/java/org/sonar/batch/components/TimeMachineConfigurationTest.java @@ -19,6 +19,15 @@ */ package org.sonar.batch.components; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.nullValue; +import static org.mockito.Matchers.argThat; +import static org.mockito.Matchers.eq; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyZeroInteractions; + import org.apache.commons.configuration.PropertiesConfiguration; import org.hamcrest.BaseMatcher; import org.hamcrest.Description; @@ -26,29 +35,25 @@ import org.junit.Test; import org.sonar.api.CoreProperties; import org.sonar.api.database.model.Snapshot; import org.sonar.api.resources.Project; -import org.sonar.batch.components.PastSnapshotFinder; -import org.sonar.batch.components.TimeMachineConfiguration; import org.sonar.jpa.test.AbstractDbUnitTestCase; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.mockito.Matchers.argThat; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.*; - public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { @Test public void shouldSkipTendencies() { PropertiesConfiguration conf = new PropertiesConfiguration(); conf.setProperty(CoreProperties.SKIP_TENDENCIES_PROPERTY, true); - assertThat(new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), is(true)); + assertThat( + new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), + is(true)); } @Test public void shouldNotSkipTendenciesByDefault() { PropertiesConfiguration conf = new PropertiesConfiguration(); - assertThat(new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), is(false)); + assertThat( + new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, mock(PastSnapshotFinder.class)).skipTendencies(), + is(false)); } @Test @@ -79,4 +84,24 @@ public class TimeMachineConfigurationTest extends AbstractDbUnitTestCase { verifyZeroInteractions(pastSnapshotFinder); } + + @Test + public void shouldReturnLastAnalysisIndexIfSet() { + PropertiesConfiguration conf = new PropertiesConfiguration(); + TimeMachineConfiguration timeMachineConfiguration = new TimeMachineConfiguration(getSession(), new Project("my:project"), conf, + mock(PastSnapshotFinder.class)); + + // Nothing set, so period for 'previous_analysis' is 1 by default + assertThat(timeMachineConfiguration.getLastAnalysisPeriodIndex(), is(1)); + + // period1 has been replaced and 'previous_analysis' not set elsewhere... + conf.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + 1, "Version 1"); + conf.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + 2, "Version 2"); + assertThat(timeMachineConfiguration.getLastAnalysisPeriodIndex(), is(nullValue())); + + // 'previous_analysis' has now been set on period 4 + conf.setProperty(CoreProperties.TIMEMACHINE_PERIOD_PREFIX + 4, CoreProperties.TIMEMACHINE_MODE_PREVIOUS_ANALYSIS); + assertThat(timeMachineConfiguration.getLastAnalysisPeriodIndex(), is(4)); + } + } |