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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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. oneIssuePerLine
  85. .setDebtRemediationFunction(oneIssuePerLine.debtRemediationFunctions().linear("1min"))
  86. .setGapDescription("It takes about 1 minute to an experienced software craftsman to remove a line of code");
  87. repo.createRule(OneIssueOnDirPerFileSensor.RULE_KEY).setName("One Issue On Dir Per File")
  88. .setHtmlDescription("Generate issues on directories");
  89. NewRule oneIssuePerFile = repo.createRule(OneIssuePerFileSensor.RULE_KEY).setName("One Issue Per File")
  90. .setHtmlDescription("Generate an issue on each file");
  91. oneIssuePerFile.setDebtRemediationFunction(oneIssuePerFile.debtRemediationFunctions().linear(TEN_MIN));
  92. NewRule oneIssuePerTestFile = repo.createRule(OneIssuePerTestFileSensor.RULE_KEY).setName("One Issue Per Test File")
  93. .setScope(RuleScope.TEST)
  94. .setHtmlDescription("Generate an issue on each test file");
  95. oneIssuePerTestFile.setDebtRemediationFunction(oneIssuePerTestFile.debtRemediationFunctions().linear(TEN_MIN));
  96. NewRule oneIssuePerDirectory = repo.createRule(OneIssuePerDirectorySensor.RULE_KEY).setName("One Issue Per Directory")
  97. .setHtmlDescription("Generate an issue on each non-empty directory");
  98. oneIssuePerDirectory.setDebtRemediationFunction(oneIssuePerDirectory.debtRemediationFunctions().linear(TEN_MIN));
  99. NewRule oneDayDebtPerFile = repo.createRule(OneDayDebtPerFileSensor.RULE_KEY).setName("One Day Debt Per File")
  100. .setHtmlDescription("Generate an issue on each file with a debt of one day");
  101. oneDayDebtPerFile.setDebtRemediationFunction(oneDayDebtPerFile.debtRemediationFunctions().linear("1d"));
  102. NewRule oneIssuePerModule = repo.createRule(OneIssuePerModuleSensor.RULE_KEY).setName("One Issue Per Module")
  103. .setHtmlDescription("Generate an issue on each module");
  104. oneIssuePerModule
  105. .setDebtRemediationFunction(oneIssuePerModule.debtRemediationFunctions().linearWithOffset("25min", "1h"))
  106. .setGapDescription("A certified architect will need roughly half an hour to start working on removal of modules, " +
  107. "then it's about one hour per module.");
  108. repo.createRule(OneBlockerIssuePerFileSensor.RULE_KEY).setName("One Blocker Issue Per File")
  109. .setHtmlDescription("Generate a blocker issue on each file, whatever the severity declared in the Quality profile");
  110. repo.createRule(CustomMessageSensor.RULE_KEY).setName("Issue With Custom Message")
  111. .setHtmlDescription("Generate an issue on each file with a custom message");
  112. repo.createRule(RandomAccessSensor.RULE_KEY).setName("One Issue Per File with Random Access")
  113. .setHtmlDescription("This issue is generated on each file");
  114. repo.createRule(MultilineIssuesSensor.RULE_KEY).setName("Creates issues with ranges/multiple locations")
  115. .setHtmlDescription("Issue with range and multiple locations");
  116. repo.createRule(OneIssuePerUnknownFileSensor.RULE_KEY).setName("Creates issues on each file with extension 'unknown'")
  117. .setHtmlDescription("This issue is generated on each file with extenstion 'unknown'");
  118. NewRule oneBugIssuePerLine = repo.createRule(OneBugIssuePerLineSensor.RULE_KEY).setName("One Bug Issue Per Line")
  119. .setHtmlDescription("Generate a bug issue on each line of a file. It requires the metric \"lines\".")
  120. .setType(RuleType.BUG);
  121. oneBugIssuePerLine
  122. .setDebtRemediationFunction(oneBugIssuePerLine.debtRemediationFunctions().linear("5min"));
  123. NewRule oneVulnerabilityIssuePerModule = repo.createRule(OneVulnerabilityIssuePerModuleSensor.RULE_KEY).setName("One Vulnerability Issue Per Module")
  124. .setHtmlDescription("Generate an issue on each module")
  125. .setType(RuleType.VULNERABILITY);
  126. oneVulnerabilityIssuePerModule
  127. .setDebtRemediationFunction(oneVulnerabilityIssuePerModule.debtRemediationFunctions().linearWithOffset("25min", "1h"))
  128. .setGapDescription("A certified architect will need roughly half an hour to start working on removal of modules, " +
  129. "then it's about one hour per module.");
  130. repo
  131. .createRule("xoo-template")
  132. .setTemplate(true)
  133. .setName("Template of rule")
  134. .setHtmlDescription("Template to be overridden by custom rules");
  135. NewRule hotspot = repo.createRule(HotspotSensor.RULE_KEY)
  136. .setName("Find security hotspots")
  137. .setType(RuleType.SECURITY_HOTSPOT)
  138. .setActivatedByDefault(false)
  139. .setHtmlDescription("Search for Security Hotspots in Xoo files");
  140. hotspot
  141. .setDebtRemediationFunction(hotspot.debtRemediationFunctions().constantPerIssue("2min"));
  142. if (version != null && version.isGreaterThanOrEqual(Version.create(7, 3))) {
  143. hotspot
  144. .addOwaspTop10(OwaspTop10.A1, OwaspTop10.A3)
  145. .addCwe(1, 123, 863);
  146. }
  147. repo.done();
  148. }
  149. private static void defineRulesXooExternal(Context context) {
  150. NewRepository repo = context.createExternalRepository(OneExternalIssuePerLineSensor.ENGINE_ID, Xoo.KEY).setName(OneExternalIssuePerLineSensor.ENGINE_ID);
  151. repo.createRule(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID)
  152. .setSeverity(OnePredefinedRuleExternalIssuePerLineSensor.SEVERITY)
  153. .setType(OnePredefinedRuleExternalIssuePerLineSensor.TYPE)
  154. .setScope(RuleScope.ALL)
  155. .setHtmlDescription("Generates one external issue in each line")
  156. .setName("One external issue per line");
  157. repo.done();
  158. }
  159. }