]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10321 Add scope to rule definition API
authorDuarte Meneses <duarte.meneses@sonarsource.com>
Thu, 25 Jan 2018 14:18:48 +0000 (15:18 +0100)
committerGrégoire Aubert <gregoire.aubert@sonarsource.com>
Wed, 7 Feb 2018 10:32:38 +0000 (11:32 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/server/rule/RulesDefinition.java
sonar-plugin-api/src/test/java/org/sonar/api/server/rule/RulesDefinitionTest.java

index baf4bb04657393fbf0bc73453c3c85b371c809f3..58f3d47b9e6eb857684649ff4c8ca606e3f5e426 100644 (file)
@@ -693,6 +693,7 @@ public interface RulesDefinition {
     private final Map<String, NewParam> paramsByKey = new HashMap<>();
     private final DebtRemediationFunctions functions;
     private boolean activatedByDefault;
+    private Scope scope;
 
     private NewRule(@Nullable String pluginKey, String repoKey, String key) {
       this.pluginKey = pluginKey;
@@ -705,6 +706,22 @@ public interface RulesDefinition {
       return this.key;
     }
 
+    /**
+     * @since 7.1
+     */
+    @CheckForNull
+    public Scope scope() {
+      return this.scope;
+    }
+
+    /**
+     * @since 7.1
+     */
+    public NewRule setScope(Scope scope) {
+      this.scope = scope;
+      return this;
+    }
+
     /**
      * Required rule name
      */
@@ -950,6 +967,7 @@ public interface RulesDefinition {
     private final Map<String, Param> params;
     private final RuleStatus status;
     private final boolean activatedByDefault;
+    private final Scope scope;
 
     private Rule(Repository repository, NewRule newRule) {
       this.pluginKey = newRule.pluginKey;
@@ -965,6 +983,7 @@ public interface RulesDefinition {
       this.status = newRule.status;
       this.debtRemediationFunction = newRule.debtRemediationFunction;
       this.gapDescription = newRule.gapDescription;
+      this.scope = newRule.scope == null ? Scope.MAIN : newRule.scope;
       this.type = newRule.type == null ? RuleTagsToTypeConverter.convert(newRule.tags) : newRule.type;
       this.tags = ImmutableSortedSet.copyOf(Sets.difference(newRule.tags, RuleTagsToTypeConverter.RESERVED_TAGS));
       Map<String, Param> paramsBuilder = new HashMap<>();
@@ -994,6 +1013,14 @@ public interface RulesDefinition {
     public String name() {
       return name;
     }
+    
+    /**
+     * @since 7.1
+     * @return
+     */
+    public Scope scope() {
+      return scope;
+    }
 
     /**
      * @see NewRule#setType(RuleType)
@@ -1153,6 +1180,10 @@ public interface RulesDefinition {
     }
   }
 
+  enum Scope {
+    ALL, MAIN, TEST;
+  }
+
   @Immutable
   class Param {
     private final String key;
index 7243bbffe3fa8af0d781166d891946068cb26fd5..7a426129bb76c9dd95e89ad6b6c8a78fccd1603a 100644 (file)
@@ -27,6 +27,7 @@ import org.sonar.api.rule.RuleStatus;
 import org.sonar.api.rule.Severity;
 import org.sonar.api.rules.RuleType;
 import org.sonar.api.server.debt.DebtRemediationFunction;
+import org.sonar.api.server.rule.RulesDefinition.Scope;
 import org.sonar.api.utils.log.LogTester;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -81,6 +82,7 @@ public class RulesDefinitionTest {
       .setInternalKey("/something")
       .setStatus(RuleStatus.BETA)
       .setTags("one", "two")
+      .setScope(Scope.ALL)
       .addTags("two", "three", "four");
 
     newRepo.createRule("ABC").setName("ABC").setMarkdownDescription("ABC");
@@ -90,6 +92,7 @@ public class RulesDefinitionTest {
     assertThat(repo.rules()).hasSize(2);
 
     RulesDefinition.Rule rule = repo.rule("NPE");
+    assertThat(rule.scope()).isEqualTo(Scope.ALL);
     assertThat(rule.key()).isEqualTo("NPE");
     assertThat(rule.name()).isEqualTo("Detect NPE");
     assertThat(rule.severity()).isEqualTo(Severity.BLOCKER);
@@ -206,6 +209,16 @@ public class RulesDefinitionTest {
     RulesDefinition.Rule rule = context.repository("findbugs").rule("NPE");
     assertThat(rule.name()).isEqualTo("NullPointer");
   }
+  
+  @Test
+  public void default_scope_should_be_main() {
+    RulesDefinition.NewRepository newFindbugs = context.createRepository("findbugs", "java");
+    newFindbugs.createRule("key").setName("name").setHtmlDescription("NPE");
+    newFindbugs.done();
+
+    RulesDefinition.Rule rule = context.repository("findbugs").rule("key");
+    assertThat(rule.scope()).isEqualTo(Scope.MAIN);
+  }
 
   @Test
   public void add_rules_to_existing_repository() {