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.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
30 import org.sonar.server.computation.task.projectanalysis.component.Component;
31 import org.sonar.server.computation.task.projectanalysis.component.Component.Status;
32 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
33 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
34 import org.sonar.server.util.cache.DiskCache;
36 public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter {
38 private final IssueCache issueCache;
39 private final IssueLifecycle issueLifecycle;
40 private final IssueVisitors issueVisitors;
41 private final ComponentIssuesLoader issuesLoader;
42 private final AnalysisMetadataHolder analysisMetadataHolder;
43 private final IssueTrackingDelegator issueTracking;
45 public IntegrateIssuesVisitor(IssueCache issueCache, IssueLifecycle issueLifecycle, IssueVisitors issueVisitors, ComponentIssuesLoader issuesLoader,
46 AnalysisMetadataHolder analysisMetadataHolder, IssueTrackingDelegator issueTracking) {
47 super(CrawlerDepthLimit.FILE, POST_ORDER);
48 this.issueCache = issueCache;
49 this.issueLifecycle = issueLifecycle;
50 this.issueVisitors = issueVisitors;
51 this.issuesLoader = issuesLoader;
52 this.analysisMetadataHolder = analysisMetadataHolder;
53 this.issueTracking = issueTracking;
57 public void visitAny(Component component) {
58 try (DiskCache<DefaultIssue>.DiskAppender cacheAppender = issueCache.newAppender()) {
59 issueVisitors.beforeComponent(component);
61 if (isIncremental(component)) {
62 // no tracking needed, simply re-use existing issues
63 List<DefaultIssue> issues = issuesLoader.loadForComponentUuid(component.getUuid());
64 reuseOpenIssues(component, issues, cacheAppender);
66 TrackingResult tracking = issueTracking.track(component);
67 fillNewOpenIssues(component, tracking.newIssues(), cacheAppender);
68 fillExistingOpenIssues(component, tracking.issuesToMerge(), cacheAppender);
69 closeIssues(component, tracking.issuesToClose(), cacheAppender);
70 copyIssues(component, tracking.issuesToCopy(), cacheAppender);
72 issueVisitors.afterComponent(component);
73 } catch (Exception e) {
74 throw new IllegalStateException(String.format("Fail to process issues of component '%s'", component.getKey()), e);
78 private boolean isIncremental(Component component) {
79 return analysisMetadataHolder.isIncrementalAnalysis() && component.getStatus() == Status.SAME;
82 private void fillNewOpenIssues(Component component, Iterable<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
83 for (DefaultIssue issue : issues) {
84 issueLifecycle.initNewOpenIssue(issue);
85 process(component, issue, cacheAppender);
89 private void copyIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
90 for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
91 DefaultIssue raw = entry.getKey();
92 DefaultIssue base = entry.getValue();
93 issueLifecycle.copyExistingOpenIssue(raw, base);
94 process(component, raw, cacheAppender);
98 private void reuseOpenIssues(Component component, Collection<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
99 for (DefaultIssue issue : issues) {
100 process(component, issue, cacheAppender);
104 private void fillExistingOpenIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
105 for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
106 DefaultIssue raw = entry.getKey();
107 DefaultIssue base = entry.getValue();
108 issueLifecycle.mergeExistingOpenIssue(raw, base);
109 process(component, raw, cacheAppender);
113 private void closeIssues(Component component, Iterable<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
114 for (DefaultIssue issue : issues) {
115 // TODO should replace flag "beingClosed" by express call to transition "automaticClose"
116 issue.setBeingClosed(true);
117 // TODO manual issues -> was updater.setResolution(newIssue, Issue.RESOLUTION_REMOVED, changeContext);. Is it a problem ?
118 process(component, issue, cacheAppender);
122 private void process(Component component, DefaultIssue issue, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
123 issueLifecycle.doAutomaticTransition(issue);
124 issueVisitors.onIssue(component, issue);
125 cacheAppender.append(issue);