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;
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
*/
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;
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<>();
public String name() {
return name;
}
+
+ /**
+ * @since 7.1
+ * @return
+ */
+ public Scope scope() {
+ return scope;
+ }
/**
* @see NewRule#setType(RuleType)
}
}
+ enum Scope {
+ ALL, MAIN, TEST;
+ }
+
@Immutable
class Param {
private final String key;
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;
.setInternalKey("/something")
.setStatus(RuleStatus.BETA)
.setTags("one", "two")
+ .setScope(Scope.ALL)
.addTags("two", "three", "four");
newRepo.createRule("ABC").setName("ABC").setMarkdownDescription("ABC");
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);
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() {