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.

OneExternalIssuePerLineSensor.java 3.2KB

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