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.

OnePredefinedAndAdHocRuleExternalIssuePerLineSensor.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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.fs.InputFile.Type;
  25. import org.sonar.api.batch.rule.Severity;
  26. import org.sonar.api.batch.sensor.Sensor;
  27. import org.sonar.api.batch.sensor.SensorContext;
  28. import org.sonar.api.batch.sensor.SensorDescriptor;
  29. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  30. import org.sonar.api.rules.RuleType;
  31. import org.sonar.xoo.Xoo;
  32. public class OnePredefinedAndAdHocRuleExternalIssuePerLineSensor implements Sensor {
  33. public static final String ACTIVATE = "sonar.onePredefinedAndAdHocRuleExternalIssuePerLine.activate";
  34. private static final String NAME = "One External Issue Per Line With A Predefined And An AdHoc Rule";
  35. @Override
  36. public void describe(SensorDescriptor descriptor) {
  37. descriptor
  38. .name(NAME)
  39. .onlyOnLanguages(Xoo.KEY)
  40. .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE).orElse(false));
  41. }
  42. @Override
  43. public void execute(SensorContext context) {
  44. FileSystem fs = context.fileSystem();
  45. FilePredicates p = fs.predicates();
  46. for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(Type.MAIN)))) {
  47. createIssues(file, context);
  48. }
  49. }
  50. private static void createIssues(InputFile file, SensorContext context) {
  51. for (int line = 1; line <= file.lines(); line++) {
  52. NewExternalIssue newIssue = context.newExternalIssue();
  53. newIssue
  54. .engineId(OnePredefinedRuleExternalIssuePerLineSensor.ENGINE_ID)
  55. .ruleId(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID)
  56. .at(newIssue.newLocation()
  57. .on(file)
  58. .at(file.selectLine(line))
  59. .message("This issue is generated on each line and the rule is predefined"))
  60. .severity(Severity.valueOf(OnePredefinedRuleExternalIssuePerLineSensor.SEVERITY))
  61. .remediationEffortMinutes(OnePredefinedRuleExternalIssuePerLineSensor.EFFORT)
  62. .type(OnePredefinedRuleExternalIssuePerLineSensor.TYPE)
  63. .save();
  64. // Even if the issue is on a predefined rule, the sensor is declaring an adHoc rule => this info should be ignored
  65. context.newAdHocRule()
  66. .engineId(OnePredefinedRuleExternalIssuePerLineSensor.ENGINE_ID)
  67. .ruleId(OnePredefinedRuleExternalIssuePerLineSensor.RULE_ID)
  68. .name("An ad hoc rule")
  69. .description("blah blah")
  70. .severity(Severity.BLOCKER)
  71. .type(RuleType.BUG)
  72. .save();
  73. }
  74. }
  75. }