3 * Copyright (C) 2009-2020 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.ce.task.projectanalysis.issue;
22 import java.util.List;
24 import java.util.stream.Stream;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
27 import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
28 import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
29 import org.sonar.ce.task.projectanalysis.util.cache.DiskCache;
30 import org.sonar.core.issue.DefaultIssue;
31 import org.sonar.core.util.stream.MoreCollectors;
33 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
35 public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter {
37 private final IssueCache issueCache;
38 private final IssueLifecycle issueLifecycle;
39 private final IssueVisitors issueVisitors;
40 private final IssueTrackingDelegator issueTracking;
41 private final SiblingsIssueMerger issueStatusCopier;
42 private final ReferenceBranchComponentUuids referenceBranchComponentUuids;
44 public IntegrateIssuesVisitor(IssueCache issueCache, IssueLifecycle issueLifecycle, IssueVisitors issueVisitors, IssueTrackingDelegator issueTracking,
45 SiblingsIssueMerger issueStatusCopier, ReferenceBranchComponentUuids referenceBranchComponentUuids) {
46 super(CrawlerDepthLimit.FILE, POST_ORDER);
47 this.issueCache = issueCache;
48 this.issueLifecycle = issueLifecycle;
49 this.issueVisitors = issueVisitors;
50 this.issueTracking = issueTracking;
51 this.issueStatusCopier = issueStatusCopier;
52 this.referenceBranchComponentUuids = referenceBranchComponentUuids;
56 public void visitAny(Component component) {
57 try (DiskCache<DefaultIssue>.DiskAppender cacheAppender = issueCache.newAppender()) {
58 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.getDbKey()), e);
70 private void fillNewOpenIssues(Component component, Stream<DefaultIssue> newIssues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
71 List<DefaultIssue> newIssuesList = newIssues
72 .peek(issueLifecycle::initNewOpenIssue)
73 .collect(MoreCollectors.toList());
75 if (newIssuesList.isEmpty()) {
79 issueStatusCopier.tryMerge(component, newIssuesList);
81 for (DefaultIssue issue : newIssuesList) {
82 process(component, issue, cacheAppender);
86 private void copyIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
87 for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
88 DefaultIssue raw = entry.getKey();
89 DefaultIssue base = entry.getValue();
90 issueLifecycle.copyExistingOpenIssueFromBranch(raw, base, referenceBranchComponentUuids.getReferenceBranchName());
91 process(component, raw, cacheAppender);
95 private void fillExistingOpenIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
96 for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
97 DefaultIssue raw = entry.getKey();
98 DefaultIssue base = entry.getValue();
99 issueLifecycle.mergeExistingOpenIssue(raw, base);
100 process(component, raw, cacheAppender);
104 private void closeIssues(Component component, Stream<DefaultIssue> issues, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
105 issues.forEach(issue -> {
106 // TODO should replace flag "beingClosed" by express call to transition "automaticClose"
107 issue.setBeingClosed(true);
108 // TODO manual issues -> was updater.setResolution(newIssue, Issue.RESOLUTION_REMOVED, changeContext);. Is it a problem ?
109 process(component, issue, cacheAppender);
113 private void process(Component component, DefaultIssue issue, DiskCache<DefaultIssue>.DiskAppender cacheAppender) {
114 issueLifecycle.doAutomaticTransition(issue);
115 issueVisitors.onIssue(component, issue);
116 cacheAppender.append(issue);