]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5417 Add rule name to active rule
authorJulien HENRY <julien.henry@sonarsource.com>
Wed, 23 Jul 2014 18:55:46 +0000 (20:55 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Wed, 23 Jul 2014 18:56:27 +0000 (20:56 +0200)
12 files changed:
sonar-batch-protocol/src/main/java/org/sonar/batch/protocol/input/ActiveRule.java
sonar-batch-protocol/src/test/java/org/sonar/batch/protocol/input/ProjectReferentialsTest.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/rule/ActiveRulesProvider.java
sonar-batch/src/main/java/org/sonar/batch/scan2/DefaultSensorContext.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/issues/IssuesOnDirMediumTest.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/ActiveRule.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/DefaultActiveRule.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/rule/internal/NewActiveRule.java
sonar-plugin-api/src/test/java/org/sonar/api/batch/rule/internal/ActiveRulesBuilderTest.java

index 144a34b292b50a2674f9cbe22a62292b55ccb642..6c1992ac1e2e8dc1141f294e43c2130edf3af11a 100644 (file)
@@ -26,12 +26,13 @@ import java.util.Map;
 
 public class ActiveRule {
   private final String repositoryKey, ruleKey;
-  private final String severity, internalKey, language;
+  private final String name, severity, internalKey, language;
   private final Map<String, String> params = new HashMap<String, String>();
 
-  public ActiveRule(String repositoryKey, String ruleKey, String severity, String internalKey, String language) {
+  public ActiveRule(String repositoryKey, String ruleKey, String name, String severity, String internalKey, String language) {
     this.repositoryKey = repositoryKey;
     this.ruleKey = ruleKey;
+    this.name = name;
     this.severity = severity;
     this.internalKey = internalKey;
     this.language = language;
@@ -45,6 +46,10 @@ public class ActiveRule {
     return ruleKey;
   }
 
+  public String name() {
+    return name;
+  }
+
   public String severity() {
     return severity;
   }
index 831f5e12cefd50e43627598309e41134412bedaa..328939ca23d0d08330083d5e6882cd65f3f46671 100644 (file)
@@ -38,7 +38,7 @@ public class ProjectReferentialsTest {
     ref.addQProfile(new QProfile("squid-java", "Java", "java", new SimpleDateFormat("dd/MM/yyyy").parse("14/03/1984")));
     ref.addSettings("foo", new HashMap<String, String>());
     ref.settings("foo").put("prop", "value");
-    ref.addActiveRule(new ActiveRule("repo", "rule", "MAJOR", "rule", "java"));
+    ref.addActiveRule(new ActiveRule("repo", "rule", "Rule", "MAJOR", "rule", "java"));
     ref.setTimestamp(10);
 
     System.out.println(ref.toJson());
@@ -46,7 +46,7 @@ public class ProjectReferentialsTest {
       .assertEquals(
         "{timestamp:10,"
           + "qprofilesByLanguage:{java:{key:\"squid-java\",name:Java,language:java,rulesUpdatedAt:\"Mar 14, 1984 12:00:00 AM\"}},"
-          + "activeRules:[{repositoryKey:repo,ruleKey:rule,severity:MAJOR,internalKey:rule,language:java,params:{}}],"
+          + "activeRules:[{repositoryKey:repo,ruleKey:rule,name:Rule,severity:MAJOR,internalKey:rule,language:java,params:{}}],"
           + "settingsByModule:{foo:{prop:value}}}",
         ref.toJson(), true);
   }
index 3ca6ff976343dc8595ae4baadc37c4728435ce4a..4405eb259e8245523cbc2af8bf64513a84278bc7 100644 (file)
@@ -96,7 +96,7 @@ public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoad
           } else {
             internalKey = rule.getConfigKey();
           }
-          ActiveRule activeRule = new ActiveRule(rule.ruleKey().repository(), rule.ruleKey().rule(), activeDto.getSeverityString(), internalKey, rule.getLanguage());
+          ActiveRule activeRule = new ActiveRule(rule.ruleKey().repository(), rule.ruleKey().rule(), rule.getName(), activeDto.getSeverityString(), internalKey, rule.getLanguage());
 
           // load parameter values
           for (ActiveRuleParamDto paramDto : paramDtosByActiveRuleId.get(activeDto.getId())) {
index 9b6b009d3df86b2be852bbd81c93a6fe21c7a515..a539b74407355d0fcebf623ec5f1315100d7f0eb 100644 (file)
@@ -48,6 +48,7 @@ public class ActiveRulesProvider extends ProviderAdapter {
     ActiveRulesBuilder builder = new ActiveRulesBuilder();
     for (ActiveRule activeRule : ref.activeRules()) {
       NewActiveRule newActiveRule = builder.create(RuleKey.of(activeRule.repositoryKey(), activeRule.ruleKey()));
+      newActiveRule.setName(activeRule.name());
       newActiveRule.setSeverity(activeRule.severity());
       newActiveRule.setLanguage(activeRule.language());
       newActiveRule.setInternalKey(activeRule.internalKey());
index c2119b18d0205a6fb766eee52e5679e3d1b0fcd3..f54dab6af55425a344fbd78e7f96c44036cf0918 100644 (file)
@@ -26,7 +26,6 @@ import org.sonar.api.batch.fs.InputFile;
 import org.sonar.api.batch.measure.Metric;
 import org.sonar.api.batch.rule.ActiveRule;
 import org.sonar.api.batch.rule.ActiveRules;
-import org.sonar.api.batch.rule.Rule;
 import org.sonar.api.batch.sensor.SensorContext;
 import org.sonar.api.batch.sensor.issue.Issue;
 import org.sonar.api.batch.sensor.issue.IssueBuilder;
@@ -129,22 +128,16 @@ public class DefaultSensorContext implements SensorContext {
       resourceKey = def.getKey();
     }
     RuleKey ruleKey = issue.ruleKey();
-    // TODO we need a Rule referential on batch side
-    Rule rule = null;
-    // Rule rule = rules.find(ruleKey);
-    // if (rule == null) {
-    // throw MessageException.of(String.format("The rule '%s' does not exist.", ruleKey));
-    // }
     ActiveRule activeRule = activeRules.find(ruleKey);
     if (activeRule == null) {
       // rule does not exist or is not enabled -> ignore the issue
       return false;
     }
-    if (/* Strings.isNullOrEmpty(rule.name()) && */Strings.isNullOrEmpty(issue.message())) {
+    if (Strings.isNullOrEmpty(activeRule.name()) && Strings.isNullOrEmpty(issue.message())) {
       throw MessageException.of(String.format("The rule '%s' has no name and the related issue has no message.", ruleKey));
     }
 
-    updateIssue((DefaultIssue) issue, activeRule, rule);
+    updateIssue((DefaultIssue) issue, activeRule);
 
     if (issueFilters.accept(SensorContextAdaptor.toDefaultIssue(def.getKey(), resourceKey, issue), null)) {
       issueCache.put(def.getKey(), resourceKey, (DefaultIssue) issue);
@@ -154,9 +147,9 @@ public class DefaultSensorContext implements SensorContext {
     return false;
   }
 
-  private void updateIssue(DefaultIssue issue, ActiveRule activeRule, Rule rule) {
+  private void updateIssue(DefaultIssue issue, ActiveRule activeRule) {
     if (Strings.isNullOrEmpty(issue.message())) {
-      issue.setMessage(rule.name());
+      issue.setMessage(activeRule.name());
     }
 
     if (issue.severity() == null) {
index ace6681869b79918dff7d64359276d8387b51218..674f472d0b5670caf7c9c1ac1ab673981f99eb97 100644 (file)
@@ -50,7 +50,7 @@ public class FileSystemMediumTest {
   public BatchMediumTester tester = BatchMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "MAJOR", "xoo", "xoo"))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "One issue per line", "MAJOR", "xoo", "xoo"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
 
index 3fec3ccb251f3cd2418e36c1cada5319c658ffd8..875e7e6d510d64f2e5e6dad2ff9775c04acbe255 100644 (file)
@@ -45,7 +45,7 @@ public class IssuesMediumTest {
   public BatchMediumTester tester = BatchMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "MAJOR", "OneIssuePerLine.internal", "xoo"))
+    .activateRule(new ActiveRule("xoo", "OneIssuePerLine", "One issue per line", "MAJOR", "OneIssuePerLine.internal", "xoo"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
 
index bbd1aefd719d06bf604c5af6657edb102d4e58ac..fdf6b15e857d4a8a3d2fcd96fc6f9507cb03ea43 100644 (file)
@@ -44,7 +44,7 @@ public class IssuesOnDirMediumTest {
   public BatchMediumTester tester = BatchMediumTester.builder()
     .registerPlugin("xoo", new XooPlugin())
     .addDefaultQProfile("xoo", "Sonar Way")
-    .activateRule(new ActiveRule("xoo", "OneIssueOnDirPerFile", "MINOR", "xoo", "xoo"))
+    .activateRule(new ActiveRule("xoo", "OneIssueOnDirPerFile", "One issue per line", "MINOR", "xoo", "xoo"))
     .bootstrapProperties(ImmutableMap.of("sonar.analysis.mode", "sensor"))
     .build();
 
index 3e3d18651e95301096b22567b28ce9be7d34844c..d24fa2eed841c4b83553b517d9f27ec53e6b5f48 100644 (file)
@@ -22,6 +22,7 @@ package org.sonar.api.batch.rule;
 import org.sonar.api.rule.RuleKey;
 
 import javax.annotation.CheckForNull;
+
 import java.util.Map;
 
 /**
@@ -32,6 +33,12 @@ public interface ActiveRule {
 
   RuleKey ruleKey();
 
+  /**
+   * Name of the rule.
+   * @since 4.5
+   */
+  String name();
+
   /**
    * Non-null severity.
    * @see org.sonar.api.rule.Severity
index 5eeea77687d7c22f5079e7a51d05d2344bba4087..b37fc6a88907ac6597c6f675d10da1958b30fdfd 100644 (file)
@@ -24,16 +24,19 @@ import org.sonar.api.batch.rule.ActiveRule;
 import org.sonar.api.rule.RuleKey;
 
 import javax.annotation.concurrent.Immutable;
+
 import java.util.Map;
 
 @Immutable
 class DefaultActiveRule implements ActiveRule {
   private final RuleKey ruleKey;
+  private final String name;
   private final String severity, internalKey, language;
   private final Map<String, String> params;
 
   DefaultActiveRule(NewActiveRule newActiveRule) {
     this.severity = newActiveRule.severity;
+    this.name = newActiveRule.name;
     this.internalKey = newActiveRule.internalKey;
     this.ruleKey = newActiveRule.ruleKey;
     this.params = ImmutableMap.copyOf(newActiveRule.params);
@@ -45,6 +48,11 @@ class DefaultActiveRule implements ActiveRule {
     return ruleKey;
   }
 
+  @Override
+  public String name() {
+    return name;
+  }
+
   @Override
   public String severity() {
     return severity;
index dd4b53d5953a3077c9fa52e1550dbff381334533..3f20d89bf3f3a666a88d6180c7f58f83932d3530 100644 (file)
@@ -33,6 +33,7 @@ import java.util.Map;
  */
 public class NewActiveRule {
   final RuleKey ruleKey;
+  String name;
   String severity = Severity.defaultSeverity();
   Map<String, String> params = new HashMap<String, String>();
   String internalKey, language;
@@ -43,6 +44,11 @@ public class NewActiveRule {
     this.ruleKey = ruleKey;
   }
 
+  public NewActiveRule setName(String name) {
+    this.name = name;
+    return this;
+  }
+
   public NewActiveRule setSeverity(@Nullable String severity) {
     this.severity = StringUtils.defaultIfBlank(severity, Severity.defaultSeverity());
     return this;
index 584b1ea7663cc24de13fbb82721a6cf2f082a446..3d2ac27b8a1339d2fa3594bce71fd481507ef593 100644 (file)
@@ -40,6 +40,7 @@ public class ActiveRulesBuilderTest {
   public void build_rules() throws Exception {
     ActiveRules activeRules = new ActiveRulesBuilder()
       .create(RuleKey.of("squid", "S0001"))
+      .setName("My Rule")
       .setSeverity(Severity.CRITICAL)
       .setInternalKey("__S0001__")
       .setParam("min", "20")
@@ -58,6 +59,7 @@ public class ActiveRulesBuilderTest {
     ActiveRule squid1 = activeRules.find(RuleKey.of("squid", "S0001"));
     assertThat(squid1.ruleKey().repository()).isEqualTo("squid");
     assertThat(squid1.ruleKey().rule()).isEqualTo("S0001");
+    assertThat(squid1.name()).isEqualTo("My Rule");
     assertThat(squid1.severity()).isEqualTo(Severity.CRITICAL);
     assertThat(squid1.internalKey()).isEqualTo("__S0001__");
     assertThat(squid1.params()).hasSize(1);