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.

IssueAssigner.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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.util.Comparator;
  22. import java.util.Date;
  23. import java.util.Optional;
  24. import org.apache.commons.lang3.StringUtils;
  25. import org.slf4j.Logger;
  26. import org.slf4j.LoggerFactory;
  27. import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
  28. import org.sonar.ce.task.projectanalysis.component.Component;
  29. import org.sonar.ce.task.projectanalysis.scm.Changeset;
  30. import org.sonar.ce.task.projectanalysis.scm.ScmInfo;
  31. import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepository;
  32. import org.sonar.core.issue.DefaultIssue;
  33. import org.sonar.core.issue.IssueChangeContext;
  34. import org.sonar.db.issue.IssueDto;
  35. import org.sonar.db.user.UserIdDto;
  36. import org.sonar.server.issue.IssueFieldsSetter;
  37. import static org.apache.commons.lang3.StringUtils.defaultIfEmpty;
  38. import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByScanBuilder;
  39. /**
  40. * Detect the SCM author and SQ assignee.
  41. * <p/>
  42. * It relies on SCM information which comes from both the report and database.
  43. */
  44. public class IssueAssigner extends IssueVisitor {
  45. private static final Logger LOGGER = LoggerFactory.getLogger(IssueAssigner.class);
  46. private final ScmInfoRepository scmInfoRepository;
  47. private final DefaultAssignee defaultAssignee;
  48. private final IssueFieldsSetter issueUpdater;
  49. private final ScmAccountToUser scmAccountToUser;
  50. private final IssueChangeContext changeContext;
  51. private String lastCommitAuthor = null;
  52. private ScmInfo scmChangesets = null;
  53. public IssueAssigner(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository, ScmAccountToUser scmAccountToUser, DefaultAssignee defaultAssignee,
  54. IssueFieldsSetter issueUpdater) {
  55. this.scmInfoRepository = scmInfoRepository;
  56. this.scmAccountToUser = scmAccountToUser;
  57. this.defaultAssignee = defaultAssignee;
  58. this.issueUpdater = issueUpdater;
  59. this.changeContext = issueChangeContextByScanBuilder(new Date(analysisMetadataHolder.getAnalysisDate())).build();
  60. }
  61. @Override
  62. public void onIssue(Component component, DefaultIssue issue) {
  63. if (issue.authorLogin() != null) {
  64. return;
  65. }
  66. loadScmChangesets(component);
  67. Optional<String> scmAuthor = guessScmAuthor(issue, component);
  68. if (scmAuthor.isPresent()) {
  69. if (scmAuthor.get().length() <= IssueDto.AUTHOR_MAX_SIZE) {
  70. issueUpdater.setNewAuthor(issue, scmAuthor.get(), changeContext);
  71. } else {
  72. LOGGER.debug("SCM account '{}' is too long to be stored as issue author", scmAuthor.get());
  73. }
  74. }
  75. if (issue.assignee() == null) {
  76. UserIdDto userId = scmAuthor.map(scmAccountToUser::getNullable).orElse(defaultAssignee.loadDefaultAssigneeUserId());
  77. issueUpdater.setNewAssignee(issue, userId, changeContext);
  78. }
  79. }
  80. private void loadScmChangesets(Component component) {
  81. if (scmChangesets == null) {
  82. Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
  83. if (scmInfoOptional.isPresent()) {
  84. scmChangesets = scmInfoOptional.get();
  85. lastCommitAuthor = scmChangesets.getLatestChangeset().getAuthor();
  86. }
  87. }
  88. }
  89. @Override
  90. public void afterComponent(Component component) {
  91. lastCommitAuthor = null;
  92. scmChangesets = null;
  93. }
  94. /**
  95. * Author of the latest change on the lines involved by the issue.
  96. * If no authors are set on the lines, then the author of the latest change on the file
  97. * is returned.
  98. */
  99. private Optional<String> guessScmAuthor(DefaultIssue issue, Component component) {
  100. String author = null;
  101. if (scmChangesets != null) {
  102. author = IssueLocations.allLinesFor(issue, component.getUuid())
  103. .filter(scmChangesets::hasChangesetForLine)
  104. .mapToObj(scmChangesets::getChangesetForLine)
  105. .filter(c -> StringUtils.isNotEmpty(c.getAuthor()))
  106. .max(Comparator.comparingLong(Changeset::getDate))
  107. .map(Changeset::getAuthor)
  108. .orElse(null);
  109. }
  110. return Optional.ofNullable(defaultIfEmpty(author, lastCommitAuthor));
  111. }
  112. }