You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

IntegrateIssuesVisitor.java 7.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. import java.util.Collection;
  22. import java.util.LinkedList;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.stream.Stream;
  26. import org.sonar.ce.task.projectanalysis.component.Component;
  27. import org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit;
  28. import org.sonar.ce.task.projectanalysis.component.FileStatuses;
  29. import org.sonar.ce.task.projectanalysis.component.ReferenceBranchComponentUuids;
  30. import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
  31. import org.sonar.ce.task.projectanalysis.util.cache.DiskCache.CacheAppender;
  32. import org.sonar.core.issue.DefaultIssue;
  33. import org.sonar.core.issue.tracking.Input;
  34. import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.POST_ORDER;
  35. public class IntegrateIssuesVisitor extends TypeAwareVisitorAdapter {
  36. private final ProtoIssueCache protoIssueCache;
  37. private final TrackerRawInputFactory rawInputFactory;
  38. private final TrackerBaseInputFactory baseInputFactory;
  39. private final IssueLifecycle issueLifecycle;
  40. private final IssueVisitors issueVisitors;
  41. private final IssueTrackingDelegator issueTracking;
  42. private final SiblingsIssueMerger issueStatusCopier;
  43. private final ReferenceBranchComponentUuids referenceBranchComponentUuids;
  44. private final PullRequestSourceBranchMerger pullRequestSourceBranchMerger;
  45. private final FileStatuses fileStatuses;
  46. public IntegrateIssuesVisitor(
  47. ProtoIssueCache protoIssueCache,
  48. TrackerRawInputFactory rawInputFactory,
  49. TrackerBaseInputFactory baseInputFactory,
  50. IssueLifecycle issueLifecycle,
  51. IssueVisitors issueVisitors,
  52. IssueTrackingDelegator issueTracking,
  53. SiblingsIssueMerger issueStatusCopier,
  54. ReferenceBranchComponentUuids referenceBranchComponentUuids,
  55. PullRequestSourceBranchMerger pullRequestSourceBranchMerger,
  56. FileStatuses fileStatuses) {
  57. super(CrawlerDepthLimit.FILE, POST_ORDER);
  58. this.protoIssueCache = protoIssueCache;
  59. this.rawInputFactory = rawInputFactory;
  60. this.baseInputFactory = baseInputFactory;
  61. this.issueLifecycle = issueLifecycle;
  62. this.issueVisitors = issueVisitors;
  63. this.issueTracking = issueTracking;
  64. this.issueStatusCopier = issueStatusCopier;
  65. this.referenceBranchComponentUuids = referenceBranchComponentUuids;
  66. this.pullRequestSourceBranchMerger = pullRequestSourceBranchMerger;
  67. this.fileStatuses = fileStatuses;
  68. }
  69. @Override
  70. public void visitAny(Component component) {
  71. try (CacheAppender<DefaultIssue> cacheAppender = protoIssueCache.newAppender()) {
  72. issueVisitors.beforeComponent(component);
  73. if (fileStatuses.isDataUnchanged(component)) {
  74. // we assume there's a previous analysis of the same branch
  75. Input<DefaultIssue> baseIssues = baseInputFactory.create(component);
  76. var issues = new LinkedList<>(baseIssues.getIssues());
  77. processIssues(component, issues);
  78. issueVisitors.beforeCaching(component);
  79. appendIssuesToCache(cacheAppender, issues);
  80. } else {
  81. Input<DefaultIssue> rawInput = rawInputFactory.create(component);
  82. TrackingResult tracking = issueTracking.track(component, rawInput);
  83. var newOpenIssues = fillNewOpenIssues(component, tracking.newIssues(), rawInput);
  84. var existingOpenIssues = fillExistingOpenIssues(tracking.issuesToMerge());
  85. var closedIssues = closeIssues(tracking.issuesToClose());
  86. var copiedIssues = copyIssues(tracking.issuesToCopy());
  87. var issues = Stream.of(newOpenIssues, existingOpenIssues, closedIssues, copiedIssues)
  88. .flatMap(Collection::stream)
  89. .toList();
  90. processIssues(component, issues);
  91. issueVisitors.beforeCaching(component);
  92. appendIssuesToCache(cacheAppender, issues);
  93. }
  94. issueVisitors.afterComponent(component);
  95. } catch (Exception e) {
  96. throw new IllegalStateException(String.format("Fail to process issues of component '%s'", component.getKey()), e);
  97. }
  98. }
  99. private void processIssues(Component component, Collection<DefaultIssue> issues) {
  100. issues.forEach(issue -> processIssue(component, issue));
  101. }
  102. private List<DefaultIssue> fillNewOpenIssues(Component component, Stream<DefaultIssue> newIssues, Input<DefaultIssue> rawInput) {
  103. List<DefaultIssue> newIssuesList = newIssues
  104. .peek(issueLifecycle::initNewOpenIssue)
  105. .toList();
  106. if (newIssuesList.isEmpty()) {
  107. return newIssuesList;
  108. }
  109. pullRequestSourceBranchMerger.tryMergeIssuesFromSourceBranchOfPullRequest(component, newIssuesList, rawInput);
  110. issueStatusCopier.tryMerge(component, newIssuesList);
  111. return newIssuesList;
  112. }
  113. private List<DefaultIssue> fillExistingOpenIssues(Map<DefaultIssue, DefaultIssue> matched) {
  114. List<DefaultIssue> newIssuesList = new LinkedList<>();
  115. for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
  116. DefaultIssue raw = entry.getKey();
  117. DefaultIssue base = entry.getValue();
  118. issueLifecycle.mergeExistingOpenIssue(raw, base);
  119. newIssuesList.add(raw);
  120. }
  121. return newIssuesList;
  122. }
  123. private static List<DefaultIssue> closeIssues(Stream<DefaultIssue> issues) {
  124. return issues.map(issue ->
  125. // TODO should replace flag "beingClosed" by express call to transition "automaticClose"
  126. issue.setBeingClosed(true)
  127. // TODO manual issues -> was updater.setResolution(newIssue, Issue.RESOLUTION_REMOVED, changeContext);. Is it a problem ?
  128. ).toList();
  129. }
  130. private List<DefaultIssue> copyIssues(Map<DefaultIssue, DefaultIssue> matched) {
  131. List<DefaultIssue> newIssuesList = new LinkedList<>();
  132. for (Map.Entry<DefaultIssue, DefaultIssue> entry : matched.entrySet()) {
  133. DefaultIssue raw = entry.getKey();
  134. DefaultIssue base = entry.getValue();
  135. issueLifecycle.copyExistingOpenIssueFromBranch(raw, base, referenceBranchComponentUuids.getReferenceBranchName());
  136. newIssuesList.add(raw);
  137. }
  138. return newIssuesList;
  139. }
  140. private void processIssue(Component component, DefaultIssue issue) {
  141. issueLifecycle.doAutomaticTransition(issue);
  142. issueVisitors.onIssue(component, issue);
  143. }
  144. private static void appendIssuesToCache(CacheAppender<DefaultIssue> cacheAppender, Collection<DefaultIssue> issues) {
  145. issues.forEach(issue -> appendIssue(issue, cacheAppender));
  146. }
  147. private static void appendIssue(DefaultIssue issue, CacheAppender<DefaultIssue> cacheAppender) {
  148. if (issue.isNew() || issue.isChanged() || issue.isCopied() || issue.isNoLongerNewCodeReferenceIssue() || issue.isToBeMigratedAsNewCodeReferenceIssue()) {
  149. cacheAppender.append(issue);
  150. }
  151. }
  152. }