ne a rule programmatically. Note that rules // could be loaded from files (JSON, XML, ...) NewRule x1Rule = repository.createRule("x1") .setName("No empty line") .setHtmlDescription("Generate an issue on empty lines") // optional tags .setTags("style", "stupid") // optional status. Default value is READY. .setStatus(RuleStatus.BETA) // default severity when the rule is activated on a Quality profile. Default value is MAJOR. .setSeverity(Severity.MINOR); x1Rule .setDebtSubCharacteristic("INTEGRATION_TESTABILITY") .setDebtRemediationFunction(x1Rule.debtRemediationFunctions().linearWithOffset("1h", "30min")); x1Rule.createParam("acceptWhitespace") .setDefaultValue("false") .setType(RuleParamType.BOOLEAN) .setDescription("Accept whitespaces on the line"); // don't forget to call done() to finalize the definition repository.done(); } }
If rules are declared in a XML file with the standard SonarQube format (see {@link org.sonar.api.server.rule.RulesDefinitionXmlLoader}), then it can be loaded by using :
public class MyJsRulesDefinition implements RulesDefinition { private final RulesDefinitionXmlLoader xmlLoader; public MyJsRulesDefinition(RulesDefinitionXmlLoader xmlLoader) { this.xmlLoader = xmlLoader; } {@literal @}Override public void define(Context context) { NewRepository repository = context.createRepository("my_js", "js").setName("My Javascript Analyzer"); // see javadoc of RulesDefinitionXmlLoader for the format xmlLoader.load(repository, getClass().getResourceAsStream("/path/to/rules.xml")); repository.done(); } }
In the above example, XML file must contain name and description of each rule. If it's not the case, then the (deprecated) English bundles can be used :
public class MyJsRulesDefinition implements RulesDefinition { private final RulesDefinitionXmlLoader xmlLoader; private final RulesDefinitionI18nLoader i18nLoader; public MyJsRulesDefinition(RulesDefinitionXmlLoader xmlLoader, RulesDefinitionI18nLoader i18nLoader) { this.xmlLoader = xmlLoader; this.i18nLoader = i18nLoader; } {@literal @}Override public void define(Context context) { NewRepository repository = context.createRepository("my_js", "js").setName("My Javascript Analyzer"); xmlLoader.load(repository, getClass().getResourceAsStream("/path/to/rules.xml"), "UTF-8"); i18nLoader.load(repository); repository.done(); } }
@since 4.3