diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2024-10-10 18:14:38 +0200 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-10-16 20:03:01 +0000 |
commit | e2cb22069d25733459a1d058be1a7c1f3ca370ef (patch) | |
tree | 8174db0874a9c60e1ed26ae7cea13fb025d6d9e6 /sonar-plugin-api-impl | |
parent | 5038a353ca5917a647e5613859fbf62e756f4a21 (diff) | |
download | sonarqube-e2cb22069d25733459a1d058be1a7c1f3ca370ef.tar.gz sonarqube-e2cb22069d25733459a1d058be1a7c1f3ca370ef.zip |
SONAR-23250 handle impacts on active rules in scanner engine
Diffstat (limited to 'sonar-plugin-api-impl')
3 files changed, 25 insertions, 6 deletions
diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java index e0af1b32898..e893c7842c7 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java @@ -19,13 +19,12 @@ */ package org.sonar.api.batch.rule.internal; -import java.util.Collections; -import java.util.HashMap; -import java.util.HashSet; import java.util.Map; import java.util.Set; import javax.annotation.concurrent.Immutable; import org.sonar.api.batch.rule.ActiveRule; +import org.sonar.api.issue.impact.Severity; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleKey; @Immutable @@ -36,6 +35,7 @@ public class DefaultActiveRule implements ActiveRule { private final String language; private final String templateRuleKey; private final Map<String, String> params; + private final Map<SoftwareQuality, Severity> impacts; private final long createdAt; private final long updatedAt; private final String qProfileKey; @@ -46,12 +46,13 @@ public class DefaultActiveRule implements ActiveRule { this.internalKey = newActiveRule.internalKey; this.templateRuleKey = newActiveRule.templateRuleKey; this.ruleKey = newActiveRule.ruleKey; - this.params = Collections.unmodifiableMap(new HashMap<>(newActiveRule.params)); + this.params = Map.copyOf(newActiveRule.params); + this.impacts = Map.copyOf(newActiveRule.impacts); this.language = newActiveRule.language; this.createdAt = newActiveRule.createdAt; this.updatedAt = newActiveRule.updatedAt; this.qProfileKey = newActiveRule.qProfileKey; - this.deprecatedKeys = Collections.unmodifiableSet(new HashSet<>(newActiveRule.deprecatedKeys)); + this.deprecatedKeys = Set.copyOf(newActiveRule.deprecatedKeys); } @Override @@ -65,6 +66,11 @@ public class DefaultActiveRule implements ActiveRule { } @Override + public Map<SoftwareQuality, Severity> impacts() { + return impacts; + } + + @Override public String language() { return language; } diff --git a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java index 9afdf89f9c9..cf21a1b39cd 100644 --- a/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java +++ b/sonar-plugin-api-impl/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java @@ -19,6 +19,7 @@ */ package org.sonar.api.batch.rule.internal; +import java.util.EnumMap; import java.util.HashMap; import java.util.HashSet; import java.util.Map; @@ -26,6 +27,7 @@ import java.util.Set; import javax.annotation.Nullable; import javax.annotation.concurrent.Immutable; import org.apache.commons.lang3.StringUtils; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; @@ -37,6 +39,7 @@ public class NewActiveRule { final RuleKey ruleKey; final String name; final String severity; + final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts; final Map<String, String> params; final long createdAt; final long updatedAt; @@ -50,6 +53,7 @@ public class NewActiveRule { this.ruleKey = builder.ruleKey; this.name = builder.name; this.severity = builder.severity; + this.impacts = builder.impacts; this.params = builder.params; this.createdAt = builder.createdAt; this.updatedAt = builder.updatedAt; @@ -68,6 +72,7 @@ public class NewActiveRule { private RuleKey ruleKey; private String name; private String severity = Severity.defaultSeverity(); + private final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = new EnumMap<>(SoftwareQuality.class); private Map<String, String> params = new HashMap<>(); private long createdAt; private long updatedAt; @@ -92,6 +97,11 @@ public class NewActiveRule { return this; } + public Builder setImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity) { + impacts.put(softwareQuality, severity); + return this; + } + public Builder setParam(String key, @Nullable String value) { // possible improvement : check that the param key exists in rule definition if (value == null) { diff --git a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java index 51d6facef11..449b7a35b40 100644 --- a/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java +++ b/sonar-plugin-api-impl/src/test/java/org/sonar/api/batch/rule/internal/NewActiveRuleTest.java @@ -20,9 +20,10 @@ package org.sonar.api.batch.rule.internal; import com.google.common.collect.ImmutableMap; +import java.util.Map; import org.junit.Before; import org.junit.Test; -import org.sonar.api.batch.rule.internal.NewActiveRule; +import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.rule.RuleKey; import org.sonar.api.rule.Severity; @@ -43,6 +44,7 @@ public class NewActiveRuleTest { .setRuleKey(RuleKey.of("foo", "bar")) .setName("name") .setSeverity(org.sonar.api.rule.Severity.CRITICAL) + .setImpact(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM) .setParam("key", "value") .setCreatedAt(1_000L) .setUpdatedAt(1_000L) @@ -55,6 +57,7 @@ public class NewActiveRuleTest { assertThat(rule.ruleKey).isEqualTo(RuleKey.of("foo", "bar")); assertThat(rule.name).isEqualTo("name"); assertThat(rule.severity).isEqualTo(org.sonar.api.rule.Severity.CRITICAL); + assertThat(rule.impacts).containsExactlyInAnyOrderEntriesOf(Map.of(SoftwareQuality.MAINTAINABILITY, org.sonar.api.issue.impact.Severity.MEDIUM)); assertThat(rule.params).isEqualTo(ImmutableMap.of("key", "value")); assertThat(rule.createdAt).isEqualTo(1_000L); assertThat(rule.updatedAt).isEqualTo(1_000L); |