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.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;
32 import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
35 * Updates issues if an anticipated transition from SonarLint is found
37 public class TransitionIssuesToAnticipatedStatesVisitor extends IssueVisitor {
39 private Collection<AnticipatedTransition> anticipatedTransitions;
40 private final AnticipatedTransitionTracker<DefaultIssue, AnticipatedTransition> tracker = new AnticipatedTransitionTracker<>();
41 private final IssueLifecycle issueLifecycle;
43 private final AnticipatedTransitionRepository anticipatedTransitionRepository;
45 public TransitionIssuesToAnticipatedStatesVisitor(AnticipatedTransitionRepository anticipatedTransitionRepository, IssueLifecycle issueLifecycle) {
46 this.anticipatedTransitionRepository = anticipatedTransitionRepository;
47 this.issueLifecycle = issueLifecycle;
51 public void beforeComponent(Component component) {
52 if (FILE.equals(component.getType())) {
53 anticipatedTransitions = anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component);
58 public void onIssue(Component component, DefaultIssue issue) {
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));
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());