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

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