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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
|
/*
* 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.scanner.postjob;
import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.Iterables;
import javax.annotation.Nullable;
import org.sonar.api.batch.AnalysisMode;
import org.sonar.api.batch.fs.InputComponent;
import org.sonar.api.batch.postjob.PostJobContext;
import org.sonar.api.batch.postjob.issue.PostJobIssue;
import org.sonar.api.batch.rule.Severity;
import org.sonar.api.config.Settings;
import org.sonar.api.rule.RuleKey;
import org.sonar.scanner.issue.IssueCache;
import org.sonar.scanner.issue.tracking.TrackedIssue;
import org.sonar.scanner.scan.filesystem.InputComponentStore;
public class DefaultPostJobContext implements PostJobContext {
private final Settings settings;
private final IssueCache cache;
private final AnalysisMode analysisMode;
private InputComponentStore inputComponentCache;
public DefaultPostJobContext(Settings settings, IssueCache cache, InputComponentStore inputComponentCache, AnalysisMode analysisMode) {
this.settings = settings;
this.cache = cache;
this.inputComponentCache = inputComponentCache;
this.analysisMode = analysisMode;
}
@Override
public Settings settings() {
return settings;
}
@Override
public AnalysisMode analysisMode() {
return analysisMode;
}
@Override
public Iterable<PostJobIssue> issues() {
if (!analysisMode.isIssues()) {
throw new UnsupportedOperationException("Issues are only available to PostJobs in 'issues' mode.");
}
return Iterables.transform(Iterables.filter(cache.all(), new ResolvedPredicate(false)), new IssueTransformer());
}
@Override
public Iterable<PostJobIssue> resolvedIssues() {
if (!analysisMode.isIssues()) {
throw new UnsupportedOperationException("Resolved issues are only available to PostJobs in 'issues' mode.");
}
return Iterables.transform(Iterables.filter(cache.all(), new ResolvedPredicate(true)), new IssueTransformer());
}
private class DefaultIssueWrapper implements PostJobIssue {
private final TrackedIssue wrapped;
public DefaultIssueWrapper(TrackedIssue wrapped) {
this.wrapped = wrapped;
}
@Override
public String key() {
return wrapped.key();
}
@Override
public RuleKey ruleKey() {
return wrapped.getRuleKey();
}
@Override
public String componentKey() {
return wrapped.componentKey();
}
@Override
public InputComponent inputComponent() {
return inputComponentCache.getByKey(wrapped.componentKey());
}
@Override
public Integer line() {
return wrapped.startLine();
}
@Override
public String message() {
return wrapped.getMessage();
}
@Override
public Severity severity() {
return Severity.valueOf(wrapped.severity());
}
@Override
public boolean isNew() {
return wrapped.isNew();
}
}
private class IssueTransformer implements Function<TrackedIssue, PostJobIssue> {
@Override
public PostJobIssue apply(TrackedIssue input) {
return new DefaultIssueWrapper(input);
}
}
private static class ResolvedPredicate implements Predicate<TrackedIssue> {
private final boolean resolved;
private ResolvedPredicate(boolean resolved) {
this.resolved = resolved;
}
@Override
public boolean apply(@Nullable TrackedIssue issue) {
if (issue != null) {
return resolved ? issue.resolution() != null : issue.resolution() == null;
}
return false;
}
}
}
|