You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

XooRulesDefinition.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.xoo.rule;
  21. import javax.annotation.Nullable;
  22. import org.sonar.api.SonarRuntime;
  23. import org.sonar.api.rule.RuleScope;
  24. import org.sonar.api.rules.RuleType;
  25. import org.sonar.api.server.rule.RuleParamType;
  26. import org.sonar.api.server.rule.RulesDefinition;
  27. import org.sonar.api.server.rule.RulesDefinitionAnnotationLoader;
  28. import org.sonar.api.utils.Version;
  29. import org.sonar.xoo.Xoo;
  30. import org.sonar.xoo.Xoo2;
  31. import org.sonar.xoo.checks.Check;
  32. /**
  33. * Define all the coding rules that are supported on the repositories named "xoo" and "xoo2"
  34. */
  35. public class XooRulesDefinition implements RulesDefinition {
  36. public static final String XOO_REPOSITORY = "xoo";
  37. public static final String XOO2_REPOSITORY = "xoo2";
  38. private static final String TEN_MIN = "10min";
  39. @Nullable
  40. private final Version version;
  41. public XooRulesDefinition() {
  42. this(null);
  43. }
  44. public XooRulesDefinition(@Nullable SonarRuntime sonarRuntime) {
  45. this.version = sonarRuntime != null ? sonarRuntime.getApiVersion() : null;
  46. }
  47. @Override
  48. public void define(Context context) {
  49. defineRulesXoo(context);
  50. defineRulesXoo2(context);
  51. defineRulesXooExternal(context);
  52. }
  53. private static void defineRulesXoo2(Context context) {
  54. NewRepository repo = context.createRepository(XOO2_REPOSITORY, Xoo2.KEY).setName("Xoo2");
  55. NewRule hasTag = repo.createRule(HasTagSensor.RULE_KEY).setName("Has Tag")
  56. .setHtmlDescription("Search for a given tag in Xoo files");
  57. NewRule oneIssuePerLine = repo.createRule(OneIssuePerLineSensor.RULE_KEY).setName("One Issue Per Line")
  58. .setHtmlDescription("Generate an issue on each line of a file. It requires the metric \"lines\".");
  59. oneIssuePerLine
  60. .setDebtRemediationFunction(hasTag.debtRemediationFunctions().linear("1min"))
  61. .setGapDescription("It takes about 1 minute to an experienced software craftsman to remove a line of code");
  62. repo.done();
  63. }
  64. private void defineRulesXoo(Context context) {
  65. NewRepository repo = context.createRepository(XOO_REPOSITORY, Xoo.KEY).setName("Xoo");
  66. new RulesDefinitionAnnotationLoader().load(repo, Check.ALL);
  67. NewRule hasTag = repo.createRule(HasTagSensor.RULE_KEY).setName("Has Tag")
  68. .setActivatedByDefault(true)
  69. .setHtmlDescription("Search for a given tag in Xoo files");
  70. hasTag
  71. .setDebtRemediationFunction(hasTag.debtRemediationFunctions().constantPerIssue("2min"));
  72. hasTag.createParam("tag")
  73. .setDefaultValue("xoo")
  74. .setDescription("The tag to search for");
  75. NewRule ruleWithParameters = repo.createRule("RuleWithParameters").setName("Rule with parameters")
  76. .setHtmlDescription("Rule containing parameter of different types : boolean, integer, etc. For information, no issue will be linked to this rule.");
  77. ruleWithParameters.createParam("string").setType(RuleParamType.STRING);
  78. ruleWithParameters.createParam("text").setType(RuleParamType.TEXT);
  79. ruleWithParameters.createParam("boolean").setType(RuleParamType.BOOLEAN);
  80. ruleWithParameters.createParam("integer").setType(RuleParamType.INTEGER);
  81. ruleWithParameters.createParam("float").setType(RuleParamType.FLOAT);
  82. NewRule oneIssuePerLine = repo.createRule(OneIssuePerLineSensor.RULE_KEY).setName("One Issue Per Line")
  83. .setHtmlDescription("Generate an issue on each line of a file. It requires the metric \"lines\".")
  84. .setTags("line");
  85. oneIssuePerLine
  86. .setDebtRemediationFunction(oneIssuePerLine.debtRemediationFunctions().linear("1min"))
  87. .setGapDescription("It takes about 1 minute to an experienced software craftsman to remove a line of code");
  88. repo.createRule(OneIssueOnDirPerFileSensor.RULE_KEY).setName("One Issue On Dir Per File")
  89. .setHtmlDescription("Generate issues on directories");
  90. NewRule oneIssuePerFile = repo.createRule(OneIssuePerFileSensor.RULE_KEY).setName("One Issue Per File")
  91. .setHtmlDescription("Generate an issue on each file");
  92. oneIssuePerFile.setDebtRemediationFunction(oneIssuePerFile.debtRemediationFunctions().linear(TEN_MIN));
  93. NewRule oneIssuePerTestFile = repo.createRule(OneIssuePerTestFileSensor.RULE_KEY).setName("One Issue Per Test File")
  94. .setScope(RuleScope.TEST)
  95. .setHtmlDescription("Generate an issue on each test file");
  96. oneIssuePerTestFile.setDebtRemediationFunction(oneIssuePerTestFile.debtRemediationFunctions().linear("8min"));
  97. NewRule oneBugIssuePerTestLine = repo.createRule(OneBugIssuePerTestLineSensor.RULE_KEY).setName("One Bug Issue Per Test Line")
  98. .setScope(RuleScope.TEST)
  99. .setHtmlDescription("Generate a bug issue on each line of a test file. It requires the metric \"lines\".")
  100. .setType(RuleType.BUG);
  101. oneBugIssuePerTestLine
  102. .setDebtRemediationFunction(oneBugIssuePerTestLine.debtRemediationFunctions().linear("4min"));
  103. NewRule oneCodeSmellIssuePerTestLine = repo.createRule(OneCodeSmellIssuePerTestLineSensor.RULE_KEY).setName("One Code Smell Issue Per Test Line")
  104. .setScope(RuleScope.TEST)
  105. .setHtmlDescription("Generate a code smell issue on each line of a test file. It requires the metric \"lines\".")
  106. .setType(RuleType.CODE_SMELL);
  107. oneCodeSmellIssuePerTestLine
  108. .setDebtRemediationFunction(oneCodeSmellIssuePerTestLine.debtRemediationFunctions().linear("3min"));
  109. NewRule oneIssuePerDirectory = repo.createRule(OneIssuePerDirectorySensor.RULE_KEY).setName("One Issue Per Directory")
  110. .setHtmlDescription("Generate an issue on each non-empty directory");
  111. oneIssuePerDirectory.setDebtRemediationFunction(oneIssuePerDirectory.debtRemediationFunctions().linear(TEN_MIN));
  112. NewRule oneDayDebtPerFile = repo.createRule(OneDayDebtPerFileSensor.RULE_KEY).setName("One Day Debt Per File")
  113. .setHtmlDescription("Generate an issue on each file with a debt of one day");
  114. oneDayDebtPerFile.setDebtRemediationFunction(oneDayDebtPerFile.debtRemediationFunctions().linear("1d"));
  115. NewRule oneIssuePerModule = repo.createRule(OneIssuePerModuleSensor.RULE_KEY).setName("One Issue Per Module")
  116. .setHtmlDescription("Generate an issue on each module");
  117. oneIssuePerModule
  118. .setDebtRemediationFunction(oneIssuePerModule.debtRemediationFunctions().linearWithOffset("25min", "1h"))
  119. .setGapDescription("A certified architect will need roughly half an hour to start working on removal of modules, " +
  120. "then it's about one hour per module.");
  121. repo.createRule(OneBlockerIssuePerFileSensor.RULE_KEY).setName("One Blocker Issue Per File")
  122. .setHtmlDescription("Generate a blocker issue on each file, whatever the severity declared in the Quality profile");
  123. repo.createRule(CustomMessageSensor.RULE_KEY).setName("Issue With Custom Message")
  124. .setHtmlDescription("Generate an issue on each file with a custom message");
  125. repo.createRule(RandomAccessSensor.RULE_KEY).setName("One Issue Per File with Random Access")
  126. .setHtmlDescription("This issue is generated on each file");
  127. repo.createRule(MultilineIssuesSensor.RULE_KEY).setName("Creates issues with ranges/multiple locations")
  128. .setHtmlDescription("Issue with range and multiple locations");
  129. repo.createRule(OneIssuePerUnknownFileSensor.RULE_KEY).setName("Creates issues on each file with extension 'unknown'")
  130. .setHtmlDescription("This issue is generated on each file with extenstion 'unknown'");
  131. NewRule oneBugIssuePerLine = repo.createRule(OneBugIssuePerLineSensor.RULE_KEY).setName("One Bug Issue Per Line")
  132. .setHtmlDescription("Generate a bug issue on each line of a file. It requires the metric \"lines\".")
  133. .setType(RuleType.BUG);
  134. oneBugIssuePerLine
  135. .setDebtRemediationFunction(oneBugIssuePerLine.debtRemediationFunctions().linear("5min"));
  136. NewRule oneCodeSmellIssuePerLine = repo.createRule(OneCodeSmellIssuePerLineSensor.RULE_KEY).setName("One Code Smell Issue Per Line")
  137. .setHtmlDescription("Generate a code smell issue on each line of a file. It requires the metric \"lines\".")
  138. .setType(RuleType.CODE_SMELL);
  139. oneCodeSmellIssuePerLine
  140. .setDebtRemediationFunction(oneBugIssuePerLine.debtRemediationFunctions().linear("9min"));
  141. NewRule oneVulnerabilityIssuePerModule = repo.createRule(OneVulnerabilityIssuePerModuleSensor.RULE_KEY).setName("One Vulnerability Issue Per Module")
  142. .setHtmlDescription("Generate an issue on each module")
  143. .setType(RuleType.VULNERABILITY);
  144. oneVulnerabilityIssuePerModule
  145. .setDebtRemediationFunction(oneVulnerabilityIssuePerModule.debtRemediationFunctions().linearWithOffset("25min", "1h"))
  146. .setGapDescription("A certified architect will need roughly half an hour to start working on removal of modules, " +
  147. "then it's about one hour per module.");
  148. repo
  149. .createRule("xoo-template")
  150. .setTemplate(true)
  151. .setName("Template of rule")
  152. .setHtmlDescription("Template to be overridden by custom rules");
  153. NewRule hotspot = repo.createRule(HotspotSensor.RULE_KEY)
  154. .setName("Find security hotspots")
  155. .setType(RuleType.SECURITY_HOTSPOT)
  156. .setActivatedByDefault(false)
  157. .setHtmlDescription("Search for Security Hotspots in Xoo files");
  158. hotspot
  159. .setDebtRemediationFunction(hotspot.debtRemediationFunctions().constantPerIssue("2min"));
  160. if (version != null && version.isGreaterThanOrEqual(Version.create(7, 3))) {
  161. hotspot
  162. .addOwaspTop10(OwaspTop10.A1, OwaspTop10.A3)
  163. .addCwe(1, 89, 123, 863);
  164. oneVulnerabilityIssuePerModule
  165. .addOwaspTop10(OwaspTop10.A9, OwaspTop10.A10)
  166. .addCwe(250, 564, 546, 943);
  167. }
  168. repo.done();
  169. }
  170. private static void defineRulesXooExternal(Context context) {
  171. NewRepository repo = context.createExternalRepository(OneExternalIssuePerLineSensor.ENGINE_ID, Xoo.KEY).setName(OneExternalIssuePerLineSensor.ENGINE_ID);
  172. repo.createRule(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID)
  173. .setSeverity(OnePredefinedRuleExternalIssuePerLineSensor.SEVERITY)
  174. .setType(OnePredefinedRuleExternalIssuePerLineSensor.TYPE)
  175. .setScope(RuleScope.ALL)
  176. .setHtmlDescription("Generates one external issue in each line")
  177. .setName("One external issue per line");
  178. repo.done();
  179. }
  180. }