-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
IssueAssigner.java
121 lines (110 loc) · 4.57 KB
/
IssueAssigner.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.computation.task.projectanalysis.issue;
import com.google.common.base.Optional;
import com.google.common.base.Strings;
import java.util.Date;
import javax.annotation.CheckForNull;
import org.apache.commons.lang.StringUtils;
import org.sonar.api.utils.log.Logger;
import org.sonar.api.utils.log.Loggers;
import org.sonar.core.issue.DefaultIssue;
import org.sonar.core.issue.IssueChangeContext;
import org.sonar.server.computation.task.projectanalysis.analysis.AnalysisMetadataHolder;
import org.sonar.server.computation.task.projectanalysis.component.Component;
import org.sonar.server.computation.task.projectanalysis.scm.ScmInfo;
import org.sonar.server.computation.task.projectanalysis.scm.ScmInfoRepository;
import org.sonar.server.issue.IssueFieldsSetter;
import static org.apache.commons.lang.StringUtils.defaultIfEmpty;
import static org.sonar.core.issue.IssueChangeContext.createScan;
/**
* Detect the SCM author and SQ assignee.
* <p/>
* It relies on:
* <ul>
* <li>SCM information sent in the report for modified files</li>
* <li>sources lines stored in database for non-modified files</li>
* </ul>
*/
public class IssueAssigner extends IssueVisitor {
private static final Logger LOGGER = Loggers.get(IssueAssigner.class);
private final ScmInfoRepository scmInfoRepository;
private final DefaultAssignee defaultAssignee;
private final IssueFieldsSetter issueUpdater;
private final ScmAccountToUser scmAccountToUser;
private final IssueChangeContext changeContext;
private String lastCommitAuthor = null;
private ScmInfo scmChangesets = null;
public IssueAssigner(AnalysisMetadataHolder analysisMetadataHolder, ScmInfoRepository scmInfoRepository, ScmAccountToUser scmAccountToUser, DefaultAssignee defaultAssignee,
IssueFieldsSetter issueUpdater) {
this.scmInfoRepository = scmInfoRepository;
this.scmAccountToUser = scmAccountToUser;
this.defaultAssignee = defaultAssignee;
this.issueUpdater = issueUpdater;
this.changeContext = createScan(new Date(analysisMetadataHolder.getAnalysisDate()));
}
@Override
public void onIssue(Component component, DefaultIssue issue) {
boolean authorWasSet = false;
if (issue.authorLogin() == null) {
loadScmChangesets(component);
String scmAuthor = guessScmAuthor(issue);
if (!Strings.isNullOrEmpty(scmAuthor)) {
issueUpdater.setNewAuthor(issue, scmAuthor, changeContext);
authorWasSet = true;
}
}
if (authorWasSet && issue.assignee() == null) {
String assigneeLogin = StringUtils.defaultIfEmpty(scmAccountToUser.getNullable(issue.authorLogin()), defaultAssignee.getLogin());
issueUpdater.setNewAssignee(issue, assigneeLogin, changeContext);
}
}
private void loadScmChangesets(Component component) {
if (scmChangesets == null) {
Optional<ScmInfo> scmInfoOptional = scmInfoRepository.getScmInfo(component);
if (scmInfoOptional.isPresent()) {
scmChangesets = scmInfoOptional.get();
lastCommitAuthor = scmChangesets.getLatestChangeset().getAuthor();
}
}
}
@Override
public void afterComponent(Component component) {
lastCommitAuthor = null;
scmChangesets = null;
}
/**
* Get the SCM login of the last committer on the line. When line is zero,
* then get the last committer on the file.
*/
@CheckForNull
private String guessScmAuthor(DefaultIssue issue) {
Integer line = issue.line();
String author = null;
if (line != null && scmChangesets != null) {
if (scmChangesets.hasChangesetForLine(line)) {
author = scmChangesets.getChangesetForLine(line).getAuthor();
} else {
LOGGER.warn("No SCM info has been found for issue {}", issue);
}
}
return defaultIfEmpty(author, lastCommitAuthor);
}
}