3 * Copyright (C) 2009-2024 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.Comparator;
23 import java.util.Date;
24 import java.util.Optional;
25 import org.apache.commons.lang.StringUtils;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.sonar.ce.task.projectanalysis.analysis.AnalysisMetadataHolder;
29 import org.sonar.ce.task.projectanalysis.component.Component;
30 import org.sonar.ce.task.projectanalysis.scm.Changeset;
31 import org.sonar.ce.task.projectanalysis.scm.ScmInfo;
32 import org.sonar.ce.task.projectanalysis.scm.ScmInfoRepository;
33 import org.sonar.core.issue.DefaultIssue;
34 import org.sonar.core.issue.IssueChangeContext;
35 import org.sonar.db.issue.IssueDto;
36 import org.sonar.db.user.UserIdDto;
37 import org.sonar.server.issue.IssueFieldsSetter;
39 import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
40 import static org.sonar.core.issue.IssueChangeContext.issueChangeContextByScanBuilder;
43 * Detect the SCM author and SQ assignee.
45 * It relies on SCM information which comes from both the report and database.
47 public class IssueAssigner extends IssueVisitor {
49 private static final Logger LOGGER = LoggerFactory.getLogger(IssueAssigner.class);
51 private final ScmInfoRepository scmInfoRepository;
52 private final DefaultAssignee defaultAssignee;
53 private final IssueFieldsSetter issueUpdater;
54 private final ScmAccountToUser scmAccountToUser;
55 private final IssueChangeContext changeContext;
57 private String lastCommitAuthor = null;
58 private ScmInfo scmChangesets = null;
60 public IssueAssigner(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository, ScmAccountToUser scmAccountToUser, DefaultAssignee defaultAssignee,
61 IssueFieldsSetter issueUpdater) {
62 this.scmInfoRepository = scmInfoRepository;
63 this.scmAccountToUser = scmAccountToUser;
64 this.defaultAssignee = defaultAssignee;
65 this.issueUpdater = issueUpdater;
66 this.changeContext = issueChangeContextByScanBuilder(new Date(analysisMetadataHolder.getAnalysisDate())).build();
70 public void onIssue(Component component, DefaultIssue issue) {
71 if (issue.authorLogin() != null) {
74 loadScmChangesets(component);
75 Optional<String> scmAuthor = guessScmAuthor(issue, component);
77 if (scmAuthor.isPresent()) {
78 if (scmAuthor.get().length() <= IssueDto.AUTHOR_MAX_SIZE) {
79 issueUpdater.setNewAuthor(issue, scmAuthor.get(), changeContext);
81 LOGGER.debug("SCM account '{}' is too long to be stored as issue author", scmAuthor.get());
85 if (issue.assignee() == null) {
86 UserIdDto userId = scmAuthor.map(scmAccountToUser::getNullable).orElse(defaultAssignee.loadDefaultAssigneeUserId());
87 issueUpdater.setNewAssignee(issue, userId, changeContext);
91 private void loadScmChangesets(Component component) {
92 if (scmChangesets == null) {
93 Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
94 if (scmInfoOptional.isPresent()) {
95 scmChangesets = scmInfoOptional.get();
96 lastCommitAuthor = scmChangesets.getLatestChangeset().getAuthor();
102 public void afterComponent(Component component) {
103 lastCommitAuthor = null;
104 scmChangesets = null;
108 * Author of the latest change on the lines involved by the issue.
109 * If no authors are set on the lines, then the author of the latest change on the file
112 private Optional<String> guessScmAuthor(DefaultIssue issue, Component component) {
113 String author = null;
114 if (scmChangesets != null) {
115 author = IssueLocations.allLinesFor(issue, component.getUuid())
116 .filter(scmChangesets::hasChangesetForLine)
117 .mapToObj(scmChangesets::getChangesetForLine)
118 .filter(c -> StringUtils.isNotEmpty(c.getAuthor()))
119 .max(Comparator.comparingLong(Changeset::getDate))
120 .map(Changeset::getAuthor)
123 return Optional.ofNullable(defaultIfEmpty(author, lastCommitAuthor));