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.

ExternalIssueImporter.java 5.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 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.scanner.externalissue;
  21. import com.google.common.base.MoreObjects;
  22. import java.util.LinkedHashSet;
  23. import java.util.Set;
  24. import java.util.stream.Collectors;
  25. import javax.annotation.CheckForNull;
  26. import org.sonar.api.batch.fs.InputFile;
  27. import org.sonar.api.batch.fs.TextPointer;
  28. import org.sonar.api.batch.rule.Severity;
  29. import org.sonar.api.batch.sensor.SensorContext;
  30. import org.sonar.api.batch.sensor.issue.NewExternalIssue;
  31. import org.sonar.api.batch.sensor.issue.NewIssueLocation;
  32. import org.sonar.api.rules.RuleType;
  33. import org.sonar.api.utils.log.Logger;
  34. import org.sonar.api.utils.log.Loggers;
  35. import org.sonar.scanner.externalissue.ReportParser.Issue;
  36. import org.sonar.scanner.externalissue.ReportParser.Location;
  37. import org.sonar.scanner.externalissue.ReportParser.Report;
  38. public class ExternalIssueImporter {
  39. private static final Logger LOG = Loggers.get(ExternalIssuesImportSensor.class);
  40. private static final int MAX_UNKNOWN_FILE_PATHS_TO_PRINT = 5;
  41. private final SensorContext context;
  42. private final Report report;
  43. private final Set<String> unknownFiles = new LinkedHashSet<>();
  44. private final Set<String> knownFiles = new LinkedHashSet<>();
  45. public ExternalIssueImporter(SensorContext context, Report report) {
  46. this.context = context;
  47. this.report = report;
  48. }
  49. public void execute() {
  50. int issueCount = 0;
  51. for (Issue issue : report.issues) {
  52. if (importIssue(issue)) {
  53. issueCount++;
  54. }
  55. }
  56. LOG.info("Imported {} {} in {} {}", issueCount, pluralize("issue", issueCount), knownFiles.size(), pluralize("file", knownFiles.size()));
  57. int numberOfUnknownFiles = unknownFiles.size();
  58. if (numberOfUnknownFiles > 0) {
  59. LOG.info("External issues ignored for " + numberOfUnknownFiles + " unknown files, including: "
  60. + unknownFiles.stream().limit(MAX_UNKNOWN_FILE_PATHS_TO_PRINT).collect(Collectors.joining(", ")));
  61. }
  62. }
  63. private boolean importIssue(Issue issue) {
  64. NewExternalIssue externalIssue = context.newExternalIssue()
  65. .engineId(issue.engineId)
  66. .ruleId(issue.ruleId)
  67. .severity(Severity.valueOf(issue.severity))
  68. .type(RuleType.valueOf(issue.type));
  69. if (issue.effortMinutes != null) {
  70. externalIssue.remediationEffortMinutes(Long.valueOf(issue.effortMinutes));
  71. }
  72. NewIssueLocation primary = fillLocation(context, externalIssue.newLocation(), issue.primaryLocation);
  73. if (primary != null) {
  74. knownFiles.add(issue.primaryLocation.filePath);
  75. externalIssue.at(primary);
  76. if (issue.secondaryLocations != null) {
  77. for (Location l : issue.secondaryLocations) {
  78. NewIssueLocation secondary = fillLocation(context, externalIssue.newLocation(), l);
  79. if (secondary != null) {
  80. externalIssue.addLocation(secondary);
  81. }
  82. }
  83. }
  84. externalIssue.save();
  85. return true;
  86. } else {
  87. unknownFiles.add(issue.primaryLocation.filePath);
  88. return false;
  89. }
  90. }
  91. private static String pluralize(String msg, int count) {
  92. if (count == 1) {
  93. return msg;
  94. }
  95. return msg + "s";
  96. }
  97. @CheckForNull
  98. private static NewIssueLocation fillLocation(SensorContext context, NewIssueLocation newLocation, Location location) {
  99. InputFile file = findFile(context, location.filePath);
  100. if (file == null) {
  101. return null;
  102. }
  103. newLocation.on(file);
  104. if (location.message != null) {
  105. newLocation.message(location.message);
  106. }
  107. if (location.textRange != null) {
  108. if (location.textRange.startColumn != null) {
  109. TextPointer start = file.newPointer(location.textRange.startLine, location.textRange.startColumn);
  110. int endLine = MoreObjects.firstNonNull(location.textRange.endLine, location.textRange.startLine);
  111. int endColumn;
  112. if (location.textRange.endColumn == null) {
  113. // assume it's until the last character of the end line
  114. endColumn = file.selectLine(endLine).end().lineOffset();
  115. } else {
  116. endColumn = location.textRange.endColumn;
  117. }
  118. TextPointer end = file.newPointer(endLine, endColumn);
  119. newLocation.at(file.newRange(start, end));
  120. } else {
  121. newLocation.at(file.selectLine(location.textRange.startLine));
  122. }
  123. }
  124. return newLocation;
  125. }
  126. @CheckForNull
  127. private static InputFile findFile(SensorContext context, String filePath) {
  128. return context.fileSystem().inputFile(context.fileSystem().predicates().hasPath(filePath));
  129. }
  130. }