org.sonarsource.analyzer.commons.ExternalRuleLoader Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sonar-analyzer-commons Show documentation
Show all versions of sonar-analyzer-commons Show documentation
Logic useful for a language plugin
/*
* SonarSource Analyzers Commons
* Copyright (C) 2009-2024 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.sonarsource.analyzer.commons;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nullable;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.sonar.api.SonarRuntime;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.issue.impact.SoftwareQuality;
import org.sonar.api.rules.CleanCodeAttribute;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.rule.RulesDefinition.NewRepository;
import org.sonar.api.server.rule.RulesDefinition.NewRule;
import org.sonar.api.utils.Version;
/**
* Creates external rule repository based on json file in the format [ { "key": "...", "name": "..." }, ... ]
*
* "key" and "name" are required properties. Also could be provided:
*
* - url (link to rule page)
* - description (html description of rule)
* - constantDebtMinutes (e.g. 30, by default 5)
* - tags (array of strings, e.g.
"tags": [ "foo", "bar" ]
)
* - severity (as in SQ API, e.g. "MAJOR")
* - type (as in SQ API, e.g. "BUG")
*
*/
public class ExternalRuleLoader {
private static final Long DEFAULT_CONSTANT_DEBT_MINUTES = 5L;
private static final RuleType DEFAULT_ISSUE_TYPE = RuleType.CODE_SMELL;
private static final Severity DEFAULT_SEVERITY = Severity.MAJOR;
private static final String DESCRIPTION_ONLY_URL = "See description of %s rule %s
at the %s website.";
private static final String DESCRIPTION_WITH_URL = "%s
See more at the %s website.
";
private static final String DESCRIPTION_FALLBACK = "This is external rule %s:%s
. No details are available.";
public static final Version API_VERSION_SUPPORTING_CLEAN_CODE_IMPACTS_AND_ATTRIBUTES = Version.create(10, 1);
private final String linterKey;
private final String linterName;
private final String languageKey;
private final boolean isCleanCodeImpactsAndAttributesSupported;
private Map rulesMap = new HashMap<>();
/**
* @deprecated Use the constructor that also provide the SonarRuntime argument to determine if you can use
* the new Clean Code attributes and impacts API.
* Then you should test "isCleanCodeImpactsAndAttributesSupported()" before using codeAttribute and codeImpacts.
*/
@Deprecated(since = "2.6")
public ExternalRuleLoader(String linterKey, String linterName, String pathToMetadata, String languageKey) {
this(linterKey, linterName, pathToMetadata, languageKey, null);
}
public ExternalRuleLoader(String linterKey, String linterName, String pathToMetadata, String languageKey, @Nullable SonarRuntime sonarRuntime) {
this.linterKey = linterKey;
this.linterName = linterName;
this.languageKey = languageKey;
isCleanCodeImpactsAndAttributesSupported = sonarRuntime != null &&
sonarRuntime.getApiVersion().isGreaterThanOrEqual(API_VERSION_SUPPORTING_CLEAN_CODE_IMPACTS_AND_ATTRIBUTES);
loadMetadataFile(pathToMetadata);
}
public boolean isCleanCodeImpactsAndAttributesSupported() {
return isCleanCodeImpactsAndAttributesSupported;
}
public void createExternalRuleRepository(org.sonar.api.server.rule.RulesDefinition.Context context) {
NewRepository externalRepo = context.createExternalRepository(linterKey, languageKey).setName(linterName);
for (ExternalRule rule : rulesMap.values()) {
NewRule newRule = externalRepo.createRule(rule.key).setName(rule.name);
newRule.setHtmlDescription(rule.getDescription(linterKey, linterName));
newRule.setDebtRemediationFunction(newRule.debtRemediationFunctions().constantPerIssue(rule.constantDebtMinutes + "min"));
newRule.setType(rule.type);
newRule.setSeverity(rule.severity.name());
rule.applyCodeAttributeAndImpact(newRule);
if (rule.tags != null) {
newRule.setTags(rule.tags);
}
}
externalRepo.done();
}
public Set ruleKeys() {
return rulesMap.keySet();
}
public RuleType ruleType(String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
return externalRule.type;
} else {
return DEFAULT_ISSUE_TYPE;
}
}
public Severity ruleSeverity(String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
return externalRule.severity;
} else {
return DEFAULT_SEVERITY;
}
}
public Long ruleConstantDebtMinutes(String ruleKey) {
ExternalRule externalRule = rulesMap.get(ruleKey);
if (externalRule != null) {
return externalRule.constantDebtMinutes;
} else {
return DEFAULT_CONSTANT_DEBT_MINUTES;
}
}
private void loadMetadataFile(String pathToMetadata) {
InputStream inputStream = ExternalRuleLoader.class.getClassLoader().getResourceAsStream(pathToMetadata);
try (InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8)) {
List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy