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-core | |
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-core')
7 files changed, 212 insertions, 7 deletions
diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java index a6d536ca07f..a750228cc37 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/DaoUtils.java @@ -22,6 +22,7 @@ package org.sonar.core.persistence; import org.sonar.core.dashboard.ActiveDashboardDao; import org.sonar.core.dashboard.DashboardDao; import org.sonar.core.duplication.DuplicationDao; +import org.sonar.core.properties.PropertiesDao; import org.sonar.core.purge.PurgeDao; import org.sonar.core.resource.ResourceDao; import org.sonar.core.resource.ResourceIndexerDao; @@ -44,6 +45,7 @@ public final class DaoUtils { DashboardDao.class, DuplicationDao.class, LoadedTemplateDao.class, + PropertiesDao.class, PurgeDao.class, ResourceIndexerDao.class, ResourceDao.class, diff --git a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java index b42b27d54d5..3629a399df3 100644 --- a/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java +++ b/sonar-core/src/main/java/org/sonar/core/persistence/MyBatis.java @@ -19,22 +19,41 @@ */ package org.sonar.core.persistence; +import java.io.IOException; +import java.io.InputStream; + import org.apache.commons.io.IOUtils; import org.apache.commons.lang.StringUtils; import org.apache.ibatis.builder.xml.XMLMapperBuilder; import org.apache.ibatis.logging.LogFactory; import org.apache.ibatis.mapping.Environment; -import org.apache.ibatis.session.*; +import org.apache.ibatis.session.Configuration; +import org.apache.ibatis.session.ExecutorType; +import org.apache.ibatis.session.SqlSession; +import org.apache.ibatis.session.SqlSessionFactory; +import org.apache.ibatis.session.SqlSessionFactoryBuilder; import org.apache.ibatis.transaction.jdbc.JdbcTransactionFactory; import org.slf4j.LoggerFactory; import org.sonar.api.BatchComponent; import org.sonar.api.ServerComponent; -import org.sonar.core.dashboard.*; +import org.sonar.core.dashboard.ActiveDashboardDto; +import org.sonar.core.dashboard.ActiveDashboardMapper; +import org.sonar.core.dashboard.DashboardDto; +import org.sonar.core.dashboard.DashboardMapper; +import org.sonar.core.dashboard.WidgetDto; +import org.sonar.core.dashboard.WidgetMapper; +import org.sonar.core.dashboard.WidgetPropertyDto; +import org.sonar.core.dashboard.WidgetPropertyMapper; import org.sonar.core.duplication.DuplicationMapper; import org.sonar.core.duplication.DuplicationUnitDto; +import org.sonar.core.properties.PropertiesMapper; import org.sonar.core.purge.PurgeMapper; import org.sonar.core.purge.PurgeableSnapshotDto; -import org.sonar.core.resource.*; +import org.sonar.core.resource.ResourceDto; +import org.sonar.core.resource.ResourceIndexDto; +import org.sonar.core.resource.ResourceIndexerMapper; +import org.sonar.core.resource.ResourceMapper; +import org.sonar.core.resource.SnapshotDto; import org.sonar.core.review.ReviewDto; import org.sonar.core.review.ReviewMapper; import org.sonar.core.rule.RuleDto; @@ -42,9 +61,6 @@ import org.sonar.core.rule.RuleMapper; import org.sonar.core.template.LoadedTemplateDto; import org.sonar.core.template.LoadedTemplateMapper; -import java.io.IOException; -import java.io.InputStream; - public class MyBatis implements BatchComponent, ServerComponent { private Database database; @@ -80,6 +96,7 @@ public class MyBatis implements BatchComponent, ServerComponent { loadMapper(conf, DashboardMapper.class); loadMapper(conf, DuplicationMapper.class); loadMapper(conf, LoadedTemplateMapper.class); + loadMapper(conf, PropertiesMapper.class); loadMapper(conf, PurgeMapper.class); loadMapper(conf, ResourceMapper.class); loadMapper(conf, ReviewMapper.class); @@ -131,7 +148,7 @@ public class MyBatis implements BatchComponent, ServerComponent { private InputStream getPathToMapper(Class mapperClass) { InputStream input = getClass().getResourceAsStream( - "/" + StringUtils.replace(mapperClass.getName(), ".", "/") + "-" + database.getDialect().getId() + ".xml"); + "/" + StringUtils.replace(mapperClass.getName(), ".", "/") + "-" + database.getDialect().getId() + ".xml"); if (input == null) { input = getClass().getResourceAsStream("/" + StringUtils.replace(mapperClass.getName(), ".", "/") + ".xml"); } diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java new file mode 100644 index 00000000000..3762d35e529 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesDao.java @@ -0,0 +1,53 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.properties; + +import java.util.List; + +import org.apache.ibatis.session.SqlSession; +import org.sonar.api.BatchComponent; +import org.sonar.api.ServerComponent; +import org.sonar.core.persistence.MyBatis; + +public class PropertiesDao implements BatchComponent, ServerComponent { + + private MyBatis mybatis; + + public PropertiesDao(MyBatis mybatis) { + this.mybatis = mybatis; + } + + /** + * Returns the logins of users who have flagged as favourite the resource identified by the given id. + * + * @param resourceId the resource id + * @return the list of logins (maybe be empty - obviously) + */ + public List<String> findUserIdsForFavouriteResource(Integer resourceId) { + SqlSession session = mybatis.openSession(); + PropertiesMapper mapper = session.getMapper(PropertiesMapper.class); + try { + return mapper.findUserIdsForFavouriteResource(resourceId); + } finally { + MyBatis.closeQuietly(session); + } + } + +} diff --git a/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java new file mode 100644 index 00000000000..4862efb0382 --- /dev/null +++ b/sonar-core/src/main/java/org/sonar/core/properties/PropertiesMapper.java @@ -0,0 +1,30 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.properties; + +import java.util.List; + +import org.apache.ibatis.annotations.Param; + +public interface PropertiesMapper { + + List<String> findUserIdsForFavouriteResource(@Param("resource_id") Integer resourceId); + +} diff --git a/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml new file mode 100644 index 00000000000..21dc926f564 --- /dev/null +++ b/sonar-core/src/main/resources/org/sonar/core/properties/PropertiesMapper.xml @@ -0,0 +1,12 @@ +<?xml version="1.0" encoding="UTF-8" ?> +<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> + +<mapper namespace="org.sonar.core.properties.PropertiesMapper"> + + <select id="findUserIdsForFavouriteResource" parameterType="map" resultType="String"> + SELECT U.login + FROM properties P, users U + WHERE P.prop_key = 'favourite' AND P.resource_id = #{resource_id} AND P.user_id = U.id + </select> + +</mapper> diff --git a/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java new file mode 100644 index 00000000000..590c24404d0 --- /dev/null +++ b/sonar-core/src/test/java/org/sonar/core/properties/PropertiesDaoTest.java @@ -0,0 +1,48 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.core.properties; + +import static org.hamcrest.Matchers.hasItems; +import static org.hamcrest.Matchers.is; +import static org.junit.Assert.assertThat; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.core.persistence.DaoTestCase; + +public class PropertiesDaoTest extends DaoTestCase { + + private PropertiesDao dao; + + @Before + public void createDao() throws Exception { + dao = new PropertiesDao(getMyBatis()); + } + + @Test + public void shouldFindUserIdsForFavouriteResource() throws Exception { + setupData("shouldFindUserIdsForFavouriteResource"); + List<String> userIds = dao.findUserIdsForFavouriteResource(2); + assertThat(userIds.size(), is(2)); + assertThat(userIds, hasItems("user3", "user4")); + } +} diff --git a/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/shouldFindUserIdsForFavouriteResource.xml b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/shouldFindUserIdsForFavouriteResource.xml new file mode 100644 index 00000000000..9a5c1a00b6a --- /dev/null +++ b/sonar-core/src/test/resources/org/sonar/core/properties/PropertiesDaoTest/shouldFindUserIdsForFavouriteResource.xml @@ -0,0 +1,43 @@ +<dataset> + + <properties + id="1" + prop_key="sonar.core.id" + text_value="2.14" + resource_id="[null]" + user_id="[null]"/> + + <properties + id="2" + prop_key="favourite" + text_value="" + resource_id="1" + user_id="2"/> + + <properties + id="3" + prop_key="favourite" + text_value="" + resource_id="2" + user_id="3"/> + + <properties + id="4" + prop_key="favourite" + text_value="" + resource_id="2" + user_id="4"/> + + <users + id="2" + login="user2"/> + + <users + id="3" + login="user3"/> + + <users + id="4" + login="user4"/> + +</dataset> |