.setSeverity(severity) .setName(name) .setInternalKey(internalKey) .setTags(tags.toArray(new String[tags.size()])) .setTemplate(template)
.setHtmlDescription("Detect <code>java.lang.NullPointerException</code>") .setSeverity(Severity.BLOCKER) .setInternalKey("/something") .setStatus(RuleStatus.BETA) .setTags("one", "two")
.setHtmlDescription("Detect <code>java.lang.NullPointerException</code>") .setSeverity(Severity.BLOCKER) .setInternalKey("/something") .setStatus(RuleStatus.BETA) .setTags("one", "two")
.setSeverity(severity) .setName(name) .setInternalKey(internalKey) .setTags(tags.toArray(new String[tags.size()])) .setTemplate(cardinality == Cardinality.MULTIPLE);
newRule.setName(ruleName(repository.getKey(), rule)); newRule.setHtmlDescription(ruleDescription(repository.getKey(), rule)); newRule.setInternalKey(rule.getConfigKey()); newRule.setTemplate(rule.isTemplate()); newRule.setSeverity(rule.getSeverity().toString());
@Override public void define(Context context) { NewRepository repository = context .createRepository(DelphiPmdConstants.REPOSITORY_KEY, DelphiLanguage.KEY) .setName(DelphiPmdConstants.REPOSITORY_NAME); List<org.sonar.api.rules.Rule> rules = DelphiRulesUtils.getInitialReferential(); // TODO Review // https://github.com/SonarCommunity/sonar-pmd/blob/master/src/main/java/org/sonar/plugins/pmd/PmdRulesDefinition.java for (org.sonar.api.rules.Rule rule : rules) { NewRule newRule = repository.createRule(rule.getKey()) .setName(rule.getName()) .setHtmlDescription(rule.getDescription()) .setInternalKey(rule.getConfigKey()) .setSeverity(rule.getSeverity().name()); for (RuleParam param : rule.getParams()) { newRule.createParam(param.getKey()) .setDefaultValue(param.getDefaultValue()) .setType(RuleParamType.parse(param.getType())) .setDescription(param.getDescription()); } } SqaleXmlLoader.load(repository, "/org/sonar/plugins/delphi/sqale/delphi-model.xml"); repository.done(); }
@SuppressWarnings("rawtypes") @Override public void define(final Context context) { final NewRepository repository = context.createRepository(REPOSITORY_KEY, Java.KEY) .setName(REPOSITORY_NAME); final List<Class> checkClasses = ImmutableList.<Class> builder() .addAll(SmellChecksRegistrar.checkClasses()) .build(); new AnnotationBasedRulesDefinition(repository, Java.KEY).addRuleClasses(false, checkClasses); for (final NewRule rule : repository.rules()) { final String metadataKey = rule.key(); // Setting internal key is essential for rule templates (see SONAR-6162), and it is not done by AnnotationBasedRulesDefinition from // sslr-squid-bridge version 2.5.1: rule.setInternalKey(metadataKey); rule.setHtmlDescription(readRuleDefinitionResource(metadataKey + ".html")); addMetadata(rule, metadataKey); } repository.done(); }
@Override public void define(Context context) { NewRepository repository = context .createRepository(REPOSITORY_KEY, Java.KEY) .setName("Finger Java Custom Rules"); new AnnotationBasedRulesDefinition(repository, Java.KEY) .addRuleClasses(/* don't fail if no SQALE annotations */ false, RulesList.getChecks()); for (NewRule rule : repository.rules()) { String metadataKey = rule.key(); System.out.println(metadataKey); // Setting internal key is essential for rule templates (see SONAR-6162), and it is not done by AnnotationBasedRulesDefinition from // sslr-squid-bridge version 2.5.1: rule.setInternalKey(metadataKey); rule.setHtmlDescription(readRuleDefinitionResource(metadataKey + ".html")); addMetadata(rule, metadataKey); } repository.done(); }
private String ruleMetadata(Class<?> ruleClass, NewRule rule) { String metadataKey = rule.key(); org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class); if (rspecKeyAnnotation != null) { metadataKey = rspecKeyAnnotation.value(); rule.setInternalKey(metadataKey); } addHtmlDescription(rule, metadataKey); addMetadata(rule, metadataKey); return metadataKey; }
@Override public void define(Context context) { NewRepository repository = context .createRepository(CheckList.REPOSITORY_KEY, Java.KEY) .setName("SonarQube"); AnnotationBasedRulesDefinition.load(repository, Java.KEY, CheckList.getChecks()); for (NewRule rule : repository.rules()) { //FIXME: set internal key to key to ensure rule templates works properly : should be removed when SONAR-6162 is fixed. rule.setInternalKey(rule.key()); } repository.done(); } }
private static String rspecKey(Class<?> ruleClass, NewRule rule) { org.sonar.java.RspecKey rspecKeyAnnotation = AnnotationUtils.getAnnotation(ruleClass, org.sonar.java.RspecKey.class); if (rspecKeyAnnotation != null) { String rspecKey = rspecKeyAnnotation.value(); rule.setInternalKey(rspecKey); return rspecKey; } else { return rule.key(); } }
.setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL)
newRule.setName(ruleName(repository.getKey(), rule)); newRule.setHtmlDescription(ruleDescription(repository.getKey(), rule)); newRule.setInternalKey(rule.getConfigKey()); newRule.setTemplate(rule.isTemplate()); newRule.setSeverity(rule.getSeverity().toString());
.setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL)
@Test public void declaring_a_rule_with_an_existing_RuleKey_still_used_should_throw_IAE() { expectedException.expect(IllegalArgumentException.class); expectedException.expectMessage("The rule 'newKey1' of repository 'fake' is declared several times"); execute(context -> { NewRepository repo = context.createRepository("fake", "java"); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "newKey1") .setStatus(RuleStatus.BETA); repo.done(); }); }
@Test public void declaring_a_rule_with_a_deprecated_RuleKey_still_used_should_throw_ISE() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("The following rule keys are declared both as deprecated and used key [fake:newKey1]"); execute(context -> { NewRepository repo = context.createRepository("fake", "java"); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA); repo.createRule("newKey2") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "newKey1") .setStatus(RuleStatus.BETA); repo.done(); }); }
@Override public void define(Context context) { NewRepository repo = context.createRepository("fake", "java"); NewRule rule1 = repo.createRule(RULE_KEY1.rule()) .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setScope(RuleScope.ALL) .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA) .setGapDescription("squid.S115.effortToFix"); rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("5d", "10h")); rule1.createParam("param1").setDescription("parameter one").setDefaultValue("default1"); rule1.createParam("param2").setDescription("parameter two").setDefaultValue("default2"); repo.createRule(HOTSPOT_RULE_KEY.rule()) .setName("Hotspot") .setHtmlDescription("Minimal hotspot") .setType(RuleType.SECURITY_HOTSPOT) .addOwaspTop10(OwaspTop10.A1, OwaspTop10.A3) .addCwe(1, 123, 863); repo.createRule(RULE_KEY2.rule()) .setName("Two") .setHtmlDescription("Minimal rule"); repo.done(); } }
@Test public void declaring_two_rules_with_same_deprecated_RuleKey_should_throw_ISE() { expectedException.expect(IllegalStateException.class); expectedException.expectMessage("The following deprecated rule keys are declared at least twice [fake:old]"); execute(context -> { NewRepository repo = context.createRepository("fake", "java"); repo.createRule("newKey1") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "old") .setStatus(RuleStatus.BETA); repo.createRule("newKey2") .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setType(RuleType.CODE_SMELL) .addDeprecatedRuleKey("fake", "old") .setStatus(RuleStatus.BETA); repo.done(); }); }
@Override public void define(Context context) { NewRepository repo = context.createRepository("fake", "java"); // almost all the attributes of rule1 are changed NewRule rule1 = repo.createRule(RULE_KEY1.rule()) .setName("One v2") .setHtmlDescription("Description of One v2") .setSeverity(INFO) .setInternalKey("config1 v2") // tag2 and tag3 removed, tag4 added .setTags("tag1", "tag4") .setType(RuleType.BUG) .setStatus(READY) .setGapDescription("squid.S115.effortToFix.v2"); rule1.setDebtRemediationFunction(rule1.debtRemediationFunctions().linearWithOffset("6d", "2h")); rule1.createParam("param1").setDescription("parameter one v2").setDefaultValue("default1 v2"); rule1.createParam("param2").setDescription("parameter two v2").setDefaultValue("default2 v2"); // rule2 is dropped, rule3 is new repo.createRule(RULE_KEY3.rule()) .setName("Three") .setHtmlDescription("Rule Three"); repo.done(); } }
@Override public void define(Context context) { NewRepository repo = context.createExternalRepository("eslint", "js"); repo.createRule(RULE_KEY1.rule()) .setName("One") .setHtmlDescription("Description of One") .setSeverity(BLOCKER) .setInternalKey("config1") .setTags("tag1", "tag2", "tag3") .setScope(RuleScope.ALL) .setType(RuleType.CODE_SMELL) .setStatus(RuleStatus.BETA); repo.createRule(EXTERNAL_HOTSPOT_RULE_KEY.rule()) .setName("Hotspot") .setHtmlDescription("Minimal hotspot") .setType(RuleType.SECURITY_HOTSPOT) .addOwaspTop10(OwaspTop10.A1, OwaspTop10.A3) .addCwe(1, 123, 863); repo.done(); } }