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);
}
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;
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)));
+++ /dev/null
-/*
- * 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);
- }
-}
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,
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);
}
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);
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
<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,
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;
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();
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(