3 * Copyright (C) 2009-2023 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.ce.task.projectanalysis.issue;
22 import com.google.common.base.Preconditions;
23 import java.util.Collections;
24 import java.util.EnumMap;
25 import java.util.List;
27 import java.util.Objects;
28 import java.util.stream.Collectors;
29 import javax.annotation.CheckForNull;
30 import javax.annotation.Nullable;
31 import javax.annotation.concurrent.Immutable;
32 import org.sonar.api.issue.impact.Severity;
33 import org.sonar.api.issue.impact.SoftwareQuality;
34 import org.sonar.api.rule.RuleKey;
35 import org.sonar.api.rules.CleanCodeAttribute;
36 import org.sonar.api.rules.RuleType;
37 import org.sonar.api.server.rule.internal.ImpactMapper;
38 import org.sonar.scanner.protocol.Constants;
39 import org.sonar.scanner.protocol.output.ScannerReport;
41 import static org.apache.commons.lang.StringUtils.isNotBlank;
42 import static org.apache.commons.lang.StringUtils.trimToNull;
45 public class NewAdHocRule {
46 private final RuleKey key;
47 private final String engineId;
48 private final String ruleId;
49 private final String name;
50 private final String description;
51 private final String severity;
52 private final RuleType ruleType;
53 private final boolean hasDetails;
55 private final CleanCodeAttribute cleanCodeAttribute;
57 private final Map<SoftwareQuality, Severity> defaultImpacts = new EnumMap<>(SoftwareQuality.class);
59 public NewAdHocRule(ScannerReport.AdHocRule ruleFromScannerReport) {
60 Preconditions.checkArgument(isNotBlank(ruleFromScannerReport.getEngineId()), "'engine id' not expected to be null for an ad hoc rule");
61 Preconditions.checkArgument(isNotBlank(ruleFromScannerReport.getRuleId()), "'rule id' not expected to be null for an ad hoc rule");
62 Preconditions.checkArgument(isNotBlank(ruleFromScannerReport.getName()), "'name' not expected to be null for an ad hoc rule");
63 Preconditions.checkArgument(!ruleFromScannerReport.getDefaultImpactsList().isEmpty() || ruleFromScannerReport.getSeverity() != Constants.Severity.UNSET_SEVERITY,
64 "'severity' not expected to be null for an ad hoc rule, or impacts should be provided instead");
65 Preconditions.checkArgument(!ruleFromScannerReport.getDefaultImpactsList().isEmpty() || ruleFromScannerReport.getType() != ScannerReport.IssueType.UNSET,
66 "'issue type' not expected to be null for an ad hoc rule, or impacts should be provided instead");
67 this.key = RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + ruleFromScannerReport.getEngineId(), ruleFromScannerReport.getRuleId());
68 this.engineId = ruleFromScannerReport.getEngineId();
69 this.ruleId = ruleFromScannerReport.getRuleId();
70 this.name = ruleFromScannerReport.getName();
71 this.description = trimToNull(ruleFromScannerReport.getDescription());
72 this.hasDetails = true;
73 this.cleanCodeAttribute = mapCleanCodeAttribute(trimToNull(ruleFromScannerReport.getCleanCodeAttribute()));
74 this.ruleType = determineType(ruleFromScannerReport);
75 this.severity = determineSeverity(ruleFromScannerReport);
76 this.defaultImpacts.putAll(determineImpacts(ruleFromScannerReport));
80 private Map<SoftwareQuality, Severity> determineImpacts(ScannerReport.AdHocRule ruleFromScannerReport) {
81 if (ruleFromScannerReport.getType().equals(ScannerReport.IssueType.SECURITY_HOTSPOT)) {
82 return Collections.emptyMap();
84 Map<SoftwareQuality, Severity> impacts = mapImpacts(ruleFromScannerReport.getDefaultImpactsList());
85 if (impacts.isEmpty()) {
86 return Map.of(ImpactMapper.convertToSoftwareQuality(this.ruleType),
87 ImpactMapper.convertToImpactSeverity(this.severity));
93 private static RuleType determineType(ScannerReport.AdHocRule ruleFromScannerReport) {
94 if (ruleFromScannerReport.getType() != ScannerReport.IssueType.UNSET) {
95 return RuleType.valueOf(ruleFromScannerReport.getType().name());
97 Map<SoftwareQuality, Severity> impacts = mapImpacts(ruleFromScannerReport.getDefaultImpactsList());
98 Map.Entry<SoftwareQuality, Severity> bestImpactForBackMapping = ImpactMapper.getBestImpactForBackmapping(impacts);
99 return ImpactMapper.convertToRuleType(bestImpactForBackMapping.getKey());
102 private static String determineSeverity(ScannerReport.AdHocRule ruleFromScannerReport) {
103 if (ruleFromScannerReport.getSeverity() != Constants.Severity.UNSET_SEVERITY) {
104 return ruleFromScannerReport.getSeverity().name();
106 Map<SoftwareQuality, Severity> impacts = mapImpacts(ruleFromScannerReport.getDefaultImpactsList());
107 Map.Entry<SoftwareQuality, Severity> bestImpactForBackMapping = ImpactMapper.getBestImpactForBackmapping(impacts);
108 return ImpactMapper.convertToDeprecatedSeverity(bestImpactForBackMapping.getValue());
111 private static CleanCodeAttribute mapCleanCodeAttribute(@Nullable String cleanCodeAttribute) {
112 if (cleanCodeAttribute == null) {
113 return CleanCodeAttribute.defaultCleanCodeAttribute();
115 return CleanCodeAttribute.valueOf(cleanCodeAttribute);
118 private static Map<SoftwareQuality, Severity> mapImpacts(List<ScannerReport.Impact> impacts) {
119 if (!impacts.isEmpty()) {
120 return impacts.stream()
121 .collect(Collectors.toMap(i -> mapSoftwareQuality(i.getSoftwareQuality()), i -> mapImpactSeverity(i.getSeverity())));
123 return Collections.emptyMap();
126 private static Severity mapImpactSeverity(String severity) {
127 return Severity.valueOf(severity);
130 private static SoftwareQuality mapSoftwareQuality(String softwareQuality) {
131 return SoftwareQuality.valueOf(softwareQuality);
134 public NewAdHocRule(ScannerReport.ExternalIssue fromIssue) {
135 Preconditions.checkArgument(isNotBlank(fromIssue.getEngineId()), "'engine id' not expected to be null for an ad hoc rule");
136 Preconditions.checkArgument(isNotBlank(fromIssue.getRuleId()), "'rule id' not expected to be null for an ad hoc rule");
137 this.key = RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + fromIssue.getEngineId(), fromIssue.getRuleId());
138 this.engineId = fromIssue.getEngineId();
139 this.ruleId = fromIssue.getRuleId();
141 this.description = null;
142 this.severity = null;
143 this.ruleType = null;
144 this.hasDetails = false;
145 this.cleanCodeAttribute = CleanCodeAttribute.defaultCleanCodeAttribute();
146 this.defaultImpacts.put(SoftwareQuality.MAINTAINABILITY, Severity.MEDIUM);
149 public RuleKey getKey() {
153 public String getEngineId() {
157 public String getRuleId() {
162 public String getName() {
167 public String getDescription() {
172 public String getSeverity() {
177 public RuleType getRuleType() {
181 public boolean hasDetails() {
185 public CleanCodeAttribute getCleanCodeAttribute() {
186 return cleanCodeAttribute;
189 public Map<SoftwareQuality, Severity> getDefaultImpacts() {
190 return defaultImpacts;
194 public boolean equals(Object o) {
198 if (o == null || getClass() != o.getClass()) {
201 NewAdHocRule that = (NewAdHocRule) o;
202 return Objects.equals(key, that.key);
206 public int hashCode() {
207 return key.hashCode();