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 org.sonar.api.issue.impact.Severity;
28 import org.sonar.api.issue.impact.SoftwareQuality;
29 import org.sonar.api.rules.RuleType;
30 import org.sonar.api.server.rule.internal.ImpactMapper;
33 * Class to map impact severity and rule severity during the override of severity of quality profile.
34 * We want to keep the severities synchronized, if the rule type or the impacts severities are customized
36 public class QProfileImpactSeverityMapper {
38 private static final BiMap<Severity, String> SEVERITY_MAPPING = new ImmutableBiMap.Builder<Severity, String>()
39 .put(Severity.INFO, org.sonar.api.rule.Severity.INFO)
40 .put(Severity.LOW, org.sonar.api.rule.Severity.MINOR)
41 .put(Severity.MEDIUM, org.sonar.api.rule.Severity.MAJOR)
42 .put(Severity.HIGH, org.sonar.api.rule.Severity.CRITICAL)
43 .put(Severity.BLOCKER, org.sonar.api.rule.Severity.BLOCKER)
46 private QProfileImpactSeverityMapper() {
49 public static Map<SoftwareQuality, Severity> mapImpactSeverities(String severity, Map<SoftwareQuality, Severity> ruleImpacts, RuleType ruleType) {
50 if (ruleType == RuleType.SECURITY_HOTSPOT) {
53 SoftwareQuality softwareQuality = ImpactMapper.convertToSoftwareQuality(ruleType);
54 Map<SoftwareQuality, Severity> result = new EnumMap<>(ruleImpacts);
55 if (ruleImpacts.containsKey(softwareQuality)) {
56 result.put(softwareQuality, SEVERITY_MAPPING.inverse().get(severity));
57 } else if (ruleImpacts.size() == 1) {
58 result.replaceAll((sq, sev) -> SEVERITY_MAPPING.inverse().get(severity));
64 public static String mapSeverity(Map<SoftwareQuality, Severity> impacts, RuleType ruleType, String ruleSeverity) {
65 SoftwareQuality softwareQuality = ImpactMapper.convertToSoftwareQuality(ruleType);
66 if (impacts.containsKey(softwareQuality)) {
67 return SEVERITY_MAPPING.get(impacts.get(softwareQuality));
68 } else if (impacts.size() == 1) {
69 return SEVERITY_MAPPING.get(impacts.entrySet().iterator().next().getValue());