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.

NewIssueClassifier.java 3.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2021 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.ce.task.projectanalysis.issue;
  21. import java.util.Optional;
  22. import java.util.Set;
  23. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  24. import org.sonar.ce.task.projectanalysis.component.Component;
  25. import org.sonar.ce.task.projectanalysis.period.PeriodHolder;
  26. import org.sonar.ce.task.projectanalysis.source.NewLinesRepository;
  27. import org.sonar.core.issue.DefaultIssue;
  28. import org.sonar.db.newcodeperiod.NewCodePeriodType;
  29. public class NewIssueClassifier {
  30. private final NewLinesRepository newLinesRepository;
  31. private final PeriodHolder periodHolder;
  32. private final AnalysisMetadataHolder analysisMetadataHolder;
  33. public NewIssueClassifier(NewLinesRepository newLinesRepository, PeriodHolder periodHolder, AnalysisMetadataHolder analysisMetadataHolder) {
  34. this.newLinesRepository = newLinesRepository;
  35. this.periodHolder = periodHolder;
  36. this.analysisMetadataHolder = analysisMetadataHolder;
  37. }
  38. public boolean isEnabled() {
  39. return analysisMetadataHolder.isPullRequest() || periodHolder.hasPeriodDate() ||
  40. (periodHolder.hasPeriod() && periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name()));
  41. }
  42. public boolean isNew(Component component, DefaultIssue issue) {
  43. if (analysisMetadataHolder.isPullRequest()) {
  44. return true;
  45. }
  46. if (periodHolder.hasPeriod()) {
  47. if (periodHolder.hasPeriodDate()) {
  48. return periodHolder.getPeriod().isOnPeriod(issue.creationDate());
  49. }
  50. if (periodHolder.getPeriod().getMode().equals(NewCodePeriodType.REFERENCE_BRANCH.name())) {
  51. return hasAtLeastOneLocationOnChangedLines(component, issue);
  52. }
  53. }
  54. return false;
  55. }
  56. private boolean hasAtLeastOneLocationOnChangedLines(Component component, DefaultIssue issue) {
  57. if (component.getType() != Component.Type.FILE) {
  58. return false;
  59. }
  60. final Optional<Set<Integer>> newLinesOpt = newLinesRepository.getNewLines(component);
  61. if (newLinesOpt.isEmpty()) {
  62. return false;
  63. }
  64. Set<Integer> newLines = newLinesOpt.get();
  65. return IssueLocations.allLinesFor(issue, component.getUuid()).anyMatch(newLines::contains);
  66. }
  67. }