/** * @see RuleTagFormat */ public NewRule setTags(String... list) { tags.clear(); addTags(list); return this; }
.addOwaspTop10(RulesDefinition.OwaspTop10.A1, RulesDefinition.OwaspTop10.A3) .addCwe(1, 2, 123) .addTags("two", "three", "four");
.addOwaspTop10(RulesDefinition.OwaspTop10.A1, RulesDefinition.OwaspTop10.A3) .addCwe(1, 2, 123) .addTags("two", "three", "four");
private void addMetadata(NewRule rule, @Nullable RuleMetadata metadata) { if (metadata == null) { return; } rule.setSeverity(metadata.defaultSeverity.toUpperCase(Locale.US)); rule.setName(metadata.title); rule.addTags(metadata.tags); if (metadata.isSecurityHotspot() && !supportsSecurityHotspots) { rule.setType(RuleType.VULNERABILITY); } else { rule.setType(RuleType.valueOf(metadata.type)); } rule.setStatus(RuleStatus.valueOf(metadata.status.toUpperCase(Locale.US))); if (metadata.remediation != null) { rule.setDebtRemediationFunction(metadata.remediation.remediationFunction(rule.debtRemediationFunctions())); rule.setGapDescription(metadata.remediation.linearDesc); } if (supportsSecurityHotspots) { addSecurityStandards(rule, metadata.securityStandards); } }
private void addMetadata(NewRule rule, String metadataKey) { URL resource = MyJavaRulesDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json"); if (resource != null) { RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class); rule.setSeverity(metatada.defaultSeverity.toUpperCase(Locale.US)); rule.setName(metatada.title); rule.addTags(metatada.tags); rule.setType(RuleType.valueOf(metatada.type)); rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase(Locale.US))); if (metatada.remediation != null) { rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions())); rule.setGapDescription(metatada.remediation.linearDesc); } } }
private static void defineCommentDensityRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY); rule.setName("Source files should have a sufficient density of comment lines") .addTags("convention") .setHtmlDescription("An issue is created on a file as soon as the density of comment lines on this file is less than the required threshold. " + "The number of comment lines to be written in order to reach the required threshold is provided by each issue message.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines required to meet minimum density") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY) .setName("The minimum required comment density") .setDefaultValue("25") .setType(RuleParamType.FLOAT); }
private static void defineLineCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE); rule.setName("Lines should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the line coverage on this file is less than the required threshold. " + "It gives the number of lines to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines under the coverage threshold") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE_PROPERTY) .setName("The minimum required line coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private void addMetadata(NewRule rule, String metadataKey) { URL resource = SonarDefinition.class.getResource(RESOURCE_BASE_PATH + "/" + metadataKey + "_java.json"); if (resource != null) { RuleMetatada metatada = gson.fromJson(readResource(resource), RuleMetatada.class); rule.setSeverity(metatada.defaultSeverity.toUpperCase(Locale.US)); rule.setName(metatada.title); rule.addTags(metatada.tags); rule.setType(RuleType.valueOf(metatada.type)); rule.setStatus(RuleStatus.valueOf(metatada.status.toUpperCase(Locale.US))); if (metatada.remediation != null) { rule.setDebtRemediationFunction(metatada.remediation.remediationFunction(rule.debtRemediationFunctions())); rule.setGapDescription(metatada.remediation.linearDesc); } } }
private static void defineBranchCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE); rule.setName("Branches should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the branch coverage on this file is less than the required threshold." + "It gives the number of branches to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("5min")) .setGapDescription("number of uncovered conditions") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE_PROPERTY) .setName("The minimum required branch coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private static void defineFailedUnitTestRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.FAILED_UNIT_TESTS); rule .setName("Failed unit tests should be fixed") .addTags("bug") .setHtmlDescription( "Test failures or errors generally indicate that regressions have been introduced. Those tests should be handled as soon as possible to reduce the cost to fix the corresponding regressions.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("10min")) .setGapDescription("number of failed tests") .setSeverity(Severity.MAJOR); }
/** * @see RuleTagFormat */ public NewRule setTags(String... list) { tags.clear(); addTags(list); return this; }
private static void defineDuplicatedBlocksRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.DUPLICATED_BLOCKS); rule.setName("Source files should not have any duplicated blocks") .addTags("pitfall") .setHtmlDescription("An issue is created on a file as soon as there is at least one block of duplicated code on this file") .setDebtRemediationFunction(rule.debtRemediationFunctions().linearWithOffset("10min", "10min")) .setGapDescription("number of duplicate blocks") .setSeverity(Severity.MAJOR); }
/** * @see RuleTagFormat */ public NewRule setTags(String... list) { tags.clear(); addTags(list); return this; }
private static void defineSkippedUnitTestRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.SKIPPED_UNIT_TESTS); rule.setName("Skipped unit tests should be either removed or fixed") .addTags("pitfall") .setHtmlDescription("Skipped unit tests are considered as dead code. Either they should be activated again (and updated) or they should be removed.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("10min")) .setGapDescription("number of skipped tests") .setSeverity(Severity.MAJOR); } }
private static void defineBranchCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE); rule.setName("Branches should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the branch coverage on this file is less than the required threshold. " + "It gives the number of branches to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("5min")) .setGapDescription("number of uncovered conditions") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE_PROPERTY) .setName("The minimum required branch coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private static void defineCommentDensityRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY); rule.setName("Source files should have a sufficient density of comment lines") .addTags("convention") .setHtmlDescription("An issue is created on a file as soon as the density of comment lines on this file is less than the required threshold. " + "The number of comment lines to be written in order to reach the required threshold is provided by each issue message.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines required to meet minimum density") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY) .setName("The minimum required comment density") .setDefaultValue("25") .setType(RuleParamType.FLOAT); }
private static void defineLineCoverageRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE); rule.setName("Lines should have sufficient coverage by tests") .addTags("bad-practice") .setHtmlDescription("An issue is created on a file as soon as the line coverage on this file is less than the required threshold. " + "It gives the number of lines to be covered in order to reach the required threshold.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min")) .setGapDescription("number of lines under the coverage threshold") .setSeverity(Severity.MAJOR); rule.createParam(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE_PROPERTY) .setName("The minimum required line coverage ratio") .setDefaultValue("65") .setType(RuleParamType.FLOAT); }
private static void defineFailedUnitTestRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.FAILED_UNIT_TESTS); rule .setName("Failed unit tests should be fixed") .addTags("bug") .setHtmlDescription( "Test failures or errors generally indicate that regressions have been introduced. Those tests should be handled as soon as possible to reduce the cost to fix the corresponding regressions.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("10min")) .setGapDescription("number of failed tests") .setSeverity(Severity.MAJOR); }
private static void defineDuplicatedBlocksRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.DUPLICATED_BLOCKS); rule.setName("Source files should not have any duplicated blocks") .addTags("pitfall") .setHtmlDescription("An issue is created on a file as soon as there is at least one block of duplicated code on this file") .setDebtRemediationFunction(rule.debtRemediationFunctions().linearWithOffset("10min", "10min")) .setGapDescription("number of duplicate blocks") .setSeverity(Severity.MAJOR); }
private static void defineSkippedUnitTestRule(RulesDefinition.NewRepository repo) { RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.SKIPPED_UNIT_TESTS); rule.setName("Skipped unit tests should be either removed or fixed") .addTags("pitfall") .setHtmlDescription("Skipped unit tests are considered as dead code. Either they should be activated again (and updated) or they should be removed.") .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("10min")) .setGapDescription("number of skipped tests") .setSeverity(Severity.MAJOR); } }