]> source.dussan.org Git - sonarqube.git/blob
c7a3e53b71733c58d6fc44692271155eac4454aa
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2023 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.Collection;
23 import java.util.List;
24 import java.util.Map;
25 import org.sonar.ce.task.projectanalysis.component.Component;
26 import org.sonar.core.issue.AnticipatedTransition;
27 import org.sonar.core.issue.DefaultIssue;
28 import org.sonar.core.issue.tracking.AnticipatedTransitionTracker;
29 import org.sonar.core.issue.tracking.Tracking;
30
31 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
32
33 /**
34  * Updates issues if an anticipated transition from SonarLint is found
35  */
36 public class TransitionIssuesToAnticipatedStatesVisitor extends IssueVisitor {
37
38   private Collection<AnticipatedTransition> anticipatedTransitions;
39   private final AnticipatedTransitionTracker<DefaultIssue, AnticipatedTransition> tracker = new AnticipatedTransitionTracker<>();
40   private final IssueLifecycle issueLifecycle;
41   
42   private final AnticipatedTransitionRepository anticipatedTransitionRepository;
43
44   public TransitionIssuesToAnticipatedStatesVisitor(AnticipatedTransitionRepository anticipatedTransitionRepository, IssueLifecycle issueLifecycle) {
45     this.anticipatedTransitionRepository = anticipatedTransitionRepository;
46     this.issueLifecycle = issueLifecycle;
47   }
48
49   @Override
50   public void beforeComponent(Component component) {
51     if (FILE.equals(component.getType())) {
52       anticipatedTransitions = anticipatedTransitionRepository.getAnticipatedTransitionByProjectUuid(component.getUuid(), component.getName());
53     }
54   }
55
56   @Override
57   public void onIssue(Component component, DefaultIssue issue) {
58     if (issue.isNew()) {
59       Tracking<DefaultIssue, AnticipatedTransition> tracking = tracker.track(List.of(issue), anticipatedTransitions);
60       Map<DefaultIssue, AnticipatedTransition> matchedRaws = tracking.getMatchedRaws();
61       if (matchedRaws.containsKey(issue)) {
62         performAnticipatedTransition(issue, matchedRaws.get(issue));
63       }
64     }
65   }
66
67   @Override
68   public void afterComponent(Component component) {
69     anticipatedTransitions.clear();
70   }
71
72   private void performAnticipatedTransition(DefaultIssue issue, AnticipatedTransition anticipatedTransition) {
73     issue.setBeingClosed(true);
74     issue.setAnticipatedTransitions(true);
75     issueLifecycle.doManualTransition(issue, anticipatedTransition.getTransition(), anticipatedTransition.getUserUuid());
76     issueLifecycle.addComment(issue, anticipatedTransition.getComment(), anticipatedTransition.getUserUuid());
77   }
78
79 }