Skip to content

Commit

Permalink
Prevent ExternalRuleLoader to manipulate code attribute and impact fi…
Browse files Browse the repository at this point in the history
…elds when runtime API < 10.1
  • Loading branch information
alban-auzeill committed Aug 11, 2023
1 parent c124049 commit 0673317
Show file tree
Hide file tree
Showing 2 changed files with 196 additions and 93 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
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 @@ -105,10 +106,7 @@ public void createExternalRuleRepository(org.sonar.api.server.rule.RulesDefiniti
newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().constantPerIssue(rule.constantDebtMinutes + "min"));
newRule.setType(rule.type);
newRule.setSeverity(rule.severity.name());
if (rule.codeAttribute != null && rule.codeImpacts != null) {
newRule.setCleanCodeAttribute(rule.codeAttribute);
rule.codeImpacts.forEach(newRule::addDefaultImpact);
}
rule.applyCodeAttributeAndImpact(newRule);

if (rule.tags != null) {
newRule.setTags(rule.tags);
Expand All @@ -123,7 +121,7 @@ public Set<String> ruleKeys() {
}

/**
* If isCleanCodeImpactsAndAttributesSupported() == true then ruleType is deprecated and replaced by codeImpacts
* Deprecated, use {@link #applyTypeAndCleanCodeAttributes(NewExternalIssue, String)} instead.
*/
@Deprecated(since = "2.6")
public RuleType ruleType(String ruleKey) {
Expand All @@ -136,7 +134,7 @@ public RuleType ruleType(String ruleKey) {
}

/**
* If isCleanCodeImpactsAndAttributesSupported() == true then ruleSeverity is deprecated and replaced by codeImpacts
* Deprecated, use {@link #applyTypeAndCleanCodeAttributes(NewExternalIssue, String)} instead.
*/
@Deprecated(since = "2.6")
public Severity ruleSeverity(String ruleKey) {
Expand All @@ -148,24 +146,19 @@ public Severity ruleSeverity(String ruleKey) {
}
}

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

@Nullable
public Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> codeImpacts(String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
return externalRule.codeImpacts;
} else {
return null;
newExternalIssue
.type(DEFAULT_ISSUE_TYPE)
.severity(DEFAULT_SEVERITY);
}
return newExternalIssue;
}

public Long ruleConstantDebtMinutes(String ruleKey) {
Expand All @@ -183,7 +176,8 @@ private void loadMetadataFile(String pathToMetadata) {

List<Map<String, Object>> rules = new JsonParser().parseArray(inputStreamReader);
for (Map<String, Object> rule : rules) {
ExternalRule externalRule = new ExternalRule(rule, isCleanCodeImpactsAndAttributesSupported);
ExternalRule externalRule = isCleanCodeImpactsAndAttributesSupported ?
new ExternalRuleWithCodeAttribute(rule) : new ExternalRule(rule);
rulesMap.put(externalRule.key, externalRule);
}

Expand All @@ -209,13 +203,7 @@ private static class ExternalRule {

final Long constantDebtMinutes;

@CheckForNull
final CleanCodeAttribute codeAttribute;

@CheckForNull
final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> codeImpacts;

public ExternalRule(Map<String, Object> rule, boolean isCleanCodeImpactsAndAttributesSupported) {
public ExternalRule(Map<String, Object> rule) {
this.key = (String) rule.get("key");
this.name = (String) rule.get("name");
this.url = (String) rule.get("url");
Expand All @@ -229,13 +217,14 @@ public ExternalRule(Map<String, Object> rule, boolean isCleanCodeImpactsAndAttri
}
type = getType(rule);
severity = getSeverity(rule);
if (isCleanCodeImpactsAndAttributesSupported) {
codeAttribute = getCodeAttribute(rule);
codeImpacts = getCodeImpacts(rule);
} else {
codeAttribute = null;
codeImpacts = null;
}
}

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) {
Expand All @@ -256,6 +245,53 @@ private static Severity getSeverity(Map<String, Object> rule) {
}
}

String getDescription(String linterKey, String linterName) {
if (description != null && url != null) {
return String.format(DESCRIPTION_WITH_URL, description, url, linterName);
}

if (description != null) {
return description;
}

if (url != null) {
return String.format(DESCRIPTION_ONLY_URL, linterName, key, url, linterName);
}

return String.format(DESCRIPTION_FALLBACK, linterKey, key);
}
}

private static class ExternalRuleWithCodeAttribute extends ExternalRule {

@CheckForNull
final CleanCodeAttribute codeAttribute;

@CheckForNull
final Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> codeImpacts;

public ExternalRuleWithCodeAttribute(Map<String, Object> rule) {
super(rule);
codeAttribute = getCodeAttribute(rule);
codeImpacts = getCodeImpacts(rule);
}

@Override
public void applyCodeAttributeAndImpact(NewRule newRule) {
if (codeAttribute != null && codeImpacts != null) {
newRule.setCleanCodeAttribute(codeAttribute);
codeImpacts.forEach(newRule::addDefaultImpact);
}
}

@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 Expand Up @@ -284,21 +320,6 @@ private static Map<SoftwareQuality, org.sonar.api.issue.impact.Severity> getCode
return null;
}

String getDescription(String linterKey, String linterName) {
if (description != null && url != null) {
return String.format(DESCRIPTION_WITH_URL, description, url, linterName);
}

if (description != null) {
return description;
}

if (url != null) {
return String.format(DESCRIPTION_ONLY_URL, linterName, key, url, linterName);
}

return String.format(DESCRIPTION_FALLBACK, linterKey, key);
}
}

}
Loading

0 comments on commit 0673317

Please sign in to comment.