3 * Copyright (C) 2009-2017 SonarSource SA
4 * mailto:info AT sonarsource DOT com
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.
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.
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.
20 package org.sonar.server.computation.task.projectanalysis.issue;
22 import static java.util.Collections.emptyList;
23 import static java.util.Collections.emptyMap;
25 import java.util.Optional;
27 import org.sonar.core.issue.DefaultIssue;
28 import org.sonar.core.issue.tracking.Tracking;
29 import org.sonar.db.component.BranchType;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.computation.task.projectanalysis.analysis.Branch;
32 import org.sonar.server.computation.task.projectanalysis.component.Component;
34 public class IssueTrackingDelegator {
35 private final ShortBranchTrackerExecution shortBranchTracker;
36 private final TrackerExecution tracker;
37 private final AnalysisMetadataHolder analysisMetadataHolder;
38 private final MergeBranchTrackerExecution mergeBranchTracker;
40 public IssueTrackingDelegator(ShortBranchTrackerExecution shortBranchTracker, MergeBranchTrackerExecution longBranchTracker,
41 TrackerExecution tracker, AnalysisMetadataHolder analysisMetadataHolder) {
42 this.shortBranchTracker = shortBranchTracker;
43 this.mergeBranchTracker = longBranchTracker;
44 this.tracker = tracker;
45 this.analysisMetadataHolder = analysisMetadataHolder;
48 public TrackingResult track(Component component) {
49 if (analysisMetadataHolder.isShortLivingBranch()) {
50 return standardResult(shortBranchTracker.track(component));
51 } else if (isFirstAnalysisSecondaryLongLivingBranch()) {
52 Tracking<DefaultIssue, DefaultIssue> tracking = mergeBranchTracker.track(component);
53 return new TrackingResult(tracking.getMatchedRaws(), emptyMap(), emptyList(), tracking.getUnmatchedRaws());
55 return standardResult(tracker.track(component));
59 private static TrackingResult standardResult(Tracking<DefaultIssue, DefaultIssue> tracking) {
60 return new TrackingResult(emptyMap(), tracking.getMatchedRaws(), tracking.getUnmatchedBases(), tracking.getUnmatchedRaws());
64 * Special case where we want to do the issue tracking with the merge branch, and copy matched issue to the current branch.
66 private boolean isFirstAnalysisSecondaryLongLivingBranch() {
67 if (analysisMetadataHolder.isFirstAnalysis()) {
68 Optional<Branch> branch = analysisMetadataHolder.getBranch();
69 if (branch.isPresent()) {
70 return !branch.get().isMain() && branch.get().getType() == BranchType.LONG;