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.

IssueTrackingDelegator.java 3.2KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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.server.computation.task.projectanalysis.issue;
  21. import org.sonar.core.issue.DefaultIssue;
  22. import org.sonar.core.issue.tracking.Tracking;
  23. import org.sonar.db.component.BranchType;
  24. import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
  25. import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
  26. import org.sonar.server.computation.task.projectanalysis.component.Component;
  27. import static java.util.Collections.emptyList;
  28. import static java.util.Collections.emptyMap;
  29. public class IssueTrackingDelegator {
  30. private final ShortBranchTrackerExecution shortBranchTracker;
  31. private final TrackerExecution tracker;
  32. private final AnalysisMetadataHolder analysisMetadataHolder;
  33. private final MergeBranchTrackerExecution mergeBranchTracker;
  34. public IssueTrackingDelegator(ShortBranchTrackerExecution shortBranchTracker, MergeBranchTrackerExecution longBranchTracker,
  35. TrackerExecution tracker, AnalysisMetadataHolder analysisMetadataHolder) {
  36. this.shortBranchTracker = shortBranchTracker;
  37. this.mergeBranchTracker = longBranchTracker;
  38. this.tracker = tracker;
  39. this.analysisMetadataHolder = analysisMetadataHolder;
  40. }
  41. public TrackingResult track(Component component) {
  42. if (analysisMetadataHolder.isShortLivingBranch() || analysisMetadataHolder.isPullRequest()) {
  43. return standardResult(shortBranchTracker.track(component));
  44. } else if (isFirstAnalysisSecondaryLongLivingBranch()) {
  45. Tracking<DefaultIssue, DefaultIssue> tracking = mergeBranchTracker.track(component);
  46. return new TrackingResult(tracking.getMatchedRaws(), emptyMap(), emptyList(), tracking.getUnmatchedRaws());
  47. } else {
  48. return standardResult(tracker.track(component));
  49. }
  50. }
  51. private static TrackingResult standardResult(Tracking<DefaultIssue, DefaultIssue> tracking) {
  52. return new TrackingResult(emptyMap(), tracking.getMatchedRaws(), tracking.getUnmatchedBases(), tracking.getUnmatchedRaws());
  53. }
  54. /**
  55. * Special case where we want to do the issue tracking with the merge branch, and copy matched issue to the current branch.
  56. */
  57. private boolean isFirstAnalysisSecondaryLongLivingBranch() {
  58. if (analysisMetadataHolder.isFirstAnalysis()) {
  59. Branch branch = analysisMetadataHolder.getBranch();
  60. return !branch.isMain() && branch.getType() == BranchType.LONG;
  61. }
  62. return false;
  63. }
  64. }