8000 Prevent ExternalRuleLoader to manipulate code attribute and impact fields when runtime API < 10.1 by alban-auzeill · Pull Request #305 · SonarSource/sonar-analyzer-commons · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Prevent ExternalRuleLoader to manipulate code attribute and impact fields when runtime API < 10.1 #305

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
121 changes: 71 additions & 50 deletions commons/src/main/java/org/sonarsource/analyzer/commons/ExternalRuleLoader.java 10000
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 Ma 72EF p<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