]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16316 Include inactive rules to quality profile findings CSV file
authorKlaudio Sinani <klaudio.sinani@sonarsource.com>
Thu, 2 Jun 2022 10:16:57 +0000 (12:16 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 2 Jun 2022 20:03:17 +0000 (20:03 +0000)
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileFindingDto.java [deleted file]
server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml
server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java

index e6640850b437509551dd4c53843e7bb38c7e694e..968adaa370f81c0038bf7d1be3e08ea4d22313ea 100644 (file)
@@ -85,4 +85,6 @@ public interface ActiveRuleMapper {
   void scrollByUuidsForIndexing(@Param("uuids") Collection<String> uuids, ResultHandler<IndexedActiveRuleDto> handler);
 
   void scrollByRuleProfileUuidForIndexing(@Param("ruleProfileUuid") String ruleProfileUuid, ResultHandler<IndexedActiveRuleDto> handler);
+
+  List<OrgActiveRuleDto> selectByQualityProfileUuid(@Param("qualityProfileUuid") String qualityProfileUuid);
 }
index eab5232a9c24a6d597640c3b889b06d4d019c300..2948705b2a910b5516f75f452fce3f5ae06b7cc0 100644 (file)
@@ -28,7 +28,6 @@ import java.util.Map;
 import java.util.Set;
 import javax.annotation.CheckForNull;
 import javax.annotation.Nullable;
-import org.apache.ibatis.session.ResultHandler;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.UuidFactory;
 import org.sonar.core.util.stream.MoreCollectors;
@@ -208,10 +207,6 @@ public class QualityProfileDao implements Dao {
     return mapper(dbSession).selectByNameAndLanguages(name, languages);
   }
 
-  public void selectQualityProfileFindings(DbSession dbSession, String qualityProfileUuid, ResultHandler<QualityProfileFindingDto> handler) {
-    mapper(dbSession).selectQualityProfileFindings(qualityProfileUuid, handler);
-  }
-
   public Map<String, Long> countProjectsByProfiles(DbSession dbSession, List<QProfileDto> profiles) {
     List<String> profileUuids = profiles.stream().map(QProfileDto::getKee).collect(MoreCollectors.toList());
     return KeyLongValue.toMap(executeLargeInputs(profileUuids, partition -> mapper(dbSession).countProjectsByProfiles(partition)));
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileFindingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileFindingDto.java
deleted file mode 100644 (file)
index 64af7bd..0000000
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * SonarQube
- * Copyright (C) 2009-2022 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;
-
-import javax.annotation.CheckForNull;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rule.RuleStatus;
-import org.sonar.api.rules.RuleType;
-import org.sonar.db.rule.SeverityUtil;
-
-public class QualityProfileFindingDto {
-  private String language = null;
-  private String title = null;
-  private String repository = null;
-  private String ruleKey = null;
-  private String status = null;
-  private Integer type = null;
-  private Integer severity = null;
-
-  public String getLanguage() {
-    return language;
-  }
-
-  public String getTitle() {
-    return title;
-  }
-
-  public RuleKey getReferenceKey() {
-    return RuleKey.of(repository, ruleKey);
-  }
-
-  public RuleStatus getStatus() {
-    return RuleStatus.valueOf(status);
-  }
-
-  public RuleType getType() {
-    return RuleType.valueOf(type);
-  }
-
-  @CheckForNull
-  public String getSeverity() {
-    return SeverityUtil.getSeverityFromOrdinal(severity);
-  }
-}
index 3b3333f48943eef0350006717bc89fc0f237e2c9..cf5e8480f94c0b2cc90621744417ad541b31b48f 100644 (file)
@@ -94,10 +94,6 @@ public interface QualityProfileMapper {
 
   List<String> selectQProfileUuidsByProjectUuid(@Param("projectUuid") String projectUuid);
 
-  void selectQualityProfileFindings(
-    @Param("qualityProfileUuid") String qualityProfileUuid,
-    @Param("handler") ResultHandler<QualityProfileFindingDto> handler);
-
   void insertProjectProfileAssociation(
     @Param("uuid") String uuid,
     @Param("projectUuid") String projectUuid,
index e243bca7cdd84a0428e302352e1b7524b9bdd1eb..59221b6d7157f2f0ac85e19ba6a1d100dac6d45f 100644 (file)
@@ -90,6 +90,10 @@ public class RuleDao implements Dao {
     return executeLargeInputs(languages, chunk -> mapper(session).selectByTypeAndLanguages(types, chunk));
   }
 
+  public List<RuleDto> selectByLanguage(DbSession session, String language) {
+    return mapper(session).selectByLanguage(language);
+  }
+
   public List<RuleDto> selectByQuery(DbSession session, RuleQuery ruleQuery) {
     return mapper(session).selectByQuery(ruleQuery);
   }
index 6c7ca31c2ff252ca6f8562a82720a04595a34bdd..4ec5ab131d6ca274fc844e790c352bc83a51273f 100644 (file)
@@ -46,6 +46,8 @@ public interface RuleMapper {
 
   List<RuleDto> selectByTypeAndLanguages(@Param("types") List<Integer> types, @Param("languages") List<String> languages);
 
+  List<RuleDto> selectByLanguage(@Param("language") String language);
+
   void insertRule(RuleDto ruleDefinitionDto);
 
   void insertRuleDescriptionSection(@Param("ruleUuid") String ruleUuid, @Param("dto") RuleDescriptionSectionDto ruleDescriptionSectionDto);
index c5ca6a87473d51eb4d93a3fb707e858bb61230d3..db706ae4d65b09e4cbad56ff3a09721fee176cfc 100644 (file)
     rp.is_built_in as isBuiltIn
   </sql>
 
-  <sql id="qualityProfileFindingColumns">
-    r.language as language,
-    r.name as title,
-    r.plugin_name as repository,
-    r.plugin_rule_key as ruleKey,
-    r.status as status,
-    r.rule_type as type,
-    r.priority as defaultSeverity,
-    ar.failure_level as severity
-  </sql>
-
   <insert id="insertRuleProfile" parameterType="map" useGeneratedKeys="false">
     insert into rules_profiles (
       uuid,
       and rp.is_built_in = ${_false}
   </select>
 
-  <select id="selectQualityProfileFindings" parameterType="String" resultType="org.sonar.db.qualityprofile.QualityProfileFindingDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
-    select
-    <include refid="qualityProfileFindingColumns"/>
-    from rules r
-    left join active_rules ar on ar.rule_uuid = r.uuid
-    inner join org_qprofiles oqp on oqp.rules_profile_uuid = ar.profile_uuid
-    where oqp.uuid=#{qualityProfileUuid,jdbcType=VARCHAR}
-  </select>
-
   <update id="renameRuleProfiles" parameterType="map">
     update rules_profiles
     set
index cf7ed03865ccb4dfde750efb4db168e797528c4e..cc70d9a3a3f282d6be24b61904029da449c11a15 100644 (file)
@@ -3,17 +3,7 @@
 
 <mapper namespace="org.sonar.db.rule.RuleMapper">
 
-  <sql id="leftOuterJoinRulesDescriptionSections">
-    left outer join rule_desc_sections rds on
-      rds.rule_uuid = r.uuid
-  </sql>
-
-  <sql id="selectJoinedTablesColumns">
-    rds.content as "rds_content",
-    rds.uuid as "rds_uuid",
-    rds.kee as "rds_kee",
-
-    r.uuid as "r_uuid",
+  <sql id="ruleColumns">
     r.plugin_rule_key as "ruleKey",
     r.plugin_name as "repositoryKey",
     r.description_format as "descriptionFormat",
     r.ad_hoc_type as "adHocType"
   </sql>
 
+  <sql id="leftOuterJoinRulesDescriptionSections">
+    left outer join rule_desc_sections rds on
+      rds.rule_uuid = r.uuid
+  </sql>
+
+  <sql id="selectJoinedTablesColumns">
+    rds.content as "rds_content",
+    rds.uuid as "rds_uuid",
+    rds.kee as "rds_kee",
+    r.uuid as "r_uuid",
+
+    <include refid="ruleColumns"/>
+  </sql>
+
   <select id="selectAll" resultMap="ruleResultMap">
     select
       <include refid="selectJoinedTablesColumns"/>
     <foreach collection="languages" item="language" separator="," open="(" close=")">#{language, jdbcType=VARCHAR}</foreach>
   </select>
 
+  <select id="selectByLanguage" parameterType="String" resultType="org.sonar.db.rule.RuleDto" fetchSize="${_scrollFetchSize}" resultSetType="FORWARD_ONLY">
+    select
+      r.uuid as uuid,
+      <include refid="ruleColumns"/>
+    from
+      rules r
+    where
+      r.status in ('READY', 'DEPRECATED')
+      and r.is_external=${_false}
+      and r.language=#{language, jdbcType=VARCHAR}
+  </select>
+
   <insert id="insertRuleDescriptionSection" parameterType="Map" useGeneratedKeys="false">
     insert into rule_desc_sections (
       uuid,
index c4b3fc210ca41807dd170fd91ebf20c6a0964c49..2ea5e44292cc041a18b54147bcc45b979cc44e6d 100644 (file)
 package org.sonar.db.qualityprofile;
 
 import com.google.common.collect.Sets;
-import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
-import java.util.stream.Collectors;
 import java.util.stream.IntStream;
 import org.assertj.core.data.MapEntry;
 import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
-import org.sonar.api.rule.RuleKey;
-import org.sonar.api.rules.RuleType;
 import org.sonar.api.utils.System2;
 import org.sonar.core.util.UtcDateUtils;
 import org.sonar.core.util.Uuids;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
-import org.sonar.db.component.BranchDto;
-import org.sonar.db.component.BranchType;
 import org.sonar.db.component.ComponentDto;
 import org.sonar.db.project.ProjectDto;
 import org.sonar.db.rule.RuleDto;
-import org.sonar.db.rule.SeverityUtil;
 
 import static com.google.common.collect.ImmutableList.of;
 import static com.google.common.collect.Lists.newArrayList;
@@ -406,55 +399,6 @@ public class QualityProfileDaoTest {
     assertThat(result.getRulesProfileUuid()).isEqualTo(profile.getRulesProfileUuid());
   }
 
-  @Test
-  public void selectQualityProfileFindings_returns_all_quality_profile_details_for_project() {
-    String projectUuid = "project_uuid";
-    String projectKey = "project_key";
-    String branchUuid = "branch_uuid";
-    String branchName = "branch";
-
-    BranchDto branch = new BranchDto()
-      .setBranchType(BranchType.BRANCH)
-      .setKey(branchName)
-      .setUuid(branchUuid)
-      .setProjectUuid(projectUuid);
-
-    db.getDbClient().branchDao().insert(db.getSession(), branch);
-
-    ProjectDto project = db.components().insertPublicProjectDto(t -> t.setProjectUuid(projectUuid)
-      .setUuid(projectUuid)
-      .setDbKey(projectKey)
-      .setMainBranchProjectUuid(branchUuid));
-
-    QProfileDto cppQPWithoutActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("cpp"));
-    db.qualityProfiles().setAsDefault(cppQPWithoutActiveRules);
-
-    QProfileDto javaBuiltInQPWithActiveRules = db.qualityProfiles().insert(qp -> qp.setIsBuiltIn(true).setLanguage("java"));
-    RuleDto rule1 = db.rules().insert(r -> r.setName("rule 1 title"));
-    ActiveRuleDto activeRule1 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule1);
-    RuleDto rule2 = db.rules().insert(r -> r.setName("rule 2 title"));
-    ActiveRuleDto activeRule2 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule2);
-    RuleDto rule3 = db.rules().insert(r -> r.setName("rule 3 title"));
-    ActiveRuleDto activeRule3 = db.qualityProfiles().activateRule(javaBuiltInQPWithActiveRules, rule3);
-
-    db.qualityProfiles().associateWithProject(project, cppQPWithoutActiveRules, javaBuiltInQPWithActiveRules);
-    db.getSession().commit();
-
-    List<QualityProfileFindingDto> findings = new ArrayList<>();
-    underTest.selectQualityProfileFindings(db.getSession(), javaBuiltInQPWithActiveRules.getKee(), result -> findings.add(result.getResultObject()));
-
-    QualityProfileFindingDto finding = findings.stream().filter(f -> f.getTitle().equals("rule 1 title")).findFirst().get();
-
-    assertThat(findings).hasSize(3);
-    assertThat(findings.stream().map(f -> f.getTitle()).collect(Collectors.toSet())).containsExactlyInAnyOrder("rule 1 title", "rule 2 title", "rule 3 title");
-    assertThat(finding.getLanguage()).isEqualTo(rule1.getLanguage());
-    assertThat(finding.getTitle()).isEqualTo(rule1.getName());
-    assertThat(finding.getReferenceKey()).isEqualTo(RuleKey.of(rule1.getRepositoryKey(), rule1.getRuleKey()));
-    assertThat(finding.getStatus()).isEqualTo(rule1.getStatus());
-    assertThat(finding.getType()).isEqualTo(RuleType.valueOf(rule1.getType()));
-    assertThat(finding.getSeverity()).isEqualTo(SeverityUtil.getSeverityFromOrdinal(activeRule1.getSeverity()));
-  }
-
   @Test
   public void should_not_selectByLanguage_with_wrong_language() {
     QProfileDto profile = QualityProfileTesting.newQualityProfileDto();
index dc4eb6fc52ecdd520d90170c43daf32d448f395d..881ebde36e01db43dba5252a40d4d174a063e529 100644 (file)
@@ -287,6 +287,39 @@ public class RuleDaoTest {
     assertThat(underTest.selectByTypeAndLanguages(db.getSession(), singletonList(RuleType.VULNERABILITY.getDbConstant()), singletonList("js"))).isEmpty();
   }
 
+  @Test
+  public void selectByLanguage() {
+    RuleDto rule1 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("java", "S001"))
+        .setType(RuleType.VULNERABILITY)
+        .setLanguage("java"));
+
+    RuleDto rule2 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("js", "S002"))
+        .setType(RuleType.SECURITY_HOTSPOT)
+        .setLanguage("js"));
+
+    RuleDto rule3 = db.rules().insert(
+      r -> r.setKey(RuleKey.of("java", "S003"))
+        .setType(RuleType.BUG)
+        .setLanguage("java"));
+
+    assertThat(underTest.selectByLanguage(db.getSession(), "java")).hasSize(2);
+
+    assertThat(underTest.selectByLanguage(db.getSession(), "java"))
+      .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
+      .containsExactlyInAnyOrder(
+        tuple(rule1.getUuid(), "java", RuleType.VULNERABILITY.getDbConstant()),
+        tuple(rule3.getUuid(), "java", RuleType.BUG.getDbConstant())
+      );
+
+    assertThat(underTest.selectByLanguage(db.getSession(), "js")).hasSize(1);
+
+    assertThat(underTest.selectByLanguage(db.getSession(), "js"))
+      .extracting(RuleDto::getUuid, RuleDto::getLanguage, RuleDto::getType)
+      .containsExactly(tuple(rule2.getUuid(), "js", RuleType.SECURITY_HOTSPOT.getDbConstant()));
+  }
+
   @Test
   public void selectByTypeAndLanguages_return_nothing_when_no_rule_on_languages() {
     db.rules().insert(