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.

OneExternalIssuePerFoobarSensor.java 3.3KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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 java.io.IOException;
  22. import java.util.List;
  23. import org.apache.commons.io.IOUtils;
  24. import org.sonar.api.batch.fs.FilePredicates;
  25. import org.sonar.api.batch.fs.FileSystem;
  26. import org.sonar.api.batch.fs.InputFile;
  27. import org.sonar.api.batch.rule.Severity;
  28. import org.sonar.api.batch.sensor.Sensor;
  29. import org.sonar.api.batch.sensor.SensorContext;
  30. import org.sonar.api.batch.sensor.SensorDescriptor;
  31. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  32. import org.sonar.api.rules.RuleType;
  33. import org.sonar.xoo.Xoo;
  34. public class OneExternalIssuePerFoobarSensor implements Sensor {
  35. public static final String RULE_ID = "OneExternalIssuePerFoobar";
  36. public static final String TAG = "foobar";
  37. public static final String ENGINE_ID = "XooEngine";
  38. public static final String SEVERITY = "MAJOR";
  39. public static final Long EFFORT = 10L;
  40. public static final RuleType TYPE = RuleType.BUG;
  41. public static final String ACTIVATE = "sonar.oneExternalIssuePerFoobar.activate";
  42. private static final String NAME = "One External Issue Per Foobar Rule";
  43. @Override
  44. public void describe(SensorDescriptor descriptor) {
  45. descriptor
  46. .name(NAME)
  47. .onlyOnLanguages(Xoo.KEY)
  48. .onlyWhenConfiguration(c -> c.getBoolean(ACTIVATE).orElse(false));
  49. }
  50. @Override
  51. public void execute(SensorContext context) {
  52. FileSystem fs = context.fileSystem();
  53. FilePredicates p = fs.predicates();
  54. for (InputFile file : fs.inputFiles(p.and(p.hasLanguages(Xoo.KEY), p.hasType(InputFile.Type.MAIN)))) {
  55. createIssues(file, context);
  56. }
  57. }
  58. private static void createIssues(InputFile file, SensorContext context) {
  59. try {
  60. List<String> lines = IOUtils.readLines(file.inputStream(), file.charset());
  61. for (int i = 0; i < lines.size(); i++) {
  62. var line = lines.get(i);
  63. if (line.contains(TAG) && !line.contains("//NOSONAR")) {
  64. NewExternalIssue newIssue = context.newExternalIssue();
  65. newIssue
  66. .engineId(ENGINE_ID)
  67. .ruleId(RULE_ID)
  68. .at(newIssue.newLocation()
  69. .on(file)
  70. .at(file.selectLine(i + 1))
  71. .message("This issue is generated on line containing foobar string"))
  72. .severity(Severity.valueOf(SEVERITY))
  73. .remediationEffortMinutes(EFFORT)
  74. .type(TYPE)
  75. .save();
  76. }
  77. }
  78. } catch (IOException e) {
  79. throw new IllegalStateException("Fail to process " + file, e);
  80. }
  81. }
  82. }