3 * Copyright (C) 2009-2017 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.server.computation.task.projectanalysis.issue;
22 import com.google.common.base.Optional;
23 import com.google.common.base.Strings;
24 import java.util.Date;
25 import javax.annotation.CheckForNull;
26 import org.apache.commons.lang.StringUtils;
27 import org.sonar.api.utils.log.Logger;
28 import org.sonar.api.utils.log.Loggers;
29 import org.sonar.core.issue.DefaultIssue;
30 import org.sonar.core.issue.IssueChangeContext;
31 import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
32 import org.sonar.server.computation.task.projectanalysis.component.Component;
33 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfo;
34 import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepository;
35 import org.sonar.server.issue.IssueFieldsSetter;
37 import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
38 import static org.sonar.core.issue.IssueChangeContext.createScan;
41 * Detect the SCM author and SQ assignee.
45 * <li>SCM information sent in the report for modified files</li>
46 * <li>sources lines stored in database for non-modified files</li>
49 public class IssueAssigner extends IssueVisitor {
51 private static final Logger LOGGER = Loggers.get(IssueAssigner.class);
53 private final ScmInfoRepository scmInfoRepository;
54 private final DefaultAssignee defaultAssignee;
55 private final IssueFieldsSetter issueUpdater;
56 private final ScmAccountToUser scmAccountToUser;
57 private final IssueChangeContext changeContext;
59 private String lastCommitAuthor = null;
60 private ScmInfo scmChangesets = null;
62 public IssueAssigner(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository, ScmAccountToUser scmAccountToUser, DefaultAssignee defaultAssignee,
63 IssueFieldsSetter issueUpdater) {
64 this.scmInfoRepository = scmInfoRepository;
65 this.scmAccountToUser = scmAccountToUser;
66 this.defaultAssignee = defaultAssignee;
67 this.issueUpdater = issueUpdater;
68 this.changeContext = createScan(new Date(analysisMetadataHolder.getAnalysisDate()));
72 public void onIssue(Component component, DefaultIssue issue) {
73 if (issue.authorLogin() == null) {
74 loadScmChangesets(component);
75 String scmAuthor = guessScmAuthor(issue);
77 if (!Strings.isNullOrEmpty(scmAuthor)) {
78 issueUpdater.setNewAuthor(issue, scmAuthor, changeContext);
81 if (issue.assignee() == null) {
82 String author = issue.authorLogin() == null ? null : scmAccountToUser.getNullable(issue.authorLogin());
83 String assigneeLogin = StringUtils.defaultIfEmpty(author, defaultAssignee.loadDefaultAssigneeLogin());
85 issueUpdater.setNewAssignee(issue, assigneeLogin, changeContext);
90 private void loadScmChangesets(Component component) {
91 if (scmChangesets == null) {
92 Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
93 if (scmInfoOptional.isPresent()) {
94 scmChangesets = scmInfoOptional.get();
95 lastCommitAuthor = scmChangesets.getLatestChangeset().getAuthor();
101 public void afterComponent(Component component) {
102 lastCommitAuthor = null;
103 scmChangesets = null;
107 * Get the SCM login of the last committer on the line. When line is zero,
108 * then get the last committer on the file.
111 private String guessScmAuthor(DefaultIssue issue) {
112 Integer line = issue.line();
113 String author = null;
114 if (line != null && scmChangesets != null) {
115 if (scmChangesets.hasChangesetForLine(line)) {
116 author = scmChangesets.getChangesetForLine(line).getAuthor();
118 LOGGER.warn("No SCM info has been found for issue {}", issue);
121 return defaultIfEmpty(author, lastCommitAuthor);