]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-8924 restrict count of projects on specified organization
authorSimon Brandhof <simon.brandhof@sonarsource.com>
Wed, 22 Mar 2017 14:12:52 +0000 (15:12 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 23 Mar 2017 16:38:34 +0000 (17:38 +0100)
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileProjectCount.java [deleted file]
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
server/sonar-server/src/main/java/org/sonar/server/qualityprofile/ws/SearchAction.java

index b06c1186917ee0e00e63f0a4f263e3b44cac8e8d..c4cc08a80ee32aed75de097b5245a06445166447 100644 (file)
@@ -23,7 +23,6 @@ import com.google.common.base.Preconditions;
 import com.google.common.collect.Lists;
 import java.util.Collection;
 import java.util.Date;
-import java.util.HashMap;
 import java.util.List;
 import java.util.Locale;
 import java.util.Map;
@@ -33,6 +32,7 @@ import org.sonar.api.utils.System2;
 import org.sonar.db.Dao;
 import org.sonar.db.DatabaseUtils;
 import org.sonar.db.DbSession;
+import org.sonar.db.KeyLongValue;
 import org.sonar.db.RowNotFoundException;
 import org.sonar.db.organization.OrganizationDto;
 
@@ -180,17 +180,8 @@ public class QualityProfileDao implements Dao {
     return mapper(dbSession).selectUuidsOfAssociatedProjects(profileKey);
   }
 
-  /**
-   * @deprecated provide organization
-   */
-  @Deprecated
-  public Map<String, Long> countProjectsByProfileKey(DbSession dbSession) {
-    Map<String, Long> countByKey = new HashMap<>();
-    QualityProfileMapper mapper = mapper(dbSession);
-    for (QualityProfileProjectCount count : mapper.countProjectsByProfile()) {
-      countByKey.put(count.getProfileKey(), count.getProjectCount());
-    }
-    return countByKey;
+  public Map<String, Long> countProjectsByProfileKey(DbSession dbSession, OrganizationDto organization) {
+    return KeyLongValue.toMap(mapper(dbSession).countProjectsByProfileKey(organization.getUuid()));
   }
 
   public void insertProjectProfileAssociation(String projectUuid, String profileKey, DbSession session) {
index f8b19c1fee6a9d5f3c2a10f24e3daf566fa598cd..b3c7f66f868e7d9954cdb84f6b147f2c57868b01 100644 (file)
@@ -23,6 +23,7 @@ import java.util.Collection;
 import java.util.List;
 import javax.annotation.CheckForNull;
 import org.apache.ibatis.annotations.Param;
+import org.sonar.db.KeyLongValue;
 
 public interface QualityProfileMapper {
 
@@ -67,7 +68,7 @@ public interface QualityProfileMapper {
 
   List<String> selectUuidsOfAssociatedProjects(@Param("profileKey") String profileKey);
 
-  List<QualityProfileProjectCount> countProjectsByProfile();
+  List<KeyLongValue> countProjectsByProfileKey(@Param("organizationUuid") String organizationUuid);
 
   QualityProfileDto selectByProjectAndLanguage(@Param("projectKey") String projectKey, @Param("language") String language);
 
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileProjectCount.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileProjectCount.java
deleted file mode 100644 (file)
index 9a42926..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2017 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program 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.
- *
- * This program 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 this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
- */
-package org.sonar.db.qualityprofile;
-
-public class QualityProfileProjectCount {
-
-  private String profileKey;
-  private Long projectCount;
-
-  public String getProfileKey() {
-    return profileKey;
-  }
-
-  public Long getProjectCount() {
-    return projectCount;
-  }
-}
index eb09fb0037cc9d6d8c41df9fa0cf521f96e214b1..e1297149508c1d0486b2371e0acf1655427a8267 100644 (file)
     ORDER BY pj.name ASC
   </select>
 
-  <select id="countProjectsByProfile" resultType="org.sonar.db.qualityprofile.QualityProfileProjectCount">
-    SELECT pp.profile_key as profileKey, count(projects.id) as projectCount
-    FROM projects projects
-    INNER JOIN project_qprofiles pp ON pp.project_uuid=projects.uuid
-    INNER JOIN rules_profiles prof ON pp.profile_key=prof.kee
-    WHERE projects.enabled=${_true}
-    GROUP BY pp.profile_key
+  <select id="countProjectsByProfileKey" resultType="KeyLongValue" parameterType="map">
+    select pp.profile_key as "key", count(projects.id) as "value"
+    from projects projects
+    inner join project_qprofiles pp ON pp.project_uuid = projects.uuid
+    inner join rules_profiles prof ON pp.profile_key = prof.kee
+    where projects.enabled = ${_true}
+    and prof.organization_uuid = #{organizationUuid, jdbcType=VARCHAR}
+    group by pp.profile_key
   </select>
 
   <select id="selectByProjectAndLanguage" parameterType="map" resultType="QualityProfile">
index 3c3862e2e94748791506f50df9703fbd4617a797..f5a808512fc3611b4a503d15068bcb48ec544174 100644 (file)
@@ -299,7 +299,7 @@ public class QualityProfileDaoTest {
     assertThat(dto.getLanguage()).isEqualTo("java");
     assertThat(dto.getParentKee()).isNull();
 
-    assertThat(underTest.selectById(dbTester.getSession(),555)).isNull();
+    assertThat(underTest.selectById(dbTester.getSession(), 555)).isNull();
   }
 
   @Test
@@ -340,12 +340,21 @@ public class QualityProfileDaoTest {
   }
 
   @Test
-  public void count_projects_by_profile() {
-    dbTester.prepareDbUnit(getClass(), "projects.xml");
-
-    assertThat(underTest.countProjectsByProfileKey(dbTester.getSession())).containsOnly(
-      MapEntry.entry("java_sonar_way", 2L),
-      MapEntry.entry("js_sonar_way", 2L));
+  public void countProjectsByProfileKey() {
+    QualityProfileDto profileWithoutProjects = dbTester.qualityProfiles().insert(organization);
+    QualityProfileDto profileWithProjects = dbTester.qualityProfiles().insert(organization);
+    ComponentDto project1 = dbTester.components().insertProject(organization);
+    ComponentDto project2 = dbTester.components().insertProject(organization);
+    dbTester.qualityProfiles().associateProjectWithQualityProfile(project1, profileWithProjects);
+    dbTester.qualityProfiles().associateProjectWithQualityProfile(project2, profileWithProjects);
+
+    OrganizationDto otherOrg = dbTester.organizations().insert();
+    QualityProfileDto profileInOtherOrg = dbTester.qualityProfiles().insert(otherOrg);
+    ComponentDto projectInOtherOrg = dbTester.components().insertProject(otherOrg);
+    dbTester.qualityProfiles().associateProjectWithQualityProfile(projectInOtherOrg, profileInOtherOrg);
+
+    assertThat(underTest.countProjectsByProfileKey(dbTester.getSession(), organization)).containsOnly(
+      MapEntry.entry(profileWithProjects.getKey(), 2L));
   }
 
   @Test
index 94c96edbc4631ff89d217ab1187d8b05dec59550..dfae9ba3ba179bbe0491f8cce70fc08e8d8c0f60 100644 (file)
@@ -132,7 +132,7 @@ public class SearchAction implements QProfileWsAction {
         .setProfiles(dataLoader.findProfiles(dbSession, request, organization))
         .setActiveRuleCountByProfileKey(dbClient.activeRuleDao().countActiveRulesByProfileKey(dbSession, organization))
         .setActiveDeprecatedRuleCountByProfileKey(dbClient.activeRuleDao().countActiveRulesForRuleStatusByProfileKey(dbSession, organization, RuleStatus.DEPRECATED))
-        .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey(dbSession));
+        .setProjectCountByProfileKey(dbClient.qualityProfileDao().countProjectsByProfileKey(dbSession, organization));
     }
   }