forked from jenkinsci/analysis-model
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSonarQubeParser.java
327 lines (293 loc) · 9.83 KB
/
SonarQubeParser.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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package edu.hm.hafner.analysis.parser;
import java.io.IOException;
import java.io.Reader;
import org.json.JSONArray;
import org.json.JSONObject;
import org.json.JSONTokener;
import edu.hm.hafner.analysis.Issue;
import edu.hm.hafner.analysis.IssueBuilder;
import edu.hm.hafner.analysis.IssueParser;
import edu.hm.hafner.analysis.ParsingException;
import edu.hm.hafner.analysis.ReaderFactory;
import edu.hm.hafner.analysis.Report;
import edu.hm.hafner.analysis.Severity;
import edu.umd.cs.findbugs.annotations.Nullable;
/**
* Base class for SonarQube parsers.
*
* @author Carles Capdevila
*/
public abstract class SonarQubeParser extends IssueParser {
private static final long serialVersionUID = 1958805067002376816L;
//Arrays
/** The components array. */
private static final String COMPONENTS = "components";
/** The issues array. */
private static final String ISSUES = "issues";
//Issues attributes
/** issue.component attribute. */
private static final String ISSUE_COMPONENT = "component";
/** issue.message attribute. */
private static final String ISSUE_MESSAGE = "message";
/** issue.line attribute. */
private static final String ISSUE_SEVERITY = "severity";
/** issue.type attribute. */
private static final String ISSUE_TYPE = "type";
//Component attributes
/** component.key attribute. */
private static final String COMPONENT_KEY = "key";
/** component.path attribute. */
private static final String COMPONENT_PATH = "path";
// Severity values
/** severity value: BLOCKER. */
private static final String SEVERITY_BLOCKER = "BLOCKER";
/** severity value: CRITICAL. */
private static final String SEVERITY_CRITICAL = "CRITICAL";
// Severity MAJOR is omitted as it corresponds with default Severity: NORMAL
/** severity value: MINOR. */
private static final String SEVERITY_MINOR = "MINOR";
/** severity value: INFO. */
private static final String SEVERITY_INFO = "INFO";
/** Fixed category: SonarQube. */
private static final String CATEGORY_SONAR_QUBE = "SonarQube";
/** The components array. */
@Nullable
private transient JSONArray components = new JSONArray();
@Override
public boolean accepts(final ReaderFactory readerFactory) {
try (Reader reader = readerFactory.create()) {
return accepts((JSONObject) new JSONTokener(reader).nextValue());
}
catch (IOException ignored) {
return false;
}
}
/**
* Returns whether this parser accepts the specified JSON object as valid input.
*
* @param object
* the JSON object to analyse
*
* @return {@code true} if this parser accepts this object as valid input, {@code false} otherwise
*/
abstract boolean accepts(JSONObject object);
@Override
public Report parse(final ReaderFactory readerFactory) throws ParsingException {
try (Reader reader = readerFactory.create()) {
JSONObject jsonReport = (JSONObject) new JSONTokener(reader).nextValue();
extractComponents(jsonReport);
if (jsonReport.has(ISSUES)) {
return extractIssues(jsonReport.optJSONArray(ISSUES));
}
return new Report();
}
catch (IOException e) {
throw new ParsingException(e);
}
}
private Report extractIssues(final JSONArray elements) {
Report report = new Report();
for (Object object : elements) {
if (object instanceof JSONObject) {
JSONObject issue = (JSONObject) object;
if (filterIssue(issue)) {
report.add(createIssueFromJsonObject(issue));
}
}
}
return report;
}
/**
* Get the components part to get the file paths on each issue (the component objects contain the most concise
* path).
*
* @param jsonReport
* the report to get the components from
*/
private void extractComponents(final JSONObject jsonReport) {
if (jsonReport.has(COMPONENTS)) {
components = jsonReport.optJSONArray(COMPONENTS);
}
}
/**
* Decides whether or not to parse and add an issue.
*
* @param issue
* the issue to filter.
*
* @return {@code true} if the issue is to be parsed and added, otherwise {@code false}
*/
boolean filterIssue(final JSONObject issue) {
return true; // Parse all issues by default
}
private Issue createIssueFromJsonObject(final JSONObject issue) {
return new IssueBuilder()
.setFileName(parseFilename(issue))
.setLineStart(parseStart(issue))
.setLineEnd(parseEnd(issue))
.setType(parseType(issue))
.setCategory(CATEGORY_SONAR_QUBE)
.setMessage(parseMessage(issue))
.setSeverity(parsePriority(issue))
.build();
}
/**
* Parse function for filename.
*
* @param issue
* the object to parse.
*
* @return the filename.
*/
private String parseFilename(final JSONObject issue) {
// Get component
String componentKey = issue.optString(ISSUE_COMPONENT, null);
JSONObject component = findComponentByKey(componentKey);
if (component == null) {
String issueComponentKey = issue.optString(ISSUE_COMPONENT);
return issueComponentKey.substring(issueComponentKey.lastIndexOf(':'));
}
else {
// Get file path inside module
String filePath = component.optString(COMPONENT_PATH);
// Get module file path
String modulePath = getModulePath(component, issue);
return modulePath + filePath;
}
}
/**
* Extracts the module path from the specified JSON objects.
*
* @param component
* the component
* @param issue
* the issue
*
* @return the module path
*/
abstract String getModulePath(JSONObject component, JSONObject issue);
/**
* Default parse for start.
*
* @param issue
* the object to parse.
*
* @return the start.
*/
abstract int parseStart(JSONObject issue);
/**
* Default parse for end.
*
* @param issue
* the object to parse.
*
* @return the end.
*/
abstract int parseEnd(JSONObject issue);
/**
* Default parse for type.
*
* @param issue
* the object to parse.
*
* @return the type.
*/
private String parseType(final JSONObject issue) {
return issue.optString(ISSUE_TYPE, "");
}
/**
* Default parse for message.
*
* @param issue
* the object to parse.
*
* @return the message.
*/
private String parseMessage(final JSONObject issue) {
return issue.optString(ISSUE_MESSAGE, "No message.");
}
/**
* Default parse for priority.
*
* @param issue
* the object to parse.
*
* @return the priority.
*/
private Severity parsePriority(final JSONObject issue) {
String severity = issue.optString(ISSUE_SEVERITY, null);
return severityToPriority(severity);
}
//UTILITIES
/**
* Find the module path inside the corresponding component.
*
* @param moduleKeyObject
* the object which contains the component key.
* @param componentKey
* the component key.
*
* @return the module path.
*/
String parseModulePath(final JSONObject moduleKeyObject, final String componentKey) {
String modulePath = "";
if (moduleKeyObject.has(componentKey)) {
String moduleKey = moduleKeyObject.getString(componentKey);
JSONObject moduleComponent = findComponentByKey(moduleKey);
if (moduleComponent != null && moduleComponent.has(COMPONENT_PATH)) {
modulePath = moduleComponent.getString(COMPONENT_PATH) + "/";
}
}
return modulePath;
}
/**
* Find the component in the components array which contains this key.
*
* @param key
* the key of the desired component.
*
* @return the desired JSONObject component, or null if it hasn't been found.
*/
@Nullable
private JSONObject findComponentByKey(final String key) {
if (components != null && key != null) {
for (Object component : components) {
if (component instanceof JSONObject) {
JSONObject jsonComponent = (JSONObject) component;
if (key.equals(jsonComponent.optString(COMPONENT_KEY))) {
return (JSONObject) component;
}
}
}
}
return null;
}
/**
* Maps issue severity to warning priority.
*
* <br>
* <strong>HIGH:</strong> BLOCKER, CRITICAL
* <br>
* <strong>NORMAL:</strong> MAJOR
* <br>
* <strong>LOW:</strong> MINOR, INFO
*
* @param severity
* a String containing the SonarQube issue severity
*
* @return a priority object corresponding to the passed severity.
*/
private Severity severityToPriority(final String severity) {
Severity priority = Severity.WARNING_NORMAL;
// Severity MAJOR is omitted as it corresponds with default Severity: NORMAL
if (severity != null) {
if (SEVERITY_BLOCKER.equals(severity) || SEVERITY_CRITICAL.equals(severity)) {
priority = Severity.WARNING_HIGH;
}
else if (SEVERITY_MINOR.equals(severity) || SEVERITY_INFO.equals(severity)) {
priority = Severity.WARNING_LOW;
}
}
return priority;
}
}