This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
/
WarningsPublisher.java
384 lines (344 loc) · 15.5 KB
/
WarningsPublisher.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
package hudson.plugins.warnings;
import hudson.Launcher;
import hudson.matrix.MatrixAggregator;
import hudson.matrix.MatrixBuild;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.plugins.analysis.core.AnnotationsClassifier;
import hudson.plugins.analysis.core.FilesParser;
import hudson.plugins.analysis.core.HealthAwarePublisher;
import hudson.plugins.analysis.core.ParserResult;
import hudson.plugins.analysis.core.BuildResult;
import hudson.plugins.analysis.util.ModuleDetector;
import hudson.plugins.analysis.util.NullModuleDetector;
import hudson.plugins.analysis.util.PluginLogger;
import hudson.plugins.analysis.util.model.FileAnnotation;
import hudson.plugins.warnings.parser.FileWarningsParser;
import hudson.plugins.warnings.parser.ParserRegistry;
import hudson.plugins.warnings.parser.ParsingCanceledException;
import java.io.File;
import java.io.IOException;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang.StringUtils;
import org.kohsuke.stapler.DataBoundConstructor;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
/**
* Publishes the results of the warnings analysis (freestyle project type).
*
* @author Ulli Hafner
*/
// CHECKSTYLE:COUPLING-OFF
public class WarningsPublisher extends HealthAwarePublisher {
private static final String PLUGIN_NAME = "WARNINGS";
private static final long serialVersionUID = -5936973521277401764L;
/** Ant file-set pattern of files to include to report. */
private final String includePattern;
/** Ant file-set pattern of files to exclude from report. */
private final String excludePattern;
/** File pattern and parser configurations. @since 3.19 */
@edu.umd.cs.findbugs.annotations.SuppressWarnings("SE")
private final List<ParserConfiguration> parserConfigurations = Lists.newArrayList();
/** Parser configurations of the console. @since 4.6 */
@edu.umd.cs.findbugs.annotations.SuppressWarnings("SE")
private List<ConsoleParser> consoleParsers = Lists.newArrayList();
/**
* Creates a new instance of <code>WarningPublisher</code>.
*
* @param healthy
* Report health as 100% when the number of annotations is less
* than this value
* @param unHealthy
* Report health as 0% when the number of annotations is greater
* than this value
* @param thresholdLimit
* determines which warning priorities should be considered when
* evaluating the build stability and health
* @param defaultEncoding
* the default encoding to be used when reading and parsing files
* @param useDeltaValues
* determines whether the absolute annotations delta or the
* actual annotations set difference should be used to evaluate
* the build stability
* @param unstableTotalAll
* annotation threshold
* @param unstableTotalHigh
* annotation threshold
* @param unstableTotalNormal
* annotation threshold
* @param unstableTotalLow
* annotation threshold
* @param unstableNewAll
* annotation threshold
* @param unstableNewHigh
* annotation threshold
* @param unstableNewNormal
* annotation threshold
* @param unstableNewLow
* annotation threshold
* @param failedTotalAll
* annotation threshold
* @param failedTotalHigh
* annotation threshold
* @param failedTotalNormal
* annotation threshold
* @param failedTotalLow
* annotation threshold
* @param failedNewAll
* annotation threshold
* @param failedNewHigh
* annotation threshold
* @param failedNewNormal
* annotation threshold
* @param failedNewLow
* annotation threshold
* @param canRunOnFailed
* determines whether the plug-in can run for failed builds, too
* @param shouldDetectModules
* determines whether module names should be derived from Maven POM or Ant build files
* @param includePattern
* Ant file-set pattern of files to include in report
* @param excludePattern
* Ant file-set pattern of files to exclude from report
* @param canResolveRelativePaths
* determines whether relative paths in warnings should be
* resolved using a time expensive operation that scans the whole
* workspace for matching files.
*/
// CHECKSTYLE:OFF
@SuppressWarnings("PMD.ExcessiveParameterList")
@DataBoundConstructor
public WarningsPublisher(final String healthy, final String unHealthy, final String thresholdLimit,
final String defaultEncoding, final boolean useDeltaValues,
final String unstableTotalAll, final String unstableTotalHigh, final String unstableTotalNormal, final String unstableTotalLow,
final String unstableNewAll, final String unstableNewHigh, final String unstableNewNormal, final String unstableNewLow,
final String failedTotalAll, final String failedTotalHigh, final String failedTotalNormal, final String failedTotalLow,
final String failedNewAll, final String failedNewHigh, final String failedNewNormal, final String failedNewLow,
final boolean canRunOnFailed, final boolean shouldDetectModules, final boolean canComputeNew,
final String includePattern, final String excludePattern, final boolean canResolveRelativePaths,
final List<ParserConfiguration> parserConfigurations, final List<ConsoleParser> consoleParsers) {
super(healthy, unHealthy, thresholdLimit, defaultEncoding, useDeltaValues,
unstableTotalAll, unstableTotalHigh, unstableTotalNormal, unstableTotalLow,
unstableNewAll, unstableNewHigh, unstableNewNormal, unstableNewLow,
failedTotalAll, failedTotalHigh, failedTotalNormal, failedTotalLow,
failedNewAll, failedNewHigh, failedNewNormal, failedNewLow,
canRunOnFailed, shouldDetectModules, canComputeNew, canResolveRelativePaths, PLUGIN_NAME);
this.includePattern = StringUtils.stripToNull(includePattern);
this.excludePattern = StringUtils.stripToNull(excludePattern);
if (consoleParsers != null) {
this.consoleParsers.addAll(consoleParsers);
}
if (parserConfigurations != null) {
this.parserConfigurations.addAll(parserConfigurations);
}
}
// CHECKSTYLE:ON
/**
* Returns the names of the configured parsers for the console log.
*
* @return the parser names
*/
public ConsoleParser[] getConsoleParsers() {
return ConsoleParser.filterExisting(consoleParsers);
}
/**
* Returns the parserConfigurations.
*
* @return the parserConfigurations
*/
public ParserConfiguration[] getParserConfigurations() {
return ParserConfiguration.filterExisting(parserConfigurations);
}
/**
* Upgrade for release 4.5 or older.
*
* @return this
*/
@Override
protected Object readResolve() {
super.readResolve();
if (consoleParsers == null) {
consoleParsers = Lists.newArrayList();
for (String parser : consoleLogParsers) {
consoleParsers.add(new ConsoleParser(parser));
}
}
return this;
}
/**
* Returns the Ant file-set pattern of files to include in report.
*
* @return Ant file-set pattern of files to include in report
*/
public String getIncludePattern() {
return includePattern;
}
/**
* Returns the Ant file-set pattern of files to exclude from report.
*
* @return Ant file-set pattern of files to exclude from report
*/
public String getExcludePattern() {
return excludePattern;
}
@Override
public Action getProjectAction(final AbstractProject<?, ?> project) {
throw new IllegalStateException("Not available since release 4.0.");
}
@Override
public Collection<? extends Action> getProjectActions(final AbstractProject<?, ?> project) {
List<Action> actions = Lists.newArrayList();
for (String parserName : getParsers()) {
actions.add(new WarningsProjectAction(project, parserName));
}
return actions;
}
private Set<String> getParsers() {
Set<String> parsers = Sets.newHashSet();
for (ConsoleParser configuration : getConsoleParsers()) {
parsers.add(configuration.getParserName());
}
for (ParserConfiguration configuration : getParserConfigurations()) {
parsers.add(configuration.getParserName());
}
return parsers;
}
@Override
public BuildResult perform(final AbstractBuild<?, ?> build, final PluginLogger logger)
throws InterruptedException, IOException {
try {
if (!hasConsoleParsers() && !hasFileParsers()) {
throw new IOException("Error: No warning parsers defined in the job configuration.");
}
List<ParserResult> fileResults = parseFiles(build, logger);
List<ParserResult> consoleResults = parseConsoleLog(build, logger);
ParserResult totals = new ParserResult();
add(totals, consoleResults);
add(totals, fileResults);
return new WarningsResult(build, new WarningsBuildHistory(build, null), totals, getDefaultEncoding(), null);
}
catch (ParsingCanceledException exception) {
throw createInterruptedException();
}
}
@Override
protected void updateBuildResult(final BuildResult result, final PluginLogger logger) {
for (WarningsResultAction action : result.getOwner().getActions(WarningsResultAction.class)) {
action.getResult().evaluateStatus(getThresholds(), getUseDeltaValues(), canComputeNew(), logger,
action.getUrlName());
}
}
private boolean hasFileParsers() {
return getParserConfigurations().length > 0;
}
private boolean hasConsoleParsers() {
return getConsoleParsers().length > 0;
}
private void add(final ParserResult totals, final List<ParserResult> results) {
for (ParserResult result : results) {
totals.addProject(result);
}
}
private InterruptedException createInterruptedException() {
return new InterruptedException("Canceling parsing since build has been aborted.");
}
private void returnIfCanceled() throws InterruptedException {
if (Thread.interrupted()) {
throw createInterruptedException();
}
}
private List<ParserResult> parseConsoleLog(final AbstractBuild<?, ?> build,
final PluginLogger logger) throws IOException, InterruptedException {
List<ParserResult> results = Lists.newArrayList();
for (ConsoleParser parser : getConsoleParsers()) {
String parserName = parser.getParserName();
logger.log("Parsing warnings in console log with parser " + parserName);
Collection<FileAnnotation> warnings = new ParserRegistry(
ParserRegistry.getParsers(parserName),
getDefaultEncoding(), getIncludePattern(), getExcludePattern()).parse(build
.getLogFile());
if (!build.getWorkspace().isRemote()) {
guessModuleNames(build, warnings);
}
ParserResult project;
if (canResolveRelativePaths()) {
project = new ParserResult(build.getWorkspace());
}
else {
project = new ParserResult();
}
project = new ParserResult(build.getWorkspace());
project.addAnnotations(warnings);
results.add(annotate(build, project, parserName));
}
return results;
}
private void guessModuleNames(final AbstractBuild<?, ?> build, final Collection<FileAnnotation> warnings) {
String workspace = build.getWorkspace().getRemote();
ModuleDetector detector = createModuleDetector(workspace);
for (FileAnnotation annotation : warnings) {
String module = detector.guessModuleName(annotation.getFileName());
annotation.setModuleName(module);
}
}
private List<ParserResult> parseFiles(final AbstractBuild<?, ?> build, final PluginLogger logger)
throws IOException, InterruptedException {
List<ParserResult> results = Lists.newArrayList();
for (ParserConfiguration configuration : getParserConfigurations()) {
String filePattern = configuration.getPattern();
String parserName = configuration.getParserName();
logger.log("Parsing warnings in files '" + filePattern + "' with parser " + parserName);
FilesParser parser = new FilesParser(PLUGIN_NAME, filePattern,
new FileWarningsParser(ParserRegistry.getParsers(parserName), getDefaultEncoding(), getIncludePattern(), getExcludePattern()),
shouldDetectModules(), isMavenBuild(build), canResolveRelativePaths());
ParserResult project = build.getWorkspace().act(parser);
logger.logLines(project.getLogMessages());
returnIfCanceled();
results.add(annotate(build, project, configuration.getParserName()));
}
return results;
}
private ParserResult annotate(final AbstractBuild<?, ?> build, final ParserResult input, final String parserName)
throws IOException, InterruptedException {
ParserResult output = build.getWorkspace().act(
new AnnotationsClassifier(input, getDefaultEncoding()));
for (FileAnnotation annotation : output.getAnnotations()) {
annotation.setPathName(build.getWorkspace().getRemote());
}
WarningsResult result = new WarningsResult(build, new WarningsBuildHistory(build, parserName),
output, getDefaultEncoding(), parserName);
build.getActions().add(new WarningsResultAction(build, this, result, parserName));
return output;
}
private ModuleDetector createModuleDetector(final String workspace) {
if (shouldDetectModules()) {
return new ModuleDetector(new File(workspace));
}
else {
return new NullModuleDetector();
}
}
@Override
public WarningsDescriptor getDescriptor() {
return (WarningsDescriptor)super.getDescriptor();
}
/** {@inheritDoc} */
public MatrixAggregator createAggregator(final MatrixBuild build, final Launcher launcher, final BuildListener listener) {
return new WarningsAnnotationsAggregator(build, launcher, listener, this, getDefaultEncoding());
}
/** Name of parsers to use for scanning the logs. */
@SuppressWarnings({"unused", "PMD"})
private transient Set<String> parserNames;
/** Determines whether the console should be ignored. */
@SuppressWarnings({"unused", "PMD"})
private transient boolean ignoreConsole;
/** Ant file-set pattern of files to work with. */
@SuppressWarnings({"unused", "PMD"})
private transient String pattern;
/** Parser to scan the console log. @since 3.19 */
private transient Set<String> consoleLogParsers;
}