Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

TransitionIssuesToAnticipatedStatesVisitor.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 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. import java.time.Instant;
  22. import java.util.Collection;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.Map;
  26. import org.apache.logging.log4j.util.Strings;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import org.sonar.ce.task.log.CeTaskMessages;
  30. import org.sonar.ce.task.projectanalysis.component.Component;
  31. import org.sonar.core.issue.AnticipatedTransition;
  32. import org.sonar.core.issue.DefaultIssue;
  33. import org.sonar.core.issue.tracking.AnticipatedTransitionTracker;
  34. import org.sonar.core.issue.tracking.Tracking;
  35. import org.sonar.db.dismissmessage.MessageType;
  36. import static org.sonar.api.issue.Issue.STATUS_OPEN;
  37. import static org.sonar.ce.task.projectanalysis.component.Component.Type.FILE;
  38. /**
  39. * Updates issues if an anticipated transition from SonarLint is found
  40. */
  41. public class TransitionIssuesToAnticipatedStatesVisitor extends IssueVisitor {
  42. private static final Logger LOGGER = LoggerFactory.getLogger(TransitionIssuesToAnticipatedStatesVisitor.class);
  43. public static final String TRANSITION_ERROR_TEMPLATE = "Cannot resolve issue at line {} of {} due to: {}";
  44. private Collection<AnticipatedTransition> anticipatedTransitions;
  45. private final AnticipatedTransitionTracker<DefaultIssue, AnticipatedTransition> tracker = new AnticipatedTransitionTracker<>();
  46. private final IssueLifecycle issueLifecycle;
  47. private final CeTaskMessages ceTaskMessages;
  48. private final AnticipatedTransitionRepository anticipatedTransitionRepository;
  49. public TransitionIssuesToAnticipatedStatesVisitor(AnticipatedTransitionRepository anticipatedTransitionRepository,
  50. IssueLifecycle issueLifecycle, CeTaskMessages ceTaskMessages) {
  51. this.anticipatedTransitionRepository = anticipatedTransitionRepository;
  52. this.issueLifecycle = issueLifecycle;
  53. this.ceTaskMessages = ceTaskMessages;
  54. }
  55. @Override
  56. public void beforeComponent(Component component) {
  57. if (FILE.equals(component.getType())) {
  58. anticipatedTransitions = anticipatedTransitionRepository.getAnticipatedTransitionByComponent(component);
  59. } else {
  60. anticipatedTransitions = Collections.emptyList();
  61. }
  62. }
  63. @Override
  64. public void onIssue(Component component, DefaultIssue issue) {
  65. if (isEligibleForAnticipatedTransitions(issue)) {
  66. Tracking<DefaultIssue, AnticipatedTransition> tracking = tracker.track(List.of(issue), anticipatedTransitions);
  67. Map<DefaultIssue, AnticipatedTransition> matchedRaws = tracking.getMatchedRaws();
  68. if (matchedRaws.containsKey(issue)) {
  69. performAnticipatedTransition(issue, matchedRaws.get(issue));
  70. }
  71. }
  72. }
  73. private static boolean isEligibleForAnticipatedTransitions(DefaultIssue issue) {
  74. return issue.isNew() && STATUS_OPEN.equals(issue.getStatus()) && null == issue.resolution();
  75. }
  76. private void performAnticipatedTransition(DefaultIssue issue, AnticipatedTransition anticipatedTransition) {
  77. try {
  78. issueLifecycle.doManualTransition(issue, anticipatedTransition.getTransition(), anticipatedTransition.getUserUuid());
  79. String transitionComment = anticipatedTransition.getComment();
  80. String comment = Strings.isNotBlank(transitionComment) ? transitionComment : "Automatically transitioned from SonarLint";
  81. issueLifecycle.addComment(issue, comment, anticipatedTransition.getUserUuid());
  82. issue.setBeingClosed(true);
  83. issue.setAnticipatedTransitionUuid(anticipatedTransition.getUuid());
  84. } catch (Exception e) {
  85. LOGGER.warn(TRANSITION_ERROR_TEMPLATE, issue.getLine(), issue.componentKey(), e.getMessage());
  86. ceTaskMessages.add(
  87. new CeTaskMessages.Message(getMessage(issue, e),
  88. Instant.now().toEpochMilli(),
  89. MessageType.GENERIC));
  90. }
  91. }
  92. private static String getMessage(DefaultIssue issue, Exception e) {
  93. final int MAX_LENGTH = 50;
  94. int componentKeyLength = issue.componentKey().length();
  95. String componentKey = componentKeyLength > MAX_LENGTH ? ("..." + issue.componentKey().substring(componentKeyLength - MAX_LENGTH, componentKeyLength)) : issue.componentKey();
  96. return String.format(TRANSITION_ERROR_TEMPLATE.replace("{}", "%s"), issue.getLine(), componentKey, e.getMessage());
  97. }
  98. }