diff options
author | Léo Geoffroy <99647462+leo-geoffroy-sonarsource@users.noreply.github.com> | 2022-04-22 14:13:59 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-05-06 20:02:43 +0000 |
commit | 6e2ed638eeb353c4048575e9ba16c968455d3142 (patch) | |
tree | 8cc600e82076e13a06b8124d8101884711316fe7 /server/sonar-webserver-core | |
parent | 988512fbbf60d11ec53c2add9a277f4c04c163f8 (diff) | |
download | sonarqube-6e2ed638eeb353c4048575e9ba16c968455d3142.tar.gz sonarqube-6e2ed638eeb353c4048575e9ba16c968455d3142.zip |
SONAR-16302 adapt RuleDto, RuleDefinitionDto and introduce RuleSectionDescriptionDto to prepare for multiple section support
Diffstat (limited to 'server/sonar-webserver-core')
-rw-r--r-- | server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java | 43 | ||||
-rw-r--r-- | server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java | 28 |
2 files changed, 42 insertions, 29 deletions
diff --git a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java index 5309cbb697f..fb4e08ec15a 100644 --- a/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java +++ b/server/sonar-webserver-core/src/main/java/org/sonar/server/rule/RegisterRules.java @@ -75,9 +75,11 @@ import static java.lang.String.format; import static java.util.Collections.emptyList; import static java.util.Collections.emptySet; import static java.util.Collections.unmodifiableMap; +import static org.apache.commons.lang.StringUtils.isNotEmpty; import static org.sonar.core.util.stream.MoreCollectors.toList; import static org.sonar.core.util.stream.MoreCollectors.toSet; import static org.sonar.core.util.stream.MoreCollectors.uniqueIndex; +import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection; /** * Register rules at server startup @@ -261,10 +263,10 @@ public class RegisterRules implements Startable { private Stream<RuleDefinitionDto> getAllModified() { return Stream.of( - created.stream(), - updated.stream(), - removed.stream(), - renamed.keySet().stream()) + created.stream(), + updated.stream(), + removed.stream(), + renamed.keySet().stream()) .flatMap(s -> s); } @@ -398,13 +400,14 @@ public class RegisterRules implements Startable { .setIsAdHoc(false) .setCreatedAt(system2.now()) .setUpdatedAt(system2.now()); - if (ruleDef.htmlDescription() != null) { - ruleDto.setDescription(ruleDef.htmlDescription()); + if (isNotEmpty(ruleDef.htmlDescription())) { + ruleDto.addRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(ruleDef.htmlDescription())); ruleDto.setDescriptionFormat(Format.HTML); - } else { - ruleDto.setDescription(ruleDef.markdownDescription()); + } else if (isNotEmpty(ruleDef.markdownDescription())) { + ruleDto.addRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(ruleDef.markdownDescription())); ruleDto.setDescriptionFormat(Format.MARKDOWN); } + DebtRemediationFunction debtRemediationFunction = ruleDef.debtRemediationFunction(); if (debtRemediationFunction != null) { ruleDto.setDefRemediationFunction(debtRemediationFunction.type().name()); @@ -481,20 +484,30 @@ public class RegisterRules implements Startable { return changed; } - private static boolean mergeDescription(RulesDefinition.Rule def, RuleDefinitionDto dto) { + private static boolean mergeDescription(RulesDefinition.Rule rule, RuleDefinitionDto ruleDefinitionDto) { boolean changed = false; - if (def.htmlDescription() != null && !Objects.equals(dto.getDescription(), def.htmlDescription())) { - dto.setDescription(def.htmlDescription()); - dto.setDescriptionFormat(Format.HTML); + + String currentDescription = ruleDefinitionDto.getDefaultRuleDescriptionSectionDto() != null ? ruleDefinitionDto.getDefaultRuleDescriptionSectionDto().getDescription() : null; + if (isHtmlDescriptionUpdated(rule, currentDescription)) { + ruleDefinitionDto.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(rule.htmlDescription())); + ruleDefinitionDto.setDescriptionFormat(Format.HTML); changed = true; - } else if (def.markdownDescription() != null && !Objects.equals(dto.getDescription(), def.markdownDescription())) { - dto.setDescription(def.markdownDescription()); - dto.setDescriptionFormat(Format.MARKDOWN); + } else if (isMarkdownDescriptionUpdated(rule, currentDescription)) { + ruleDefinitionDto.addOrReplaceRuleDescriptionSectionDto(createDefaultRuleDescriptionSection(rule.markdownDescription())); + ruleDefinitionDto.setDescriptionFormat(Format.MARKDOWN); changed = true; } return changed; } + private static boolean isMarkdownDescriptionUpdated(RulesDefinition.Rule rule, @Nullable String currentDescription) { + return isNotEmpty(rule.markdownDescription()) && !Objects.equals(rule.markdownDescription(), currentDescription); + } + + private static boolean isHtmlDescriptionUpdated(RulesDefinition.Rule def, @Nullable String currentDescription) { + return isNotEmpty(def.htmlDescription()) && !Objects.equals(def.htmlDescription(), currentDescription); + } + private static boolean mergeDebtDefinitions(RulesDefinition.Rule def, RuleDefinitionDto dto) { // Debt definitions are set to null if the sub-characteristic and the remediation function are null DebtRemediationFunction debtRemediationFunction = def.debtRemediationFunction(); diff --git a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java index 6c7f40dc89f..e9cbb4ebbc7 100644 --- a/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java +++ b/server/sonar-webserver-core/src/test/java/org/sonar/server/rule/RegisterRulesTest.java @@ -88,6 +88,7 @@ import static org.sonar.api.server.rule.RulesDefinition.NewRepository; import static org.sonar.api.server.rule.RulesDefinition.NewRule; import static org.sonar.api.server.rule.RulesDefinition.OwaspTop10; import static org.sonar.api.server.rule.RulesDefinition.OwaspTop10Version.Y2021; +import static org.sonar.db.rule.RuleDescriptionSectionDto.createDefaultRuleDescriptionSection; @RunWith(DataProviderRunner.class) public class RegisterRulesTest { @@ -124,7 +125,6 @@ public class RegisterRulesTest { private ActiveRuleIndexer activeRuleIndexer; private RuleIndex ruleIndex; - @Before public void before() { ruleIndexer = new RuleIndexer(es.client(), dbClient); @@ -140,7 +140,7 @@ public class RegisterRulesTest { assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(3); RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RULE_KEY1); assertThat(rule1.getName()).isEqualTo("One"); - assertThat(rule1.getDescription()).isEqualTo("Description of One"); + assertThat(rule1.getDefaultRuleDescriptionSection().getDescription()).isEqualTo("Description of One"); assertThat(rule1.getSeverityString()).isEqualTo(BLOCKER); assertThat(rule1.getTags()).isEmpty(); assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag2", "tag3"); @@ -159,7 +159,7 @@ public class RegisterRulesTest { RuleDto hotspotRule = dbClient.ruleDao().selectOrFailByKey(db.getSession(), HOTSPOT_RULE_KEY); assertThat(hotspotRule.getName()).isEqualTo("Hotspot"); - assertThat(hotspotRule.getDescription()).isEqualTo("Minimal hotspot"); + assertThat(hotspotRule.getDefaultRuleDescriptionSection().getDescription()).isEqualTo("Minimal hotspot"); assertThat(hotspotRule.getCreatedAt()).isEqualTo(DATE1.getTime()); assertThat(hotspotRule.getUpdatedAt()).isEqualTo(DATE1.getTime()); assertThat(hotspotRule.getType()).isEqualTo(RuleType.SECURITY_HOTSPOT.getDbConstant()); @@ -188,7 +188,7 @@ public class RegisterRulesTest { assertThat(dbClient.ruleDao().selectAllDefinitions(db.getSession())).hasSize(2); RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), EXTERNAL_RULE_KEY1); assertThat(rule1.getName()).isEqualTo("One"); - assertThat(rule1.getDescription()).isEqualTo("Description of One"); + assertThat(rule1.getDefaultRuleDescriptionSection().getDescription()).isEqualTo("Description of One"); assertThat(rule1.getSeverityString()).isEqualTo(BLOCKER); assertThat(rule1.getTags()).isEmpty(); assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag2", "tag3"); @@ -207,7 +207,7 @@ public class RegisterRulesTest { RuleDto hotspotRule = dbClient.ruleDao().selectOrFailByKey(db.getSession(), EXTERNAL_HOTSPOT_RULE_KEY); assertThat(hotspotRule.getName()).isEqualTo("Hotspot"); - assertThat(hotspotRule.getDescription()).isEqualTo("Minimal hotspot"); + assertThat(hotspotRule.getDefaultRuleDescriptionSection().getDescription()).isEqualTo("Minimal hotspot"); assertThat(hotspotRule.getCreatedAt()).isEqualTo(DATE1.getTime()); assertThat(hotspotRule.getUpdatedAt()).isEqualTo(DATE1.getTime()); assertThat(hotspotRule.getType()).isEqualTo(RuleType.SECURITY_HOTSPOT.getDbConstant()); @@ -335,7 +335,7 @@ public class RegisterRulesTest { // rule1 has been updated rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RULE_KEY1); assertThat(rule1.getName()).isEqualTo("One v2"); - assertThat(rule1.getDescription()).isEqualTo("Description of One v2"); + assertThat(rule1.getDefaultRuleDescriptionSection().getDescription()).isEqualTo("Description of One v2"); assertThat(rule1.getSeverityString()).isEqualTo(INFO); assertThat(rule1.getTags()).containsOnly("usertag1", "usertag2"); assertThat(rule1.getSystemTags()).containsOnly("tag1", "tag4"); @@ -449,7 +449,7 @@ public class RegisterRulesTest { // rule1 has been updated RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of("fake", "rule")); assertThat(rule1.getName()).isEqualTo("Name2"); - assertThat(rule1.getDescription()).isEqualTo("Description"); + assertThat(rule1.getDefaultRuleDescriptionSection().getDescription()).isEqualTo("Description"); assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions()).getTotal()).isOne(); assertThat(ruleIndex.search(new RuleQuery().setQueryText("Name1"), new SearchOptions()).getTotal()).isZero(); @@ -528,7 +528,7 @@ public class RegisterRulesTest { RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository, ruleKey2)); assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid()); assertThat(rule2.getName()).isEqualTo("Name2"); - assertThat(rule2.getDescription()).isEqualTo(rule1.getDescription()); + assertThat(rule2.getDefaultRuleDescriptionSection().getDescription()).isEqualTo(rule1.getDefaultRuleDescriptionSection().getDescription()); SearchIdResult<String> searchRule2 = ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions()); assertThat(searchRule2.getUuids()).containsOnly(rule2.getUuid()); @@ -570,7 +570,7 @@ public class RegisterRulesTest { RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repository2, ruleKey)); assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid()); assertThat(rule2.getName()).isEqualTo("Name2"); - assertThat(rule2.getDescription()).isEqualTo(rule1.getDescription()); + assertThat(rule2.getDefaultRuleDescriptionSection().getDescription()).isEqualTo(rule1.getDefaultRuleDescriptionSection().getDescription()); SearchIdResult<String> searchRule2 = ruleIndex.search(new RuleQuery().setQueryText("Name2"), new SearchOptions()); assertThat(searchRule2.getUuids()).containsOnly(rule2.getUuid()); @@ -610,7 +610,7 @@ public class RegisterRulesTest { RuleDto rule2 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of(repo2, ruleKey2)); assertThat(rule2.getUuid()).isEqualTo(rule1.getUuid()); assertThat(rule2.getName()).isEqualTo(rule1.getName()); - assertThat(rule2.getDescription()).isEqualTo(rule1.getDescription()); + assertThat(rule2.getDefaultRuleDescriptionSection().getDescription()).isEqualTo(rule1.getDefaultRuleDescriptionSection().getDescription()); assertThat(ruleIndex.search(new RuleQuery().setQueryText(name), new SearchOptions()).getUuids()) .containsOnly(rule2.getUuid()); @@ -618,7 +618,7 @@ public class RegisterRulesTest { @DataProvider public static Object[][] allRenamingCases() { - return new Object[][]{ + return new Object[][] { {"repo1", "rule1", "repo1", "rule2"}, {"repo1", "rule1", "repo2", "rule1"}, {"repo1", "rule1", "repo2", "rule2"}, @@ -688,7 +688,7 @@ public class RegisterRulesTest { // rule1 has been updated RuleDto rule1 = dbClient.ruleDao().selectOrFailByKey(db.getSession(), RuleKey.of("fake", "rule")); assertThat(rule1.getName()).isEqualTo("Name"); - assertThat(rule1.getDescription()).isEqualTo("Desc2"); + assertThat(rule1.getDefaultRuleDescriptionSection().getDescription()).isEqualTo("Desc2"); assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc2"), new SearchOptions()).getTotal()).isOne(); assertThat(ruleIndex.search(new RuleQuery().setQueryText("Desc1"), new SearchOptions()).getTotal()).isZero(); @@ -702,7 +702,7 @@ public class RegisterRulesTest { NewRepository repo = context.createExternalRepository("fake", rule.getLanguage()); repo.createRule(rule.getRuleKey()) .setName(rule.getName()) - .setHtmlDescription(rule.getDescription()); + .setHtmlDescription(rule.getDefaultRuleDescriptionSectionDto().getDescription()); repo.done(); }); @@ -836,7 +836,7 @@ public class RegisterRulesTest { .setRepositoryKey("findbugs") .setName("Rule One") .setScope(Scope.ALL) - .setDescription("Rule one description") + .addRuleDescriptionSectionDto(createDefaultRuleDescriptionSection("Rule one description")) .setDescriptionFormat(RuleDto.Format.HTML) .setSystemTags(newHashSet("tag1", "tag2"))); db.getSession().commit(); |