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.

OneExternalIssueCctPerLineSensor.java 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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 org.sonar.api.batch.fs.FilePredicates;
  22. import org.sonar.api.batch.fs.FileSystem;
  23. import org.sonar.api.batch.fs.InputFile;
  24. import org.sonar.api.batch.sensor.Sensor;
  25. import org.sonar.api.batch.sensor.SensorContext;
  26. import org.sonar.api.batch.sensor.SensorDescriptor;
  27. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  28. import org.sonar.xoo.Xoo;
  29. import static org.sonar.api.issue.impact.Severity.LOW;
  30. import static org.sonar.api.issue.impact.Severity.MEDIUM;
  31. import static org.sonar.api.issue.impact.SoftwareQuality.MAINTAINABILITY;
  32. import static org.sonar.api.issue.impact.SoftwareQuality.RELIABILITY;
  33. import static org.sonar.api.rules.CleanCodeAttribute.CLEAR;
  34. public class OneExternalIssueCctPerLineSensor implements Sensor {
  35. public static final String RULE_ID = "OneExternalIssueWithPerLineSensor";
  36. public static final String ENGINE_ID = "XooEngine";
  37. public static final Long EFFORT = 10L;
  38. public static final String ACTIVATE = "sonar.oneExternalIssueWithPerLineSensor.activate";
  39. public static final String REGISTER_AD_HOC_RULE = "sonar.oneExternalIssueWithPerLineSensor.adhocRule";
  40. private static final String NAME = "One External Issue Per Line in Clean Code Taxonomy format";
  41. @Override
  42. public void describe(SensorDescriptor descriptor) {
  43. descriptor
  44. .name(NAME)
  45. .onlyOnLanguages(Xoo.KEY)
  46. .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE).orElse(false));
  47. }
  48. @Override
  49. public void execute(SensorContext context) {
  50. FileSystem fs = context.fileSystem();
  51. FilePredicates p = fs.predicates();
  52. for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.MAIN)))) {
  53. createIssues(file, context);
  54. }
  55. if (context.config().getBoolean(REGISTER_AD_HOC_RULE).orElse(false)) {
  56. context.newAdHocRule()
  57. .engineId(ENGINE_ID)
  58. .ruleId(RULE_ID)
  59. .name("An ad hoc rule")
  60. .description("blah blah")
  61. .cleanCodeAttribute(CLEAR)
  62. .addDefaultImpact(MAINTAINABILITY, MEDIUM)
  63. .addDefaultImpact(RELIABILITY, LOW)
  64. .save();
  65. }
  66. }
  67. private static void createIssues(InputFile file, SensorContext context) {
  68. for (int line = 1; line <= file.lines(); line++) {
  69. NewExternalIssue newIssue = context.newExternalIssue();
  70. newIssue
  71. .engineId(ENGINE_ID)
  72. .ruleId(RULE_ID)
  73. .at(newIssue.newLocation()
  74. .on(file)
  75. .at(file.selectLine(line))
  76. .message("This issue is generated on each line"))
  77. .remediationEffortMinutes(EFFORT)
  78. .save();
  79. }
  80. }
  81. }