Skip to content

Commit

Permalink
PLUGINAPI-58 PLUGINAPI-60 Add impacts to plugin API
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-geoffroy-sonarsource committed Jul 28, 2023
1 parent 85f662f commit 4baf8f9
Show file tree
Hide file tree
Showing 18 changed files with 214 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## 10.1

* Remove @Beta Code Characteristics `org.sonar.api.code.CodeCharacteristic`
* Introduce `org.sonar.api.issue.impact.SoftwareQuality` and `org.sonar.api.issue.impact.Severity` to define impacts of rules and issues

## 10.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
*/
package org.sonar.api.batch.sensor.issue;

import java.util.Map;
import javax.annotation.CheckForNull;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.RuleType;

/**
Expand Down Expand Up @@ -53,4 +55,10 @@ public interface ExternalIssue extends IIssue {
*/
RuleType type();

/**
* Impacts of the issue.
* @since 10.1
*/
Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts();

}
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@
package org.sonar.api.batch.sensor.issue;

import java.util.List;
import java.util.Map;
import java.util.Optional;
import javax.annotation.CheckForNull;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.issue.NewIssue.FlowType;
import org.sonar.api.batch.sensor.issue.fix.QuickFix;
import org.sonar.api.issue.impact.SoftwareQuality;

/**
* Represents an issue detected by a {@link Sensor}.
Expand Down Expand Up @@ -66,6 +68,12 @@ interface Flow {
@CheckForNull
Severity overriddenSeverity();

/**
* Retrieve the overriden impacts for this issue.
* @since 10.1
*/
Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> overridenImpacts();

/**
* Primary locations for this issue.
* @since 5.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.annotation.Nullable;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;

Expand Down Expand Up @@ -66,6 +67,13 @@ public interface NewExternalIssue {
*/
NewExternalIssue severity(Severity severity);

/**
* Add a new impact or override the severity of an impact already defined by the rule.
* It is only possible to define a default impact for a given {@link SoftwareQuality}
* @since 10.1
*/
NewExternalIssue addImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity);

/**
* Primary location for this issue.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.batch.sensor.issue.fix.NewQuickFix;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;

/**
Expand Down Expand Up @@ -57,6 +58,13 @@ enum FlowType {
*/
NewIssue overrideSeverity(@Nullable Severity severity);

/**
* Override severity of an impact defined by the rule.
* The impact for the SoftwareQuality must have already been defined by the rule.
* @since 10.1
*/
NewIssue overrideImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity);

/**
* Primary location for this issue.
* @since 5.2
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
*/
package org.sonar.api.batch.sensor.rule;

import java.util.Map;
import javax.annotation.CheckForNull;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.RuleType;

/**
Expand Down Expand Up @@ -61,4 +63,18 @@ public interface AdHocRule {
*/
RuleType type();

/**
* Default impacts of the rule
* @since 10.1
*/
Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> defaultImpacts();

/**
* Clean Code Attribute of the rule according to Clean Code Taxonomy.
*
* @since 10.1
*/
@CheckForNull
CleanCodeAttribute cleanCodeAttribute();

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.annotation.Nullable;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.Sensor;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.RuleType;

/**
Expand Down Expand Up @@ -63,6 +64,13 @@ public interface NewAdHocRule {
*/
NewAdHocRule severity(Severity severity);

/**
* Add new default impact to the rule.
* It is only possible to define a default impact for a given {@link SoftwareQuality}
* @since 10.1
*/
NewAdHocRule addDefaultImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity);

/**
* Save the rule. There is almost no validation, except that no duplicated ad hoc rule keys are permitted.
*/
Expand Down
8 changes: 8 additions & 0 deletions plugin-api/src/main/java/org/sonar/api/ce/measure/Issue.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
*/
package org.sonar.api.ce.measure;

import java.util.Map;
import javax.annotation.CheckForNull;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rules.RuleType;
import org.sonar.api.utils.Duration;
Expand Down Expand Up @@ -59,4 +62,9 @@ public interface Issue {

RuleType type();

/**
* @since 10.1
*/
Map<SoftwareQuality, Severity> impacts();

}
26 changes: 26 additions & 0 deletions plugin-api/src/main/java/org/sonar/api/issue/impact/Severity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Sonar Plugin API
* Copyright (C) 2009-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.issue.impact;

public enum Severity {
LOW,
MEDIUM,
HIGH
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* Sonar Plugin API
* Copyright (C) 2009-2023 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.api.issue.impact;

public enum SoftwareQuality {
MAINTAINABILITY,
RELIABILITY,
SECURITY
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
import javax.annotation.concurrent.Immutable;
import org.sonar.api.ExtensionPoint;
import org.sonar.api.ce.ComputeEngineSide;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleScope;
import org.sonar.api.rule.RuleStatus;
Expand Down Expand Up @@ -407,6 +409,14 @@ abstract class NewRule {

public abstract NewRule setSeverity(String s);

/**
* Add a default impact to the rule.
* The impact can be overriden by an issue of this rule.
* It is only possible to define a default impact for a given {@link SoftwareQuality}
* @since 10.1
*/
public abstract NewRule addDefaultImpact(SoftwareQuality softwareQuality, Severity severity);

/**
* The type as defined by the SonarQube Quality Model.
* <br>
Expand Down Expand Up @@ -595,6 +605,12 @@ abstract class Rule {

public abstract String severity();

/**
* @see NewRule#addDefaultImpact(SoftwareQuality, Severity)
* @since 10.1
*/
public abstract Map<SoftwareQuality, Severity> defaultImpacts();

/**
* See {@link NewRule#addDescriptionSection(RuleDescriptionSection)} to understand what are the requirements for a section.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.EnumMap;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand All @@ -35,6 +36,7 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.apache.commons.io.IOUtils;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleScope;
import org.sonar.api.rule.RuleStatus;
Expand Down Expand Up @@ -82,6 +84,7 @@ class DefaultNewRule extends RulesDefinition.NewRule {
private String markdownDescription;
private String internalKey;
private String severity = Severity.MAJOR;
private Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> defaultImpacts = new EnumMap<>(SoftwareQuality.class);
private boolean template;
private RuleStatus status = RuleStatus.defaultStatus();
private DebtRemediationFunction debtRemediationFunction;
Expand Down Expand Up @@ -151,6 +154,13 @@ public DefaultNewRule setType(RuleType t) {
return this;
}

@Override
public RulesDefinition.NewRule addDefaultImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity) {
checkArgument(!defaultImpacts.containsKey(softwareQuality), "Impact for Software quality %s has already been defined for rule %s", softwareQuality, this);
this.defaultImpacts.put(softwareQuality, severity);
return this;
}

@Override
public RulesDefinition.NewRule addDescriptionSection(RuleDescriptionSection ruleDescriptionSection) {
assertRuleDescriptionSectionIsValid(ruleDescriptionSection);
Expand Down Expand Up @@ -440,6 +450,10 @@ String severity() {
return severity;
}

Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> defaultImpacts() {
return defaultImpacts;
}

boolean template() {
return template;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import java.util.TreeSet;
import javax.annotation.CheckForNull;
import javax.annotation.concurrent.Immutable;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleScope;
import org.sonar.api.rule.RuleStatus;
Expand All @@ -52,6 +54,7 @@ public class DefaultRule extends RulesDefinition.Rule {
private final String markdownDescription;
private final String internalKey;
private final String severity;
private final Map<SoftwareQuality, Severity> defaultImpacts;
private final boolean template;
private final DebtRemediationFunction debtRemediationFunction;
private final String gapDescription;
Expand All @@ -75,6 +78,7 @@ public class DefaultRule extends RulesDefinition.Rule {
this.markdownDescription = newRule.markdownDescription();
this.internalKey = newRule.internalKey();
this.severity = newRule.severity();
this.defaultImpacts = Collections.unmodifiableMap(newRule.defaultImpacts());
this.template = newRule.template();
this.status = newRule.status();
this.debtRemediationFunction = newRule.debtRemediationFunction();
Expand Down Expand Up @@ -132,6 +136,11 @@ public String severity() {
return severity;
}

@Override
public Map<SoftwareQuality, Severity> defaultImpacts() {
return defaultImpacts;
}

@Override
public List<RuleDescriptionSection> ruleDescriptionSections() {
return Collections.unmodifiableList(ruleDescriptionSections);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import org.apache.commons.lang.RandomStringUtils;
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.issue.impact.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleScope;
import org.sonar.api.rule.RuleStatus;
Expand Down Expand Up @@ -136,6 +138,9 @@ public void testSimpleSetGet() {
rule.setSeverity("MAJOR");
assertThat(rule.severity()).isEqualTo("MAJOR");

rule.addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);
assertThat(rule.defaultImpacts()).hasSize(1).containsEntry(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);

rule.addDescriptionSection(RULE_DESCRIPTION_SECTION);
assertThat(rule.getRuleDescriptionSections()).containsExactly(RULE_DESCRIPTION_SECTION);

Expand Down Expand Up @@ -365,4 +370,12 @@ public void fail_if_trying_to_insert_education_principle_with_invalid_key(){
assertThatThrownBy(() -> rule.addEducationPrincipleKeys("invalid principle"))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
public void fail_if_same_software_quality_is_added_twice(){
rule.addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.HIGH);
assertThatThrownBy(()->rule.addDefaultImpact(SoftwareQuality.MAINTAINABILITY, Severity.LOW))
.isInstanceOf(IllegalArgumentException.class)
.hasMessage("Impact for Software quality MAINTAINABILITY has already been defined for rule [repository=repo, key=key]");
}
}
Loading

0 comments on commit 4baf8f9

Please sign in to comment.