]> source.dussan.org Git - sonarqube.git/blob
ed37db38cb9cafc7a2d17cc7a862541ae4d8d6ef
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.qualityprofile;
21
22 import com.google.common.collect.BiMap;
23 import com.google.common.collect.ImmutableBiMap;
24 import java.util.EnumMap;
25 import java.util.Map;
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;
31
32 /**
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
35  */
36 public class QProfileImpactSeverityMapper {
37
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)
44     .build();
45
46   private QProfileImpactSeverityMapper() {
47   }
48
49   public static Map<SoftwareQuality, Severity> mapImpactSeverities(String severity, Map<SoftwareQuality, Severity> ruleImpacts, RuleType ruleType) {
50     if (ruleType == RuleType.SECURITY_HOTSPOT) {
51       return Map.of();
52     }
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));
59     }
60     return result;
61   }
62
63   @CheckForNull
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());
70     }
71     return ruleSeverity;
72   }
73 }