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 12KB

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