]> source.dussan.org Git - sonarqube.git/blob
47af93eaade5067af9745116ab62d5853ac7c871
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.ce.task.projectanalysis.issue;
21
22 import java.util.List;
23 import java.util.Map;
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.CacheAppender;
30 import org.sonar.core.issue.DefaultIssue;
31 import org.sonar.core.issue.tracking.Input;
32 import org.sonar.core.util.stream.MoreCollectors;
33
34 import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
35
36 public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter {
37
38   private final ProtoIssueCache protoIssueCache;
39   private final TrackerRawInputFactory rawInputFactory;
40   private final IssueLifecycle issueLifecycle;
41   private final IssueVisitors issueVisitors;
42   private final IssueTrackingDelegator issueTracking;
43   private final SiblingsIssueMerger issueStatusCopier;
44   private final ReferenceBranchComponentUuids referenceBranchComponentUuids;
45   private final PullRequestSourceBranchMerger pullRequestSourceBranchMerger;
46
47   public IntegrateIssuesVisitor(
48     ProtoIssueCache protoIssueCache,
49     TrackerRawInputFactory rawInputFactory,
50     IssueLifecycle issueLifecycle,
51     IssueVisitors issueVisitors,
52     IssueTrackingDelegator issueTracking,
53     SiblingsIssueMerger issueStatusCopier,
54     ReferenceBranchComponentUuids referenceBranchComponentUuids,
55     PullRequestSourceBranchMerger pullRequestSourceBranchMerger) {
56     super(CrawlerDepthLimit.FILE, POST_ORDER);
57     this.protoIssueCache = protoIssueCache;
58     this.rawInputFactory = rawInputFactory;
59     this.issueLifecycle = issueLifecycle;
60     this.issueVisitors = issueVisitors;
61     this.issueTracking = issueTracking;
62     this.issueStatusCopier = issueStatusCopier;
63     this.referenceBranchComponentUuids = referenceBranchComponentUuids;
64     this.pullRequestSourceBranchMerger = pullRequestSourceBranchMerger;
65   }
66
67   @Override
68   public void visitAny(Component component) {
69     try (CacheAppender<DefaultIssue> cacheAppender = protoIssueCache.newAppender()) {
70       issueVisitors.beforeComponent(component);
71       Input<DefaultIssue> rawInput = rawInputFactory.create(component);
72       TrackingResult tracking = issueTracking.track(component, rawInput);
73       fillNewOpenIssues(component, tracking.newIssues(), rawInput, cacheAppender);
74       fillExistingOpenIssues(component, tracking.issuesToMerge(), cacheAppender);
75       closeIssues(component, tracking.issuesToClose(), cacheAppender);
76       copyIssues(component, tracking.issuesToCopy(), cacheAppender);
77       issueVisitors.afterComponent(component);
78     } catch (Exception e) {
79       throw new IllegalStateException(String.format("Fail to process issues of component '%s'", component.getDbKey()), e);
80     }
81   }
82
83   private void fillNewOpenIssues(Component component, Stream<DefaultIssue> newIssues, Input<DefaultIssue> rawInput, CacheAppender<DefaultIssue> cacheAppender) {
84     List<DefaultIssue> newIssuesList = newIssues
85       .peek(issueLifecycle::initNewOpenIssue)
86       .collect(MoreCollectors.toList());
87
88     if (newIssuesList.isEmpty()) {
89       return;
90     }
91
92     pullRequestSourceBranchMerger.tryMergeIssuesFromSourceBranchOfPullRequest(component, newIssuesList, rawInput);
93     issueStatusCopier.tryMerge(component, newIssuesList);
94
95     for (DefaultIssue issue : newIssuesList) {
96       process(component, issue, cacheAppender);
97     }
98   }
99
100   private void copyIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, CacheAppender<DefaultIssue> cacheAppender) {
101     for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
102       DefaultIssue raw = entry.getKey();
103       DefaultIssue base = entry.getValue();
104       issueLifecycle.copyExistingOpenIssueFromBranch(raw, base, referenceBranchComponentUuids.getReferenceBranchName());
105       process(component, raw, cacheAppender);
106     }
107   }
108
109   private void fillExistingOpenIssues(Component component, Map<DefaultIssue, DefaultIssue> matched, CacheAppender<DefaultIssue> cacheAppender) {
110     for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
111       DefaultIssue raw = entry.getKey();
112       DefaultIssue base = entry.getValue();
113       issueLifecycle.mergeExistingOpenIssue(raw, base);
114       process(component, raw, cacheAppender);
115     }
116   }
117
118   private void closeIssues(Component component, Stream<DefaultIssue> issues, CacheAppender<DefaultIssue> cacheAppender) {
119     issues.forEach(issue -> {
120       // TODO should replace flag "beingClosed" by express call to transition "automaticClose"
121       issue.setBeingClosed(true);
122       // TODO manual issues -> was updater.setResolution(newIssue, Issue.RESOLUTION_REMOVED, changeContext);. Is it a problem ?
123       process(component, issue, cacheAppender);
124     });
125   }
126
127   private void process(Component component, DefaultIssue issue, CacheAppender<DefaultIssue> cacheAppender) {
128     issueLifecycle.doAutomaticTransition(issue);
129     issueVisitors.onIssue(component, issue);
130     if (issue.isNew() || issue.isChanged() || issue.isCopied() || issue.isNoLongerNewCodeReferenceIssue()) {
131       cacheAppender.append(issue);
132     }
133   }
134
135 }