org.sonarsource.analyzer.commons.ExternalRuleLoader Maven / Gradle / Ivy
/*
* SonarQube Analyzer Commons
* Copyright (C) 2009-2018 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.List;
import java.util.Map;
import java.util.Set;
import javax.annotation.CheckForNull;
import org.json.simple.JSONArray;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.rules.RuleType;
import org.sonar.api.server.rule.RulesDefinition.NewRepository;
import org.sonar.api.server.rule.RulesDefinition.NewRule;
/**
* 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.";
private final String linterKey;
private final String linterName;
private final String languageKey;
private Map rulesMap = new HashMap<>();
public ExternalRuleLoader(String linterKey, String linterName, String pathToMetadata, String languageKey) {
this.linterKey = linterKey;
this.linterName = linterName;
this.languageKey = languageKey;
loadMetadataFile(pathToMetadata);
}
/**
* IMPORTANT This method should not be used when SonarQube runtime version is less than 7.2
* because it would trigger calls to {@link org.sonar.api.server.rule.RulesDefinition.Context#createExternalRepository}
* which was added in SonarQube 7.2.
*/
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);
if (rule.tags != null) {
newRule.setTags(rule.tags);
}
if (rule.severity != null) {
newRule.setSeverity(rule.severity);
}
}
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 && externalRule.severity != null) {
return Severity.valueOf(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