8000 Remove usage of @Beta cleanCodeAttribute and addImpact of NewExternal Issue that could be removed in a near future by alban-auzeill · Pull Request #306 · SonarSource/sonar-analyzer-commons · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Remove usage of @Beta cleanCodeAttribute and addImpact of NewExternal Issue that could be removed in a near future #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
import org.json.simple.JSONObject;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.batch.sensor.issue.NewExternalIssue;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.CleanCodeAttribute;
import org.sonar.api.rules.RuleType;
Expand Down Expand Up @@ -120,10 +119,6 @@ public Set<String> ruleKeys() {
return rulesMap.keySet();
}

/**
* Deprecated, use {@link #applyTypeAndCleanCodeAttributes(NewExternalIssue, String)} instead.
*/
@Deprecated(since = "2.6")
public RuleType ruleType(String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
Expand All @@ -133,10 +128,6 @@ public RuleType ruleType(String ruleKey) {
}
}

/**
* Deprecated, use {@link #applyTypeAndCleanCodeAttributes(NewExternalIssue, String)} instead.
*/
@Deprecated(since = "2.6")
public Severity ruleSeverity(String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
Expand All @@ -146,21 +137,6 @@ public Severity ruleSeverity(String ruleKey) {
}
}

public NewExternalIssue applyTypeAndCleanCodeAttributes(NewExternalIssue newExternalIssue, String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
newExternalIssue
.type(externalRule.type)
.severity(externalRule.severity);
externalRule.applyCodeAttributeAndImpact(newExternalIssue);
} else {
newExternalIssue
.type(DEFAULT_ISSUE_TYPE)
.severity(DEFAULT_SEVERITY);
}
return newExternalIssue;
}

public Long ruleConstantDebtMinutes(String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
Expand Down Expand Up @@ -223,10 +199,6 @@ public void applyCodeAttributeAndImpact(NewRule newRule) {
// only supported by ExternalRuleWithCodeAttribute
}

public void applyCodeAttributeAndImpact(NewExternalIssue newExternalIssue) {
// only supported by ExternalRuleWithCodeAttribute
}

private static RuleType getType(Map<String, Object> rule) {
String strType = (String) rule.get("type");
if (strType != null) {
Expand Down Expand Up @@ -284,14 +256,6 @@ public void applyCodeAttributeAndImpact(NewRule newRule) {
}
}

@Override
public void applyCodeAttributeAndImpact(NewExternalIssue newExternalIssue) {
if (codeAttribute != null && codeImpacts != null) {
newExternalIssue.cleanCodeAttribute(codeAttribute);
codeImpacts.forEach(newExternalIssue::addImpact);
}
}

@Nullable
private static CleanCodeAttribute getCodeAttribute(Map<String, Object> rule) {
JSONObject code = (JSONObject) rule.get("code");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,108 +244,6 @@ public void test_repository_10_1() {
);
}

@Test
public void test_code_attributes_on_new_issues_SQ_API_10_0() {
DefaultExternalIssueWithCodeAttribute externalIssue = new DefaultExternalIssueWithCodeAttribute(mock(DefaultInputProject.class));
ExternalRuleLoader externalRuleLoader = loadMyLinterJson(RUNTIME_10_0);

String ruleKey = "identifiable-low-maintainability-rule";
externalRuleLoader.applyTypeAndCleanCodeAttributes(externalIssue, ruleKey);

SoftAssertions softly = new SoftAssertions();
softly.assertThat(externalIssue.type()).as("type of " + ruleKey).isEqualTo(BUG);
softly.assertThat(externalIssue.severity()).as("severity of " + ruleKey).isEqualTo(MINOR);
softly.assertThat(externalIssue.cleanCodeAttribute()).as("cleanCodeAttribute of " + ruleKey).isNull();
softly.assertThat(externalIssue.impacts()).as("impacts of " + ruleKey).isEmpty();
softly.assertAll();
}

@Test
public void test_code_attributes_on_new_issues_SQ_API_10_1() {
ExternalRuleLoader externalRuleLoader = loadMyLinterJson(RUNTIME_10_1);

String ruleKey = "identifiable-low-maintainability-rule";
DefaultExternalIssueWithCodeAttribute externalIssue = new DefaultExternalIssueWithCodeAttribute(mock(DefaultInputProject.class));
externalRuleLoader.applyTypeAndCleanCodeAttributes(externalIssue, ruleKey);

SoftAssertions softly = new SoftAssertions();
softly.assertThat(externalIssue.type()).as("type of " + ruleKey).isEqualTo(BUG);
softly.assertThat(externalIssue.severity()).as("severity of " + ruleKey).isEqualTo(MINOR);
softly.assertThat(externalIssue.cleanCodeAttribute()).as("cleanCodeAttribute of " + ruleKey).isEqualTo(IDENTIFIABLE);
softly.assertThat(externalIssue.impacts()).as("impacts of " + ruleKey).isEqualTo(Map.of(MAINTAINABILITY, LOW));
softly.assertAll();

ruleKey = "missing-code-attribute";
externalIssue = new DefaultExternalIssueWithCodeAttribute(mock(DefaultInputProject.class));
externalRuleLoader.applyTypeAndCleanCodeAttributes(externalIssue, ruleKey);

softly = new SoftAssertions();
softly.assertThat(externalIssue.cleanCodeAttribute()).as("cleanCodeAttribute of " + ruleKey).isNull();
softly.assertThat(externalIssue.impacts()).as("impacts of " + ruleKey).isEmpty();
softly.assertAll();

ruleKey = "missing-impacts";
externalIssue = new DefaultExternalIssueWithCodeAttribute(mock(DefaultInputProject.class));
externalRuleLoader.applyTypeAndCleanCodeAttributes(externalIssue, ruleKey);

softly = new SoftAssertions();
softly.assertThat(externalIssue.cleanCodeAttribute()).as("cleanCodeAttribute of " + ruleKey).isNull();
softly.assertThat(externalIssue.impacts()).as("impacts of " + ruleKey).isEmpty();
softly.assertAll();
}

@Test
public void test_code_attributes_on_unknown_rule() {
DefaultExternalIssueWithCodeAttribute externalIssue = new DefaultExternalIssueWithCodeAttribute(mock(DefaultInputProject.class));
ExternalRuleLoader externalRuleLoader = loadMyLinterJson(RUNTIME_10_1);

String ruleKey = "unknown-rule-key";
externalRuleLoader.applyTypeAndCleanCodeAttributes(externalIssue, ruleKey);

SoftAssertions softly = new SoftAssertions();
softly.assertThat(externalIssue.type()).as("type of " + ruleKey).isEqualTo(CODE_SMELL);
softly.assertThat(externalIssue.severity()).as("severity of " + ruleKey).isEqualTo(MAJOR);
softly.assertThat(externalIssue.cleanCodeAttribute()).as("cleanCodeAttribute of " + ruleKey).isNull();
softly.assertThat(externalIssue.impacts()).as("impacts of " + ruleKey).isEmpty();
softly.assertAll();
}

/**
* This class exists because sonar-plugin-api-impl:10.1.0.73491 does not yet implement all the methods of sonar-plugin-api:10.1.0.809
* Will be removed and replace with DefaultExternalIssue once sonar-plugin-api-impl has a new version.
*/
class DefaultExternalIssueWithCodeAttribute extends DefaultExternalIssue {
private CleanCodeAttribute attribute = null;
private final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts = new LinkedHashMap<>();

public DefaultExternalIssueWithCodeAttribute(DefaultInputProject project) {
super(project);
}

@Override
public Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> impacts() {
return impacts;
}

@org.jetbrains.annotations.Nullable
@Override
public CleanCodeAttribute cleanCodeAttribute() {
return attribute;
}

@Override
public NewExternalIssue cleanCodeAttribute(CleanCodeAttribute attribute) {
this.attribute = attribute;
return this;
}

@Override
public NewExternalIssue addImpact(SoftwareQuality softwareQuality, org.sonar.api.issue.impact.Severity severity) {
impacts.put(softwareQuality, severity);
return this;
}
}

private static void assertRule(ExternalRuleLoader externalRuleLoader, String ruleKey,
RuleType expectedRuleType, Severity expectedRuleSeverity, Long expectedDebtMinutes) {
SoftAssertions softly = new SoftAssertions();
Expand Down
0