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.api.batch.sensor.issue.internal;
22 import java.util.EnumMap;
24 import javax.annotation.CheckForNull;
25 import javax.annotation.Nullable;
26 import org.sonar.api.batch.fs.internal.DefaultInputProject;
27 import org.sonar.api.batch.rule.Severity;
28 import org.sonar.api.batch.sensor.internal.SensorStorage;
29 import org.sonar.api.batch.sensor.issue.ExternalIssue;
30 import org.sonar.api.batch.sensor.issue.NewExternalIssue;
31 import org.sonar.api.issue.impact.SoftwareQuality;
32 import org.sonar.api.rule.RuleKey;
33 import org.sonar.api.rules.CleanCodeAttribute;
34 import org.sonar.api.rules.RuleType;
36 import static java.lang.String.format;
37 import static java.util.Objects.requireNonNull;
38 import static org.sonar.api.utils.Preconditions.checkArgument;
39 import static org.sonar.api.utils.Preconditions.checkState;
41 public class DefaultExternalIssue extends AbstractDefaultIssue<DefaultExternalIssue> implements ExternalIssue, NewExternalIssue {
43 private Severity severity;
44 private RuleType type;
45 private String engineId;
46 private String ruleId;
48 private Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = new EnumMap<>(SoftwareQuality.class);
49 private CleanCodeAttribute cleanCodeAttribute;
51 public DefaultExternalIssue(DefaultInputProject project) {
55 public DefaultExternalIssue(DefaultInputProject project, @Nullable SensorStorage storage) {
56 super(project, storage);
60 public DefaultExternalIssue remediationEffortMinutes(@Nullable Long effort) {
61 checkArgument(effort == null || effort >= 0, format("effort must be greater than or equal 0 (got %s)", effort));
67 public DefaultExternalIssue severity(Severity severity) {
68 this.severity = severity;
73 public DefaultExternalIssue addImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity) {
74 this.impacts.put(softwareQuality, severity);
79 public String engineId() {
84 public String ruleId() {
88 public String cveId() {
93 public Severity severity() {
98 public Long remediationEffort() {
103 public void doSave() {
104 requireNonNull(this.engineId, "Engine id is mandatory on external issue");
105 requireNonNull(this.ruleId, "Rule id is mandatory on external issue");
106 checkState(primaryLocation != null, "Primary location is mandatory on every external issue");
107 checkState(primaryLocation.message() != null, "External issues must have a message");
112 public RuleType type() {
117 public Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts() {
123 public CleanCodeAttribute cleanCodeAttribute() {
124 return cleanCodeAttribute;
128 public NewExternalIssue engineId(String engineId) {
129 this.engineId = engineId;
134 public NewExternalIssue ruleId(String ruleId) {
135 this.ruleId = ruleId;
139 public NewExternalIssue cveId(String cveId) {
145 public DefaultExternalIssue forRule(RuleKey ruleKey) {
146 this.engineId = ruleKey.repository();
147 this.ruleId = ruleKey.rule();
152 public RuleKey ruleKey() {
153 if (engineId != null && ruleId != null) {
154 return RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + engineId, ruleId);
160 public DefaultExternalIssue type(RuleType type) {
166 public DefaultExternalIssue cleanCodeAttribute(CleanCodeAttribute attribute) {
167 this.cleanCodeAttribute = attribute;