From 4177479b2bfed51d286fe1e278d1e2f88083d5c1 Mon Sep 17 00:00:00 2001 From: Eric Giffon Date: Tue, 21 Nov 2023 15:24:20 +0100 Subject: [PATCH] SONAR-20764 Use adhoc type when indexing adhoc rules --- .../org/sonar/db/rule/RuleForIndexingDto.java | 13 ++++++++++- .../sonar/db/rule/RuleForIndexingDtoTest.java | 14 ++++++++++- .../org/sonar/server/rule/index/RuleDoc.java | 11 +++++++-- .../sonar/server/rule/index/RuleDocTest.java | 23 +++++++++++++++---- 4 files changed, 52 insertions(+), 9 deletions(-) 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 68812d0551f..07bb752d3ad 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 @@ -49,7 +49,8 @@ public class RuleForIndexingDto { private String internalKey; private String language; private boolean isExternal; - + private boolean isAdHoc; + private Integer adHocType; private int type; private long createdAt; @@ -81,6 +82,8 @@ public class RuleForIndexingDto { ruleForIndexingDto.internalKey = r.getConfigKey(); ruleForIndexingDto.language = r.getLanguage(); ruleForIndexingDto.isExternal = r.isExternal(); + ruleForIndexingDto.isAdHoc = r.isAdHoc(); + ruleForIndexingDto.adHocType = r.getAdHocType(); ruleForIndexingDto.type = r.getType(); ruleForIndexingDto.createdAt = r.getCreatedAt(); ruleForIndexingDto.updatedAt = r.getUpdatedAt(); @@ -177,6 +180,14 @@ public class RuleForIndexingDto { return isExternal; } + public boolean isAdHoc() { + return isAdHoc; + } + + public Integer getAdHocType() { + return adHocType; + } + public long getCreatedAt() { return createdAt; } diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleForIndexingDtoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleForIndexingDtoTest.java index 995b26d195b..f963ecbb24f 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleForIndexingDtoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/rule/RuleForIndexingDtoTest.java @@ -25,6 +25,7 @@ import org.sonar.api.issue.impact.Severity; import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rules.CleanCodeAttribute; import org.sonar.api.rules.CleanCodeAttributeCategory; +import org.sonar.api.rules.RuleType; import org.sonar.db.issue.ImpactDto; import static org.assertj.core.api.Assertions.assertThat; @@ -47,4 +48,15 @@ public class RuleForIndexingDtoTest { assertThat(impact.getSoftwareQuality()).isEqualTo(SoftwareQuality.SECURITY); } -} \ No newline at end of file + @Test + public void fromRuleDto_whenAdHocRule_setAdHocFields() { + RuleDto ruleDto = RuleTesting.newRuleWithoutDescriptionSection(); + ruleDto.setIsAdHoc(true); + ruleDto.setAdHocType(RuleType.BUG); + + RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto); + + assertThat(ruleForIndexingDto.isAdHoc()).isTrue(); + assertThat(ruleForIndexingDto.getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant()); + } +} diff --git a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java index 440dce5017e..2649e597fbc 100644 --- a/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java +++ b/server/sonar-server-common/src/main/java/org/sonar/server/rule/index/RuleDoc.java @@ -35,7 +35,6 @@ import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.RuleStatus; -import org.sonar.api.rules.Rule; import org.sonar.api.rules.RuleType; import org.sonar.db.issue.ImpactDto; import org.sonar.db.rule.RuleDescriptionSectionDto; @@ -337,7 +336,7 @@ public class RuleDoc extends BaseDoc { .setRuleKey(dto.getPluginRuleKey()) .setSeverity(dto.getSeverityAsString()) .setStatus(dto.getStatus().toString()) - .setType(dto.getTypeAsRuleType()) + .setType(getType(dto)) .setCreatedAt(dto.getCreatedAt()) .setTags(Sets.union(dto.getTags(), dto.getSystemTags())) .setUpdatedAt(dto.getUpdatedAt()) @@ -347,6 +346,14 @@ public class RuleDoc extends BaseDoc { .setImpacts(dto.getImpacts().stream().collect(Collectors.toMap(ImpactDto::getSoftwareQuality, ImpactDto::getSeverity))); } + @CheckForNull + private static RuleType getType(RuleForIndexingDto dto) { + if (dto.isAdHoc() && dto.getAdHocType() != null) { + return RuleType.valueOf(dto.getAdHocType()); + } + return dto.getTypeAsRuleType(); + } + @CheckForNull private static String getRuleKey(RuleForIndexingDto dto) { if (dto.getTemplateRuleKey() != null && dto.getTemplateRepository() != null) { diff --git a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java index 916153de5fa..292051162cd 100644 --- a/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java +++ b/server/sonar-server-common/src/test/java/org/sonar/server/rule/index/RuleDocTest.java @@ -38,7 +38,7 @@ import static org.sonar.server.security.SecurityStandards.fromSecurityStandards; public class RuleDocTest { @Test - public void ruleDocOf_mapsFieldCorrectly() { + public void createFrom_mapsFieldCorrectly() { RuleDto ruleDto = newRule(); RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto); ruleForIndexingDto.setTemplateRuleKey("templateKey"); @@ -75,7 +75,7 @@ public class RuleDocTest { } @Test - public void ruleDocOf_whenGivenNoHtmlSections_hasEmptyStringInHtmlDescription() { + public void createFrom_whenGivenNoHtmlSections_hasEmptyStringInHtmlDescription() { RuleDto ruleDto = newRuleWithoutDescriptionSection(); ruleDto.setDescriptionFormat(RuleDto.Format.HTML); @@ -87,7 +87,7 @@ public class RuleDocTest { } @Test - public void ruleDocOf_whenGivenMultipleHtmlSections_hasConcatenationInHtmlDescription() { + public void createFrom_whenGivenMultipleHtmlSections_hasConcatenationInHtmlDescription() { RuleDescriptionSectionDto section1 = buildRuleDescriptionSectionDto("section1", "

html content 1

"); RuleDescriptionSectionDto section2 = buildRuleDescriptionSectionDto("section2", "

html content 2

"); RuleDescriptionSectionDto section3ctx1 = buildRuleDescriptionSectionDtoWithContext("section3", "

html content 3.1

", "ctx1"); @@ -108,7 +108,7 @@ public class RuleDocTest { } @Test - public void ruleDocOf_whenGivenMultipleMarkdownSections_transformToHtmlAndConcatenatesInHtmlDescription() { + public void createFrom_whenGivenMultipleMarkdownSections_transformToHtmlAndConcatenatesInHtmlDescription() { RuleDescriptionSectionDto section1 = buildRuleDescriptionSectionDto("section1", "*html content 1*"); RuleDescriptionSectionDto section2 = buildRuleDescriptionSectionDto("section2", "*html content 2*"); @@ -126,7 +126,7 @@ public class RuleDocTest { } @Test - public void ruleDocOf_whenSecurityHotSpot_shouldNotPopulateCleanCodeAttribute() { + public void createFrom_whenSecurityHotSpot_shouldNotPopulateCleanCodeAttribute() { RuleDto ruleDto = newRule(); ruleDto.setCleanCodeAttribute(CleanCodeAttribute.CONVENTIONAL); ruleDto.setType(RuleType.SECURITY_HOTSPOT.getDbConstant()); @@ -138,6 +138,19 @@ public class RuleDocTest { assertThat(field).isNull(); } + @Test + public void createFrom_whenAdHocRule_shouldPopulateWithAdHocType() { + RuleDto ruleDto = newRule(); + ruleDto.setType(RuleType.CODE_SMELL); + ruleDto.setIsAdHoc(true); + ruleDto.setAdHocType(RuleType.BUG); + RuleForIndexingDto ruleForIndexingDto = RuleForIndexingDto.fromRuleDto(ruleDto); + + RuleDoc ruleDoc = RuleDoc.createFrom(ruleForIndexingDto, fromSecurityStandards(Set.of())); + + assertThat(ruleDoc.type()).isEqualTo(RuleType.BUG); + } + private static RuleDescriptionSectionDto buildRuleDescriptionSectionDto(String key, String content) { return RuleDescriptionSectionDto.builder().key(key).content(content).build(); } -- 2.39.5