diff options
author | Pierre Guillot <pierre.guillot@sonarsource.com> | 2024-12-10 15:29:09 +0100 |
---|---|---|
committer | Steve Marion <steve.marion@sonarsource.com> | 2024-12-18 11:13:21 +0100 |
commit | d639a965bce7acafb004906cd07a8f0b5f7af993 (patch) | |
tree | 647cd646abddb12dfeeef7e637aa33b4658f1049 /server/sonar-db-dao/src/main | |
parent | 451c1c2e4856ec3df87f86189fcdb25b31794027 (diff) | |
download | sonarqube-d639a965bce7acafb004906cd07a8f0b5f7af993.tar.gz sonarqube-d639a965bce7acafb004906cd07a8f0b5f7af993.zip |
SONAR-22998 fetch active rules with a dedicated endpoint
Co-authored-by: Julien HENRY <julien.henry@sonarsource.com>
Diffstat (limited to 'server/sonar-db-dao/src/main')
6 files changed, 104 insertions, 18 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java index e085a3cc315..76c9ae977ef 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDao.java @@ -84,8 +84,15 @@ public class ActiveRuleDao implements Dao { /** * Active rule on removed rule are NOT returned */ - public List<OrgActiveRuleDto> selectByProfileUuid(DbSession dbSession, String uuid) { - return mapper(dbSession).selectByProfileUuid(uuid); + public List<OrgActiveRuleDto> selectByProfileUuid(DbSession dbSession, String profileUuid) { + return selectByProfileUuids(dbSession, List.of(profileUuid)); + } + + /** + * Active rule on removed rule are NOT returned + */ + public List<OrgActiveRuleDto> selectByProfileUuids(DbSession dbSession, Collection<String> profileUuids) { + return mapper(dbSession).selectByProfileUuids(profileUuids); } public List<OrgActiveRuleDto> selectByTypeAndProfileUuids(DbSession dbSession, List<Integer> types, List<String> uuids) { @@ -162,6 +169,10 @@ public class ActiveRuleDao implements Dao { return executeLargeInputs(activeRuleUuids, mapper(dbSession)::selectParamsByActiveRuleUuids); } + public List<ActiveRuleParamDto> selectAllParamsByProfileUuids(final DbSession dbSession, Collection<String> profileUuids) { + return mapper(dbSession).selectAllParamsByProfileUuids(profileUuids); + } + public ActiveRuleParamDto insertParam(DbSession dbSession, ActiveRuleDto activeRule, ActiveRuleParamDto activeRuleParam) { checkArgument(activeRule.getUuid() != null, ACTIVE_RULE_IS_NOT_PERSISTED); checkArgument(activeRuleParam.getUuid() == null, ACTIVE_RULE_PARAM_IS_ALREADY_PERSISTED); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java index 906cdb4a2c7..483ed6e34a9 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/qualityprofile/ActiveRuleDto.java @@ -58,10 +58,14 @@ public class ActiveRuleDto { // These fields do not exist in db, it's only retrieve by joins private String repository; private String ruleField; - private String ruleProfileUuid; private String securityStandards; private boolean isExternal; + private String name; + private String configKey; + private String templateUuid; + private String language; + public ActiveRuleDto() { // nothing to do here } @@ -69,12 +73,12 @@ public class ActiveRuleDto { public ActiveRuleDto setKey(ActiveRuleKey key) { this.repository = key.getRuleKey().repository(); this.ruleField = key.getRuleKey().rule(); - this.ruleProfileUuid = key.getRuleProfileUuid(); + this.profileUuid = key.getRuleProfileUuid(); return this; } public ActiveRuleKey getKey() { - return new ActiveRuleKey(ruleProfileUuid, RuleKey.of(repository, ruleField)); + return new ActiveRuleKey(profileUuid, RuleKey.of(repository, ruleField)); } public RuleKey getRuleKey() { @@ -207,6 +211,43 @@ public class ActiveRuleDto { return this; } + public String getName() { + return name; + } + + public ActiveRuleDto setName(String name) { + this.name = name; + return this; + } + + public String getConfigKey() { + return configKey; + } + + public ActiveRuleDto setConfigKey(String configKey) { + this.configKey = configKey; + return this; + } + + @CheckForNull + public String getTemplateUuid() { + return templateUuid; + } + + public ActiveRuleDto setTemplateUuid(@Nullable String templateUuid) { + this.templateUuid = templateUuid; + return this; + } + + public String getLanguage() { + return language; + } + + public ActiveRuleDto setLanguage(String language) { + this.language = language; + return this; + } + public static ActiveRuleDto createFor(QProfileDto profile, RuleDto ruleDto) { requireNonNull(profile.getRulesProfileUuid(), "Profile is not persisted"); requireNonNull(ruleDto.getUuid(), "Rule is not persisted"); @@ -214,6 +255,10 @@ public class ActiveRuleDto { dto.setProfileUuid(profile.getRulesProfileUuid()); dto.setRuleUuid(ruleDto.getUuid()); dto.setKey(ActiveRuleKey.of(profile, ruleDto.getKey())); + dto.setName(ruleDto.getName()); + dto.setConfigKey(ruleDto.getConfigKey()); + dto.setTemplateUuid(ruleDto.getTemplateUuid()); + dto.setLanguage(ruleDto.getLanguage()); dto.setImpacts(ruleDto.getDefaultImpactsMap()); return dto; } 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 d2b2dddfdbe..af2d2dcaa98 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 @@ -52,7 +52,7 @@ public interface ActiveRuleMapper { List<OrgActiveRuleDto> selectByRuleUuids(@Param("ruleUuids") List<String> partitionOfRuleUuids); - List<OrgActiveRuleDto> selectByProfileUuid(String uuid); + List<OrgActiveRuleDto> selectByProfileUuids(@Param("profileUuids") Collection<String> profileUuids); List<OrgActiveRuleDto> selectByTypeAndProfileUuids(@Param("types") List<Integer> types, @Param("profileUuids") List<String> uuids); @@ -90,4 +90,6 @@ public interface ActiveRuleMapper { void scrollByRuleProfileUuidForIndexing(@Param("ruleProfileUuid") String ruleProfileUuid, ResultHandler<IndexedActiveRuleDto> handler); int countMissingRules(@Param("rulesProfileUuid") String rulesProfileUuid, @Param("compareToRulesProfileUuid") String compareToRulesProfileUuid); + + List<ActiveRuleParamDto> selectAllParamsByProfileUuids(@Param("profileUuids") Collection<String> profileUuids); } 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 f9d0982e4b4..34d9bd3ea71 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 @@ -168,7 +168,11 @@ public class QualityProfileDao implements Dao { } public List<QProfileDto> selectAssociatedToProjectAndLanguages(DbSession dbSession, ProjectDto project, Collection<String> languages) { - return executeLargeInputs(languages, partition -> mapper(dbSession).selectAssociatedToProjectUuidAndLanguages(project.getUuid(), partition)); + return selectAssociatedToProjectAndLanguages(dbSession, project.getUuid(), languages); + } + + public List<QProfileDto> selectAssociatedToProjectAndLanguages(DbSession dbSession, String projectUuid, Collection<String> languages) { + return executeLargeInputs(languages, partition -> mapper(dbSession).selectAssociatedToProjectUuidAndLanguages(projectUuid, partition)); } public List<QProfileDto> selectQProfilesByProjectUuid(DbSession dbSession, String projectUuid) { @@ -251,6 +255,7 @@ public class QualityProfileDao implements Dao { public List<ProjectQProfileLanguageAssociationDto> selectAllProjectAssociations(DbSession dbSession) { return mapper(dbSession).selectAllProjectAssociations(); } + public Collection<String> selectUuidsOfCustomRulesProfiles(DbSession dbSession, String language, String name) { return mapper(dbSession).selectUuidsOfCustomRuleProfiles(language, name); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java index 6d1f544e076..722f131695b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDto.java @@ -72,7 +72,7 @@ public class RuleDto { private RuleDto.Format descriptionFormat = null; private RuleStatus status = null; - private Set<ImpactDto> defaultImpacts = new HashSet<>(); + private final Set<ImpactDto> defaultImpacts = new HashSet<>(); private String name = null; private String configKey = null; @@ -375,7 +375,6 @@ public class RuleDto { return this; } - @CheckForNull public String getLanguage() { return language; } @@ -650,13 +649,12 @@ public class RuleDto { @Override public boolean equals(Object obj) { - if (!(obj instanceof RuleDto)) { + if (!(obj instanceof RuleDto other)) { return false; } if (this == obj) { return true; } - RuleDto other = (RuleDto) obj; return Objects.equals(this.uuid, other.uuid); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml index 10442858528..c6facb12ff6 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/qualityprofile/ActiveRuleMapper.xml @@ -26,14 +26,19 @@ a.inheritance as "inheritance", a.impacts as "impactsString", a.prioritized_rule as "prioritizedRule", + a.created_at as "createdAt", + a.updated_at as "updatedAt", + r.plugin_rule_key as "rulefield", r.plugin_name as "repository", r.security_standards as "securityStandards", - rp.uuid as "ruleProfileUuid", - a.created_at as "createdAt", - a.updated_at as "updatedAt", - oqp.uuid as "orgProfileUuid", - r.is_external as "isExternal" + r.is_external as "isExternal", + r.name as "name", + r.plugin_config_key as "configKey", + r.template_uuid as "templateUuid", + r.language as "language", + + oqp.uuid as "orgProfileUuid" </sql> <sql id="activeRuleKeyJoin"> @@ -113,14 +118,18 @@ and r.plugin_name = #{repository, jdbcType=VARCHAR} </select> - <select id="selectByProfileUuid" parameterType="string" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto"> + <select id="selectByProfileUuids" parameterType="string" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto"> select <include refid="orgActiveRuleColumns"/> from active_rules a inner join rules_profiles rp on rp.uuid = a.profile_uuid inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid inner join rules r on r.uuid = a.rule_uuid and r.status != 'REMOVED' - where oqp.uuid = #{id, jdbcType=VARCHAR} + where + oqp.uuid in + <foreach collection="profileUuids" item="profileUuid" separator="," open="(" close=")"> + #{profileUuid, jdbcType=VARCHAR} + </foreach> </select> <select id="selectByTypeAndProfileUuids" parameterType="Map" resultType="org.sonar.db.qualityprofile.OrgActiveRuleDto"> @@ -306,6 +315,22 @@ </where> </select> + <select id="selectAllParamsByProfileUuids" parameterType="map" resultType="ActiveRuleParam"> + select + <include refid="activeRuleParamColumns"/> + from active_rule_parameters p + inner join active_rules ar on ar.uuid = p.active_rule_uuid + inner join rules_profiles rp on rp.uuid = ar.profile_uuid + inner join org_qprofiles oqp on oqp.rules_profile_uuid = rp.uuid + inner join rules r on r.uuid = ar.rule_uuid + <where> + <foreach collection="profileUuids" item="profileUuid" open="(" separator=" or " close=")"> + oqp.uuid = #{profileUuid, jdbcType=VARCHAR} + </foreach> + and r.status != 'REMOVED' + </where> + </select> + <select id="countActiveRulesByQuery" resultType="KeyLongValue" parameterType="map"> select oqp.uuid as "key", count(ar.uuid) as "value" from active_rules ar |