3 * Copyright (C) 2009-2023 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.Collection;
23 import java.util.List;
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;
31 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
34 * Updates issues if an anticipated transition from SonarLint is found
36 public class TransitionIssuesToAnticipatedStatesVisitor extends IssueVisitor {
38 private Collection<AnticipatedTransition> anticipatedTransitions;
39 private final AnticipatedTransitionTracker<DefaultIssue, AnticipatedTransition> tracker = new AnticipatedTransitionTracker<>();
40 private final IssueLifecycle issueLifecycle;
42 private final AnticipatedTransitionRepository anticipatedTransitionRepository;
44 public TransitionIssuesToAnticipatedStatesVisitor(AnticipatedTransitionRepository anticipatedTransitionRepository, IssueLifecycle issueLifecycle) {
45 this.anticipatedTransitionRepository = anticipatedTransitionRepository;
46 this.issueLifecycle = issueLifecycle;
50 public void beforeComponent(Component component) {
51 if (FILE.equals(component.getType())) {
52 anticipatedTransitions = anticipatedTransitionRepository.getAnticipatedTransitionByProjectUuid(component.getUuid(), component.getName());
57 public void onIssue(Component component, DefaultIssue issue) {
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));
68 public void afterComponent(Component component) {
69 anticipatedTransitions.clear();
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());