3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.qualityprofile;
22 import com.google.common.collect.BiMap;
23 import com.google.common.collect.ImmutableBiMap;
24 import java.util.EnumMap;
26 import javax.annotation.CheckForNull;
27 import javax.annotation.Nullable;
28 import org.sonar.api.issue.impact.Severity;
29 import org.sonar.api.issue.impact.SoftwareQuality;
30 import org.sonar.api.rules.RuleType;
31 import org.sonar.api.server.rule.internal.ImpactMapper;
34 * Class to map impact severity and rule severity during the override of severity of quality profile.
35 * We want to keep the severities synchronized, if the rule type or the impacts severities are customized
37 public class QProfileImpactSeverityMapper {
39 private static final BiMap<Severity, String> SEVERITY_MAPPING = new ImmutableBiMap.Builder<Severity, String>()
40 .put(Severity.INFO, org.sonar.api.rule.Severity.INFO)
41 .put(Severity.LOW, org.sonar.api.rule.Severity.MINOR)
42 .put(Severity.MEDIUM, org.sonar.api.rule.Severity.MAJOR)
43 .put(Severity.HIGH, org.sonar.api.rule.Severity.CRITICAL)
44 .put(Severity.BLOCKER, org.sonar.api.rule.Severity.BLOCKER)
47 private QProfileImpactSeverityMapper() {
50 public static Map<SoftwareQuality, Severity> mapImpactSeverities(@Nullable String severity, Map<SoftwareQuality, Severity> ruleImpacts, RuleType ruleType) {
51 Map<SoftwareQuality, Severity> result = ruleImpacts.isEmpty() ? Map.of() : new EnumMap<>(ruleImpacts);
52 if (severity == null || ruleImpacts.isEmpty()) {
55 SoftwareQuality softwareQuality = ImpactMapper.convertToSoftwareQuality(ruleType);
56 if (ruleImpacts.containsKey(softwareQuality)) {
57 result.put(softwareQuality, SEVERITY_MAPPING.inverse().get(severity));
58 } else if (ruleImpacts.size() == 1) {
59 result.replaceAll((sq, sev) -> SEVERITY_MAPPING.inverse().get(severity));
65 public static String mapSeverity(Map<SoftwareQuality, Severity> impacts, RuleType ruleType, @Nullable String ruleSeverity) {
66 SoftwareQuality softwareQuality = ImpactMapper.convertToSoftwareQuality(ruleType);
67 if (impacts.containsKey(softwareQuality)) {
68 return SEVERITY_MAPPING.get(impacts.get(softwareQuality));
69 } else if (impacts.size() == 1) {
70 return SEVERITY_MAPPING.get(impacts.entrySet().iterator().next().getValue());