Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

CommonRuleDefinitionsImpl.java 6.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.rule;
  21. import org.sonar.api.resources.Language;
  22. import org.sonar.api.resources.Languages;
  23. import org.sonar.api.rule.Severity;
  24. import org.sonar.api.server.rule.RuleParamType;
  25. import org.sonar.api.server.rule.RulesDefinition;
  26. import static org.sonar.server.rule.CommonRuleKeys.commonRepositoryForLang;
  27. /**
  28. * Declare the few rules that are automatically created by core for all languages. These rules
  29. * check measure values against thresholds defined in Quality profiles.
  30. */
  31. // this class must not be mixed with other RulesDefinition so it does implement the interface RulesDefinitions.
  32. // It replaces the common-rules that are still embedded within plugins.
  33. public class CommonRuleDefinitionsImpl implements CommonRuleDefinitions {
  34. private final Languages languages;
  35. public CommonRuleDefinitionsImpl(Languages languages) {
  36. this.languages = languages;
  37. }
  38. @Override
  39. public void define(RulesDefinition.Context context) {
  40. for (Language language : languages.all()) {
  41. RulesDefinition.NewRepository repo = context.createRepository(commonRepositoryForLang(language.getKey()), language.getKey());
  42. repo.setName("Common " + language.getName());
  43. defineBranchCoverageRule(repo);
  44. defineLineCoverageRule(repo);
  45. defineCommentDensityRule(repo);
  46. defineDuplicatedBlocksRule(repo);
  47. defineFailedUnitTestRule(repo);
  48. defineSkippedUnitTestRule(repo);
  49. repo.done();
  50. }
  51. }
  52. private static void defineBranchCoverageRule(RulesDefinition.NewRepository repo) {
  53. RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE);
  54. rule.setName("Branches should have sufficient coverage by tests")
  55. .addTags("bad-practice")
  56. .setHtmlDescription("An issue is created on a file as soon as the branch coverage on this file is less than the required threshold. "
  57. + "It gives the number of branches to be covered in order to reach the required threshold.")
  58. .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("5min"))
  59. .setGapDescription("number of uncovered conditions")
  60. .setSeverity(Severity.MAJOR);
  61. rule.createParam(CommonRuleKeys.INSUFFICIENT_BRANCH_COVERAGE_PROPERTY)
  62. .setName("The minimum required branch coverage ratio")
  63. .setDefaultValue("65")
  64. .setType(RuleParamType.FLOAT);
  65. }
  66. private static void defineLineCoverageRule(RulesDefinition.NewRepository repo) {
  67. RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE);
  68. rule.setName("Lines should have sufficient coverage by tests")
  69. .addTags("bad-practice")
  70. .setHtmlDescription("An issue is created on a file as soon as the line coverage on this file is less than the required threshold. " +
  71. "It gives the number of lines to be covered in order to reach the required threshold.")
  72. .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min"))
  73. .setGapDescription("number of lines under the coverage threshold")
  74. .setSeverity(Severity.MAJOR);
  75. rule.createParam(CommonRuleKeys.INSUFFICIENT_LINE_COVERAGE_PROPERTY)
  76. .setName("The minimum required line coverage ratio")
  77. .setDefaultValue("65")
  78. .setType(RuleParamType.FLOAT);
  79. }
  80. private static void defineCommentDensityRule(RulesDefinition.NewRepository repo) {
  81. RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY);
  82. rule.setName("Source files should have a sufficient density of comment lines")
  83. .addTags("convention")
  84. .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. " +
  85. "The number of comment lines to be written in order to reach the required threshold is provided by each issue message.")
  86. .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("2min"))
  87. .setGapDescription("number of lines required to meet minimum density")
  88. .setSeverity(Severity.MAJOR);
  89. rule.createParam(CommonRuleKeys.INSUFFICIENT_COMMENT_DENSITY_PROPERTY)
  90. .setName("The minimum required comment density")
  91. .setDefaultValue("25")
  92. .setType(RuleParamType.FLOAT);
  93. }
  94. private static void defineDuplicatedBlocksRule(RulesDefinition.NewRepository repo) {
  95. RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.DUPLICATED_BLOCKS);
  96. rule.setName("Source files should not have any duplicated blocks")
  97. .addTags("pitfall")
  98. .setHtmlDescription("An issue is created on a file as soon as there is at least one block of duplicated code on this file")
  99. .setDebtRemediationFunction(rule.debtRemediationFunctions().linearWithOffset("10min", "10min"))
  100. .setGapDescription("number of duplicate blocks")
  101. .setSeverity(Severity.MAJOR);
  102. }
  103. private static void defineFailedUnitTestRule(RulesDefinition.NewRepository repo) {
  104. RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.FAILED_UNIT_TESTS);
  105. rule
  106. .setName("Failed unit tests should be fixed")
  107. .addTags("bug")
  108. .setHtmlDescription("Test failures or errors generally indicate that regressions have been introduced. "
  109. + "Those tests should be handled as soon as possible to reduce the cost to fix the corresponding regressions.")
  110. .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("10min"))
  111. .setGapDescription("number of failed tests")
  112. .setSeverity(Severity.MAJOR);
  113. }
  114. private static void defineSkippedUnitTestRule(RulesDefinition.NewRepository repo) {
  115. RulesDefinition.NewRule rule = repo.createRule(CommonRuleKeys.SKIPPED_UNIT_TESTS);
  116. rule.setName("Skipped unit tests should be either removed or fixed")
  117. .addTags("pitfall")
  118. .setHtmlDescription("Skipped unit tests are considered as dead code. Either they should be activated again (and updated) or they should be removed.")
  119. .setDebtRemediationFunction(rule.debtRemediationFunctions().linear("10min"))
  120. .setGapDescription("number of skipped tests")
  121. .setSeverity(Severity.MAJOR);
  122. }
  123. }