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 org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
24 import java.util.Collection;
25 import java.util.List;
28 import org.sonar.core.issue.DefaultIssue;
29 import org.sonar.core.issue.tracking.Tracking;
30 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
31 import org.sonar.server.computation.task.projectanalysis.component.Component;
32 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
33 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
34 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
35 import org.sonar.server.util.cache.DiskCache;
37 public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter {
39 private final IssueCache issueCache;
40 private final IssueLifecycle issueLifecycle;
41 private final IssueVisitors issueVisitors;
42 private final ComponentIssuesLoader issuesLoader;
43 private final AnalysisMetadataHolder analysisMetadataHolder;
44 private final IssueTrackingDelegator issueTracking;
46 public IntegrateIssuesVisitor(IssueCache issueCache, IssueLifecycle issueLifecycle, IssueVisitors issueVisitors, ComponentIssuesLoader issuesLoader,
47 AnalysisMetadataHolder analysisMetadataHolder, IssueTrackingDelegator issueTracking) {
48 super(CrawlerDepthLimit.FILE, POST_ORDER);
49 this.issueCache = issueCache;
50 this.issueLifecycle = issueLifecycle;
51 this.issueVisitors = issueVisitors;
52 this.issuesLoader = issuesLoader;
53 this.analysisMetadataHolder = analysisMetadataHolder;
54 this.issueTracking = issueTracking;
58 public void visitAny(Component component) {
59 try (DiskCache<DefaultIssue>.DiskAppender cacheAppender = issueCache.newAppender()) {
60 issueVisitors.beforeComponent(component);
62 if (isIncremental(component)) {
63 List<DefaultIssue> issues = issuesLoader.loadForComponentUuid(component.getUuid());
64 fillIncrementalOpenIssues(component, issues, cacheAppender);
66 Tracking<DefaultIssue, DefaultIssue> tracking = issueTracking.track(component);
67 fillNewOpenIssues(component, tracking.getUnmatchedRaws(), cacheAppender);
68 fillExistingOpenIssues(component, tracking.getMatchedRaws(), cacheAppender);
69 closeUnmatchedBaseIssues(component, tracking.getUnmatchedBases(), cacheAppender);
71 issueVisitors.afterComponent(component);
72 } catch (Exception e) {
73 throw new IllegalStateException(String.format("Fail to process issues of component '%s'", component.getKey()), e);
77 private boolean isIncremental(Component component) {
78 return analysisMetadataHolder.isIncrementalAnalysis() && component.getStatus() == Status.SAME;
81 private void fillNewOpenIssues(Component component, Iterable<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
82 for (DefaultIssue issue : issues) {
83 issueLifecycle.initNewOpenIssue(issue);
84 process(component, issue, cacheAppender);
88 private void fillIncrementalOpenIssues(Component component, Collection<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
89 for (DefaultIssue issue : issues) {
90 process(component, issue, cacheAppender);
94 private void fillExistingOpenIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
95 for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
96 DefaultIssue raw = entry.getKey();
97 DefaultIssue base = entry.getValue();
98 issueLifecycle.mergeExistingOpenIssue(raw, base);
99 process(component, raw, cacheAppender);
103 private void closeUnmatchedBaseIssues(Component component, Iterable<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
104 for (DefaultIssue issue : issues) {
105 // TODO should replace flag "beingClosed" by express call to transition "automaticClose"
106 issue.setBeingClosed(true);
107 // TODO manual issues -> was updater.setResolution(newIssue, Issue.RESOLUTION_REMOVED, changeContext);. Is it a problem ?
108 process(component, issue, cacheAppender);
112 private void process(Component component, DefaultIssue issue, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
113 issueLifecycle.doAutomaticTransition(issue);
114 issueVisitors.onIssue(component, issue);
115 cacheAppender.append(issue);