From: Klaudio Sinani Date: Thu, 2 Jun 2022 10:16:57 +0000 (+0200) Subject: SONAR-16316 Include inactive rules to quality profile findings CSV file X-Git-Tag: 9.5.0.56709~52 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=11c8360024fe1d575276b2b7e1245fd125b8e12f;p=sonarqube.git SONAR-16316 Include inactive rules to quality profile findings CSV file --- diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java index e6640850b43..968adaa370f 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleMapper.java @@ -85,4 +85,6 @@ public interface ActiveRuleMapper { void scrollByUuidsForIndexing(@Param("uuids") Collection uuids, ResultHandler handler); void scrollByRuleProfileUuidForIndexing(@Param("ruleProfileUuid") String ruleProfileUuid, ResultHandler handler); + + List selectByQualityProfileUuid(@Param("qualityProfileUuid") String qualityProfileUuid); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java index eab5232a9c2..2948705b2a9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileDao.java @@ -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 handler) { - mapper(dbSession).selectQualityProfileFindings(qualityProfileUuid, handler); - } - public Map countProjectsByProfiles(DbSession dbSession, List profiles) { List 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 index 64af7bd6605..00000000000 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileFindingDto.java +++ /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); - } -} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java index 3b3333f4894..cf5e8480f94 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/QualityProfileMapper.java @@ -94,10 +94,6 @@ public interface QualityProfileMapper { List selectQProfileUuidsByProjectUuid(@Param("projectUuid") String projectUuid); - void selectQualityProfileFindings( - @Param("qualityProfileUuid") String qualityProfileUuid, - @Param("handler") ResultHandler handler); - void insertProjectProfileAssociation( @Param("uuid") String uuid, @Param("projectUuid") String projectUuid, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java index e243bca7cdd..59221b6d715 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDao.java @@ -90,6 +90,10 @@ public class RuleDao implements Dao { return executeLargeInputs(languages, chunk -> mapper(session).selectByTypeAndLanguages(types, chunk)); } + public List selectByLanguage(DbSession session, String language) { + return mapper(session).selectByLanguage(language); + } + public List selectByQuery(DbSession session, RuleQuery ruleQuery) { return mapper(session).selectByQuery(ruleQuery); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java index 6c7ca31c2ff..4ec5ab131d6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleMapper.java @@ -46,6 +46,8 @@ public interface RuleMapper { List selectByTypeAndLanguages(@Param("types") List types, @Param("languages") List languages); + List selectByLanguage(@Param("language") String language); + void insertRule(RuleDto ruleDefinitionDto); void insertRuleDescriptionSection(@Param("ruleUuid") String ruleUuid, @Param("dto") RuleDescriptionSectionDto ruleDescriptionSectionDto); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml index c5ca6a87473..db706ae4d65 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/QualityProfileMapper.xml @@ -23,17 +23,6 @@ rp.is_built_in as isBuiltIn - - 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 - - insert into rules_profiles ( uuid, @@ -381,15 +370,6 @@ and rp.is_built_in = ${_false} - - update rules_profiles set diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml index cf7ed03865c..cc70d9a3a3f 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/rule/RuleMapper.xml @@ -3,17 +3,7 @@ - - left outer join rule_desc_sections rds on - rds.rule_uuid = r.uuid - - - - rds.content as "rds_content", - rds.uuid as "rds_uuid", - rds.kee as "rds_kee", - - r.uuid as "r_uuid", + r.plugin_rule_key as "ruleKey", r.plugin_name as "repositoryKey", r.description_format as "descriptionFormat", @@ -51,6 +41,20 @@ r.ad_hoc_type as "adHocType" + + left outer join rule_desc_sections rds on + rds.rule_uuid = r.uuid + + + + rds.content as "rds_content", + rds.uuid as "rds_uuid", + rds.kee as "rds_kee", + r.uuid as "r_uuid", + + + + + + insert into rule_desc_sections ( uuid, diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java index c4b3fc210ca..2ea5e44292c 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/qualityprofile/QualityProfileDaoTest.java @@ -20,32 +20,25 @@ 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 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(); diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java index dc4eb6fc52e..881ebde36e0 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleDaoTest.java @@ -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(