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