]> source.dussan.org Git - sonarqube.git/blob
571bf550852f8a030492bbaf19fc298881d1ceb6
[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 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;
32
33 /**
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
36  */
37 public class QProfileImpactSeverityMapper {
38
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)
45     .build();
46
47   private QProfileImpactSeverityMapper() {
48   }
49
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()) {
53       return result;
54     }
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));
60     }
61     return result;
62   }
63
64   @CheckForNull
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());
71     }
72     return ruleSeverity;
73   }
74 }