From 63c72abe86c9b5a9fa15a40406ede811128d075c Mon Sep 17 00:00:00 2001 From: =?utf8?q?L=C3=A9o=20Geoffroy?= <99647462+leo-geoffroy-sonarsource@users.noreply.github.com> Date: Wed, 27 Apr 2022 10:53:10 +0200 Subject: [PATCH] SONAR-16302 add management of indexing for rules description sections --- .../db/rule/RuleDescriptionSectionDto.java | 18 +++++ .../org/sonar/db/rule/RuleForIndexingDto.java | 41 ++++++++-- .../org/sonar/db/rule/RuleMapper.xml | 74 +++++++++++++------ .../java/org/sonar/db/rule/RuleDaoTest.java | 19 +++-- .../server/rule/HotspotRuleDescription.java | 2 +- .../server/rule/RuleDescriptionFormatter.java | 27 +++++-- .../org/sonar/server/rule/index/RuleDoc.java | 10 +-- .../rule/RuleDescriptionFormatterTest.java | 40 ++++++++++ 8 files changed, 186 insertions(+), 45 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java index 76dc0684d9e..444f0e64522 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleDescriptionSectionDto.java @@ -19,6 +19,7 @@ */ package org.sonar.db.rule; +import java.util.Objects; import java.util.StringJoiner; import static org.sonar.api.utils.Preconditions.checkArgument; @@ -73,6 +74,23 @@ public class RuleDescriptionSectionDto { .toString(); } + @Override + public boolean equals(Object o) { + if (this == o) { + return true; + } + if (o == null || getClass() != o.getClass()) { + return false; + } + RuleDescriptionSectionDto that = (RuleDescriptionSectionDto) o; + return Objects.equals(uuid, that.uuid) && Objects.equals(key, that.key) && Objects.equals(description, that.description); + } + + @Override + public int hashCode() { + return Objects.hash(uuid, key, description); + } + public static final class RuleDescriptionSectionDtoBuilder { private String uuid; private String key = null; diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java index afb8a4af448..58b88632d1a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/rule/RuleForIndexingDto.java @@ -19,19 +19,23 @@ */ package org.sonar.db.rule; +import java.util.HashSet; +import java.util.List; +import java.util.Optional; import java.util.Set; import javax.annotation.CheckForNull; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; import org.sonar.api.rules.RuleType; +import static org.sonar.db.rule.RuleDescriptionSectionDto.DEFAULT_KEY; + public class RuleForIndexingDto { private String uuid; private String repository; private String pluginRuleKey; private String name; - private String description; private RuleDto.Format descriptionFormat; private Integer severity; private RuleStatus status; @@ -48,6 +52,8 @@ public class RuleForIndexingDto { private long createdAt; private long updatedAt; + private Set ruleDescriptionSectionsDtos = new HashSet<>(); + public RuleForIndexingDto() { // nothing to do here } @@ -60,22 +66,30 @@ public class RuleForIndexingDto { return repository; } + public void setRepository(String repository) { + this.repository = repository; + } + public String getPluginRuleKey() { return pluginRuleKey; } - public String getName() { - return name; + public void setPluginRuleKey(String pluginRuleKey) { + this.pluginRuleKey = pluginRuleKey; } - public String getDescription() { - return description; + public String getName() { + return name; } public RuleDto.Format getDescriptionFormat() { return descriptionFormat; } + public void setDescriptionFormat(RuleDto.Format descriptionFormat) { + this.descriptionFormat = descriptionFormat; + } + public Integer getSeverity() { return severity; } @@ -144,4 +158,21 @@ public class RuleForIndexingDto { public RuleKey getRuleKey() { return RuleKey.of(repository, pluginRuleKey); } + + public Set getRuleDescriptionSectionsDtos() { + return ruleDescriptionSectionsDtos; + } + + public void setRuleDescriptionSectionsDtos(Set ruleDescriptionSectionsDtos) { + this.ruleDescriptionSectionsDtos = ruleDescriptionSectionsDtos; + } + + private Optional findExistingSectionWithSameKey(String ruleDescriptionSectionKey) { + return ruleDescriptionSectionsDtos.stream().filter(section -> section.getKey().equals(ruleDescriptionSectionKey)).findAny(); + } + + @CheckForNull + public RuleDescriptionSectionDto getDefaultRuleDescriptionSectionDto() { + return findExistingSectionWithSameKey(DEFAULT_KEY).orElse(null); + } } 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 0188bf511d8..bef6fde79b5 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 @@ -83,22 +83,22 @@ - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + @@ -165,6 +165,33 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + - where @@ -370,10 +397,10 @@ order by r.created_at asc - - order by r.created_at asc + order by r.created_at,r.uuid asc @@ -396,10 +423,15 @@ r.rule_type as "type", r.created_at as "createdAt", r.updated_at as "updatedAt", - rm.tags as "tags" + rm.tags as "tags", + rds.uuid as "rds_uuid", + rds.kee as "rds_kee", + rds.description as "rds_description" from rules r left outer join rules t on t.uuid = r.template_uuid left outer join rules_metadata rm on r.uuid = rm.rule_uuid + left outer join rule_desc_sections rds on + rds.rule_uuid = r.uuid