aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-webserver-common
diff options
context:
space:
mode:
authorLéo Geoffroy <leo.geoffroy@sonarsource.com>2024-10-15 14:14:59 +0200
committersonartech <sonartech@sonarsource.com>2024-10-16 20:03:02 +0000
commitaff39a4a01010cd4e6425ecd6f17146dd3274f89 (patch)
treec5249a8436cddc1506d5bb6a0e211e5b033e0fae /server/sonar-webserver-common
parentc18a1fe1ccd427bf9511d7370777cd5042219f43 (diff)
downloadsonarqube-aff39a4a01010cd4e6425ecd6f17146dd3274f89.tar.gz
sonarqube-aff39a4a01010cd4e6425ecd6f17146dd3274f89.zip
SONAR-23258 fix bug with custom rules
Diffstat (limited to 'server/sonar-webserver-common')
-rw-r--r--server/sonar-webserver-common/src/it/java/org/sonar/server/common/rule/RuleCreatorIT.java17
-rw-r--r--server/sonar-webserver-common/src/main/java/org/sonar/server/common/rule/RuleCreator.java10
2 files changed, 17 insertions, 10 deletions
diff --git a/server/sonar-webserver-common/src/it/java/org/sonar/server/common/rule/RuleCreatorIT.java b/server/sonar-webserver-common/src/it/java/org/sonar/server/common/rule/RuleCreatorIT.java
index a904093cf98..dea97caa528 100644
--- a/server/sonar-webserver-common/src/it/java/org/sonar/server/common/rule/RuleCreatorIT.java
+++ b/server/sonar-webserver-common/src/it/java/org/sonar/server/common/rule/RuleCreatorIT.java
@@ -89,7 +89,8 @@ public class RuleCreatorIT {
private final DbSession dbSession = dbTester.getSession();
private final UuidFactory uuidFactory = new SequenceUuidFactory();
- private final RuleCreator underTest = new RuleCreator(system2, new RuleIndexer(es.client(), dbTester.getDbClient()), dbTester.getDbClient(), newFullTypeValidations(), uuidFactory);
+ private final RuleCreator underTest = new RuleCreator(system2, new RuleIndexer(es.client(), dbTester.getDbClient()), dbTester.getDbClient(), newFullTypeValidations(),
+ uuidFactory);
@Test
public void create_custom_rule() {
@@ -357,6 +358,7 @@ public class RuleCreatorIT {
.setName("My custom")
.setMarkdownDescription("some description")
.setSeverity(Severity.MAJOR)
+ .setImpacts(List.of(new NewCustomRule.Impact(RELIABILITY, MEDIUM)))
.setStatus(RuleStatus.READY);
NewCustomRule secondRule = NewCustomRule.createForCustomRule(RuleKey.parse("java:CUSTOM_RULE_2"), templateRule.getKey())
@@ -365,7 +367,7 @@ public class RuleCreatorIT {
.setSeverity(Severity.MAJOR)
.setStatus(RuleStatus.READY);
- List<RuleKey> customRuleKeys = underTest.create(dbSession, Arrays.asList(firstRule, secondRule))
+ List<RuleKey> customRuleKeys = underTest.restore(dbSession, Arrays.asList(firstRule, secondRule))
.stream()
.map(RuleDto::getKey)
.toList();
@@ -376,6 +378,11 @@ public class RuleCreatorIT {
assertThat(rules).asList()
.extracting("ruleKey")
.containsOnly("CUSTOM_RULE_1", "CUSTOM_RULE_2");
+
+ RuleDto customRule1 = rules.stream().filter(e -> e.getRuleKey().equals("CUSTOM_RULE_1")).findFirst().orElseThrow();
+ assertThat(customRule1.getSeverityString()).isEqualTo(Severity.MAJOR);
+ assertThat(customRule1.getDefaultImpactsMap()).containsExactlyInAnyOrderEntriesOf(Map.of(RELIABILITY, MEDIUM));
+
}
@Test
@@ -391,8 +398,8 @@ public class RuleCreatorIT {
.setSeverity(Severity.MAJOR)
.setStatus(RuleStatus.READY)
.setParameters(Map.of("regex", "a.*"));
-
- assertThatThrownBy(() -> underTest.create(dbSession, singletonList(newRule)))
+ List<NewCustomRule> newRules = singletonList(newRule);
+ assertThatThrownBy(() -> underTest.restore(dbSession, newRules))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("This rule is not a template rule: java:S001");
}
@@ -413,7 +420,7 @@ public class RuleCreatorIT {
.setStatus(RuleStatus.READY);
List<NewCustomRule> newRules = singletonList(newRule);
- assertThatThrownBy(() -> underTest.create(dbSession, newRules))
+ assertThatThrownBy(() -> underTest.restore(dbSession, newRules))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("The template key doesn't exist: java:S001");
}
diff --git a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/rule/RuleCreator.java b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/rule/RuleCreator.java
index 4b5fdcc8448..004ff3188d4 100644
--- a/server/sonar-webserver-common/src/main/java/org/sonar/server/common/rule/RuleCreator.java
+++ b/server/sonar-webserver-common/src/main/java/org/sonar/server/common/rule/RuleCreator.java
@@ -85,7 +85,7 @@ public class RuleCreator {
.orElseThrow(() -> new IllegalArgumentException(format(TEMPLATE_KEY_NOT_EXIST_FORMAT, templateKey)));
checkArgument(templateRule.isTemplate(), "This rule is not a template rule: %s", templateKey.toString());
checkArgument(templateRule.getStatus() != RuleStatus.REMOVED, TEMPLATE_KEY_NOT_EXIST_FORMAT, templateKey.toString());
- validateCustomRule(newRule, dbSession, templateKey);
+ validateCustomRule(newRule, dbSession, templateKey, true);
Optional<RuleDto> definition = loadRule(dbSession, newRule.ruleKey());
RuleDto ruleDto = definition.map(dto -> updateExistingRule(dto, newRule, dbSession))
@@ -95,7 +95,7 @@ public class RuleCreator {
return ruleDto;
}
- public List<RuleDto> create(DbSession dbSession, List<NewCustomRule> newRules) {
+ public List<RuleDto> restore(DbSession dbSession, List<NewCustomRule> newRules) {
Set<RuleKey> templateKeys = newRules.stream().map(NewCustomRule::templateKey).collect(Collectors.toSet());
Map<RuleKey, RuleDto> templateRules = dbClient.ruleDao().selectByKeys(dbSession, templateKeys)
.stream()
@@ -112,7 +112,7 @@ public class RuleCreator {
List<RuleDto> customRules = newRules.stream()
.map(newCustomRule -> {
RuleDto templateRule = templateRules.get(newCustomRule.templateKey());
- validateCustomRule(newCustomRule, dbSession, templateRule.getKey());
+ validateCustomRule(newCustomRule, dbSession, templateRule.getKey(), false);
return createCustomRule(newCustomRule, templateRule, dbSession);
})
.toList();
@@ -121,7 +121,7 @@ public class RuleCreator {
return customRules;
}
- private void validateCustomRule(NewCustomRule newRule, DbSession dbSession, RuleKey templateKey) {
+ private void validateCustomRule(NewCustomRule newRule, DbSession dbSession, RuleKey templateKey, boolean checkImpacts) {
List<String> errors = new ArrayList<>();
validateRuleKey(errors, newRule.ruleKey(), templateKey);
@@ -135,7 +135,7 @@ public class RuleCreator {
if (severity != null && !Severity.ALL.contains(severity)) {
errors.add(format("Severity \"%s\" is invalid", severity));
}
- if (!CollectionUtils.isEmpty(newRule.getImpacts()) && (StringUtils.isNotBlank(newRule.severity()) || newRule.type() != null)) {
+ if (checkImpacts && !CollectionUtils.isEmpty(newRule.getImpacts()) && (StringUtils.isNotBlank(newRule.severity()) || newRule.type() != null)) {
errors.add("The rule cannot have both impacts and type/severity specified");
}