1 package de.example.plugins.custom.javascript;
3 import java.io.IOException;
7 import javax.annotation.Nullable;
9 import org.apache.commons.lang.StringUtils;
10 import org.sonar.api.rule.RuleStatus;
11 import org.sonar.api.server.debt.DebtRemediationFunction;
12 import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader;
13 import org.sonar.api.utils.AnnotationUtils;
14 import org.sonar.plugins.javascript.JavaScriptLanguage;
15 import org.sonar.plugins.javascript.api.CustomJavaScriptRulesDefinition;
16 import org.sonar.squidbridge.annotations.RuleTemplate;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.base.Charsets;
20 import com.google.common.collect.Iterables;
21 import com.google.common.io.Resources;
22 import com.google.gson.Gson;
24 import de.example.custom.javascript.checks.CheckList;
28 * This class will be injected (SonarQube is using PicoContainer) in
29 * org.sonar.plugins.javascript.JavaScriptSquidSensor.
31 * It seems like the SonarQube developers in charge of writing the JavaScript plugin tried to
32 * make easy the creation of custom Java plugins.
34 * So, JavaScriptSquidSensor will be the object that will run my rules (my Checks) whenever it finds JavaScript code.
35 * I do not have to do anything else, what is great!
38 public class CustomRulesDefinition extends CustomJavaScriptRulesDefinition {
39 private static final String RESOURCE_BASE_PATH = "/de/example/l10n/javascript/rules/custom";
41 private final Gson gson = new Gson();
44 public String repositoryName() {
45 return CheckList.REPOSITORY_NAME;
49 public String repositoryKey() {
50 return CheckList.REPOSITORY_KEY;
54 public Class[] checkClasses() {
55 return CheckList.getChecks().toArray();
59 * I do not want to use the define method implemented in org.sonar.plugins.javascript.api.CustomJavaScriptRulesDefinition.
62 public void define(Context context) {
63 NewRepository repository = context
64 .createRepository(repositoryKey(), JavaScriptLanguage.KEY)
65 .setName(repositoryName());
66 List<Class> checks = CheckList.getChecks();
67 new RulesDefinitionAnnotationLoader().load(repository, Iterables.toArray(checks, Class.class));
68 for (Class ruleClass : checks) {
69 newRule(ruleClass, repository);
75 protected void newRule(Class<?> ruleClass, NewRepository repository) {
77 org.sonar.check.Rule ruleAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.check.Rule.class);
78 if (ruleAnnotation == null) {
79 throw new IllegalArgumentException("No Rule annotation was found on " + ruleClass);
81 String ruleKey = ruleAnnotation.key();
82 if (StringUtils.isEmpty(ruleKey)) {
83 throw new IllegalArgumentException("No key is defined in Rule annotation of " + ruleClass);
85 NewRule rule = repository.rule(ruleKey);
87 throw new IllegalStateException("No rule was created for " + ruleClass + " in " + repository.key());
90 // Check whether it is a Rule Template.
91 rule.setTemplate(AnnotationUtils.getAnnotation(ruleClass, RuleTemplate.class) != null);
92 ruleMetadata(ruleClass, rule);
95 private void ruleMetadata(Class<?> ruleClass, NewRule rule) {
96 String metadataKey = rule.key();
97 addHtmlDescription(rule, metadataKey);
98 addMetadata(rule, metadataKey);
102 private void addMetadata(NewRule rule, String metadataKey) {
103 URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_javascript.json");
104 if (resource != null) {
105 RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class);
106 rule.setSeverity(metatada.defaultSeverity.toUpperCase());
107 rule.setName(metatada.title);
108 rule.addTags(metatada.tags);
109 rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase()));
110 if(metatada.remediation != null) {
111 rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions()));
112 rule.setGapDescription(metatada.remediation.linearDesc);
117 private void addHtmlDescription(NewRule rule, String metadataKey) {
118 URL resource = CustomRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_javascript.html");
119 if (resource != null) {
120 rule.setHtmlDescription(readResource(resource));
124 private String readResource(URL resource) {
126 return Resources.toString(resource, Charsets.UTF_8);
127 } catch (IOException e) {
128 throw new IllegalStateException("Failed to read: " + resource, e);
132 private static class RuleMetatada {
136 Remediation remediation;
139 String defaultSeverity;
142 private static class Remediation {
149 public DebtRemediationFunction remediationFunction(DebtRemediationFunctions drf) {
150 if(func.startsWith("Constant")) {
151 return drf.constantPerIssue(constantCost.replace("mn", "min"));
153 if("Linear".equals(func)) {
154 return drf.linear(linearFactor.replace("mn", "min"));
156 return drf.linearWithOffset(linearFactor.replace("mn", "min"), linearOffset.replace("mn", "min"));