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 java.util.ArrayList;
23 import java.util.List;
25 import org.sonar.core.issue.DefaultIssue;
26 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
27 import org.sonar.server.computation.task.projectanalysis.component.Component;
28 import org.sonar.server.computation.task.projectanalysis.component.CrawlerDepthLimit;
29 import org.sonar.server.computation.task.projectanalysis.component.TypeAwareVisitorAdapter;
30 import org.sonar.server.util.cache.DiskCache;
32 import static org.sonar.server.computation.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
34 public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter {
36 private final IssueCache issueCache;
37 private final IssueLifecycle issueLifecycle;
38 private final IssueVisitors issueVisitors;
39 private final IssueTrackingDelegator issueTracking;
40 private final ShortBranchIssueStatusCopier issueStatusCopier;
41 private final AnalysisMetadataHolder analysisMetadataHolder;
43 public IntegrateIssuesVisitor(IssueCache issueCache, IssueLifecycle issueLifecycle, IssueVisitors issueVisitors,
44 AnalysisMetadataHolder analysisMetadataHolder, IssueTrackingDelegator issueTracking, ShortBranchIssueStatusCopier issueStatusCopier) {
45 super(CrawlerDepthLimit.FILE, POST_ORDER);
46 this.issueCache = issueCache;
47 this.issueLifecycle = issueLifecycle;
48 this.issueVisitors = issueVisitors;
49 this.analysisMetadataHolder = analysisMetadataHolder;
50 this.issueTracking = issueTracking;
51 this.issueStatusCopier = issueStatusCopier;
55 public void visitAny(Component component) {
56 try (DiskCache<DefaultIssue>.DiskAppender cacheAppender = issueCache.newAppender()) {
57 issueVisitors.beforeComponent(component);
59 TrackingResult tracking = issueTracking.track(component);
60 fillNewOpenIssues(component, tracking.newIssues(), cacheAppender);
61 fillExistingOpenIssues(component, tracking.issuesToMerge(), cacheAppender);
62 closeIssues(component, tracking.issuesToClose(), cacheAppender);
63 copyIssues(component, tracking.issuesToCopy(), cacheAppender);
64 issueVisitors.afterComponent(component);
65 } catch (Exception e) {
66 throw new IllegalStateException(String.format("Fail to process issues of component '%s'", component.getKey()), e);
70 private void fillNewOpenIssues(Component component, Iterable<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
71 List<DefaultIssue> list = new ArrayList<>();
73 issues.forEach(issue -> {
74 issueLifecycle.initNewOpenIssue(issue);
82 if (analysisMetadataHolder.isLongLivingBranch()) {
83 issueStatusCopier.updateStatus(component, list);
86 for (DefaultIssue issue : list) {
87 process(component, issue, cacheAppender);
91 private void copyIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
92 for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
93 DefaultIssue raw = entry.getKey();
94 DefaultIssue base = entry.getValue();
95 issueLifecycle.copyExistingOpenIssue(raw, base);
96 process(component, raw, cacheAppender);
100 private void fillExistingOpenIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
101 for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
102 DefaultIssue raw = entry.getKey();
103 DefaultIssue base = entry.getValue();
104 issueLifecycle.mergeExistingOpenIssue(raw, base);
105 process(component, raw, cacheAppender);
109 private void closeIssues(Component component, Iterable<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
110 for (DefaultIssue issue : issues) {
111 // TODO should replace flag "beingClosed" by express call to transition "automaticClose"
112 issue.setBeingClosed(true);
113 // TODO manual issues -> was updater.setResolution(newIssue, Issue.RESOLUTION_REMOVED, changeContext);. Is it a problem ?
114 process(component, issue, cacheAppender);
118 private void process(Component component, DefaultIssue issue, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
119 issueLifecycle.doAutomaticTransition(issue);
120 issueVisitors.onIssue(component, issue);
121 cacheAppender.append(issue);