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.

NotificationFactory.java 7.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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.notification;
  21. import com.google.common.collect.ImmutableMap;
  22. import java.util.Map;
  23. import java.util.Optional;
  24. import java.util.Set;
  25. import javax.annotation.CheckForNull;
  26. import javax.annotation.Nullable;
  27. import org.sonar.api.ce.ComputeEngineSide;
  28. import org.sonar.api.rule.RuleKey;
  29. import org.sonar.api.utils.Durations;
  30. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  31. import org.sonar.ce.task.projectanalysis.analysis.Branch;
  32. import org.sonar.ce.task.projectanalysis.component.Component;
  33. import org.sonar.ce.task.projectanalysis.component.DepthTraversalTypeAwareCrawler;
  34. import org.sonar.ce.task.projectanalysis.component.TreeRootHolder;
  35. import org.sonar.ce.task.projectanalysis.component.TypeAwareVisitorAdapter;
  36. import org.sonar.ce.task.projectanalysis.issue.RuleRepository;
  37. import org.sonar.core.issue.DefaultIssue;
  38. import org.sonar.core.util.stream.MoreCollectors;
  39. import org.sonar.db.user.UserDto;
  40. import org.sonar.server.issue.notification.IssuesChangesNotification;
  41. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder;
  42. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.AnalysisChange;
  43. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.ChangedIssue;
  44. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.Project;
  45. import org.sonar.server.issue.notification.IssuesChangesNotificationBuilder.User;
  46. import org.sonar.server.issue.notification.IssuesChangesNotificationSerializer;
  47. import org.sonar.server.issue.notification.MyNewIssuesNotification;
  48. import org.sonar.server.issue.notification.NewIssuesNotification;
  49. import org.sonar.server.issue.notification.NewIssuesNotification.DetailsSupplier;
  50. import org.sonar.server.issue.notification.NewIssuesNotification.RuleDefinition;
  51. import static com.google.common.base.Preconditions.checkState;
  52. import static java.util.Objects.requireNonNull;
  53. import static org.sonar.ce.task.projectanalysis.component.ComponentVisitor.Order.PRE_ORDER;
  54. import static org.sonar.ce.task.projectanalysis.component.CrawlerDepthLimit.FILE;
  55. import static org.sonar.db.component.BranchType.PULL_REQUEST;
  56. @ComputeEngineSide
  57. public class NotificationFactory {
  58. private final TreeRootHolder treeRootHolder;
  59. private final AnalysisMetadataHolder analysisMetadataHolder;
  60. private final RuleRepository ruleRepository;
  61. private final Durations durations;
  62. private final IssuesChangesNotificationSerializer issuesChangesSerializer;
  63. private Map<String, Component> componentsByUuid;
  64. public NotificationFactory(TreeRootHolder treeRootHolder, AnalysisMetadataHolder analysisMetadataHolder,
  65. RuleRepository ruleRepository, Durations durations, IssuesChangesNotificationSerializer issuesChangesSerializer) {
  66. this.treeRootHolder = treeRootHolder;
  67. this.analysisMetadataHolder = analysisMetadataHolder;
  68. this.ruleRepository = ruleRepository;
  69. this.durations = durations;
  70. this.issuesChangesSerializer = issuesChangesSerializer;
  71. }
  72. public MyNewIssuesNotification newMyNewIssuesNotification(Map<String, UserDto> assigneesByUuid) {
  73. verifyAssigneesByUuid(assigneesByUuid);
  74. return new MyNewIssuesNotification(durations, new DetailsSupplierImpl(assigneesByUuid));
  75. }
  76. public NewIssuesNotification newNewIssuesNotification(Map<String, UserDto> assigneesByUuid) {
  77. verifyAssigneesByUuid(assigneesByUuid);
  78. return new NewIssuesNotification(durations, new DetailsSupplierImpl(assigneesByUuid));
  79. }
  80. public IssuesChangesNotification newIssuesChangesNotification(Set<DefaultIssue> issues, Map<String, UserDto> assigneesByUuid) {
  81. AnalysisChange change = new AnalysisChange(analysisMetadataHolder.getAnalysisDate());
  82. Set<ChangedIssue> changedIssues = issues.stream()
  83. .map(issue -> new ChangedIssue.Builder(issue.key())
  84. .setAssignee(getAssignee(issue.assignee(), assigneesByUuid))
  85. .setNewResolution(issue.resolution())
  86. .setNewStatus(issue.status())
  87. .setRule(getRuleByRuleKey(issue.ruleKey()))
  88. .setProject(getProject())
  89. .build())
  90. .collect(MoreCollectors.toSet(issues.size()));
  91. return issuesChangesSerializer.serialize(new IssuesChangesNotificationBuilder(changedIssues, change));
  92. }
  93. @CheckForNull
  94. public User getAssignee(@Nullable String assigneeUuid, Map<String, UserDto> assigneesByUuid) {
  95. if (assigneeUuid == null) {
  96. return null;
  97. }
  98. UserDto dto = assigneesByUuid.get(assigneeUuid);
  99. checkState(dto != null, "Can not find DTO for assignee uuid %s", assigneeUuid);
  100. return new User(dto.getUuid(), dto.getLogin(), dto.getName());
  101. }
  102. private IssuesChangesNotificationBuilder.Rule getRuleByRuleKey(RuleKey ruleKey) {
  103. return ruleRepository.findByKey(ruleKey)
  104. .map(t -> new IssuesChangesNotificationBuilder.Rule(ruleKey, t.getType(), t.getName()))
  105. .orElseThrow(() -> new IllegalStateException("Can not find rule " + ruleKey + " in RuleRepository"));
  106. }
  107. private Project getProject() {
  108. Component project = treeRootHolder.getRoot();
  109. Branch branch = analysisMetadataHolder.getBranch();
  110. Project.Builder builder = new Project.Builder(project.getUuid())
  111. .setKey(project.getKey())
  112. .setProjectName(project.getName());
  113. if (branch.getType() != PULL_REQUEST && !branch.isMain()) {
  114. builder.setBranchName(branch.getName());
  115. }
  116. return builder.build();
  117. }
  118. private static void verifyAssigneesByUuid(Map<String, UserDto> assigneesByUuid) {
  119. requireNonNull(assigneesByUuid, "assigneesByUuid can't be null");
  120. }
  121. private class DetailsSupplierImpl implements DetailsSupplier {
  122. private final Map<String, UserDto> assigneesByUuid;
  123. private DetailsSupplierImpl(Map<String, UserDto> assigneesByUuid) {
  124. this.assigneesByUuid = assigneesByUuid;
  125. }
  126. @Override
  127. public Optional<RuleDefinition> getRuleDefinitionByRuleKey(RuleKey ruleKey) {
  128. requireNonNull(ruleKey, "ruleKey can't be null");
  129. return ruleRepository.findByKey(ruleKey)
  130. .map(t -> new RuleDefinition(t.getName(), t.getLanguage()));
  131. }
  132. @Override
  133. public Optional<String> getComponentNameByUuid(String uuid) {
  134. requireNonNull(uuid, "uuid can't be null");
  135. return Optional.ofNullable(lazyLoadComponentsByUuid().get(uuid))
  136. .map(t -> t.getType() == Component.Type.FILE || t.getType() == Component.Type.DIRECTORY ? t.getShortName() : t.getName());
  137. }
  138. private Map<String, Component> lazyLoadComponentsByUuid() {
  139. if (componentsByUuid == null) {
  140. ImmutableMap.Builder<String, Component> builder = ImmutableMap.builder();
  141. new DepthTraversalTypeAwareCrawler(new TypeAwareVisitorAdapter(FILE, PRE_ORDER) {
  142. @Override
  143. public void visitAny(Component any) {
  144. builder.put(any.getUuid(), any);
  145. }
  146. }).visit(treeRootHolder.getRoot());
  147. componentsByUuid = builder.build();
  148. }
  149. return componentsByUuid;
  150. }
  151. @Override
  152. public Optional<String> getUserNameByUuid(String uuid) {
  153. requireNonNull(uuid, "uuid can't be null");
  154. return Optional.ofNullable(assigneesByUuid.get(uuid))
  155. .map(UserDto::getName);
  156. }
  157. }
  158. }