-
Notifications
You must be signed in to change notification settings - Fork 272
/
PublishIssuesStep.java
701 lines (628 loc) · 23.4 KB
/
PublishIssuesStep.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
package io.jenkins.plugins.analysis.core.steps;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import org.apache.commons.lang3.StringUtils;
import org.eclipse.collections.impl.factory.Sets;
import edu.hm.hafner.analysis.Severity;
import edu.umd.cs.findbugs.annotations.NonNull;
import edu.umd.cs.findbugs.annotations.Nullable;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.jenkinsci.plugins.workflow.actions.WarningAction;
import org.jenkinsci.plugins.workflow.graph.FlowNode;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import hudson.Extension;
import hudson.model.Action;
import hudson.model.Job;
import hudson.model.Result;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.util.ComboBoxModel;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import io.jenkins.plugins.analysis.core.model.LabelProviderFactory;
import io.jenkins.plugins.analysis.core.model.ResultAction;
import io.jenkins.plugins.analysis.core.model.StaticAnalysisLabelProvider;
import io.jenkins.plugins.analysis.core.util.HealthDescriptor;
import io.jenkins.plugins.analysis.core.util.LogHandler;
import io.jenkins.plugins.analysis.core.util.ModelValidation;
import io.jenkins.plugins.analysis.core.util.QualityGate;
import io.jenkins.plugins.analysis.core.util.QualityGate.QualityGateResult;
import io.jenkins.plugins.analysis.core.util.QualityGate.QualityGateType;
import io.jenkins.plugins.analysis.core.util.QualityGateEvaluator;
import io.jenkins.plugins.analysis.core.util.QualityGateStatus;
import io.jenkins.plugins.analysis.core.util.QualityGateStatusHandler;
/**
* Publish issues created by a static analysis build. The recorded issues are stored as a {@link ResultAction} in the
* associated Jenkins build. If the issues report has a unique ID, then the created action will use this ID as well.
* Otherwise a default ID is used to publish the results. In any case, the computed ID can be overwritten by specifying
* an ID as step parameter.
*/
@SuppressWarnings({"InstanceVariableMayNotBeInitialized", "PMD.ExcessiveImports", "PMD.ExcessivePublicCount", "PMD.DataClass"})
public class PublishIssuesStep extends Step {
private final List<AnnotatedReport> reports;
private String sourceCodeEncoding = StringUtils.EMPTY;
private boolean ignoreQualityGate = false; // by default, a successful quality gate is mandatory
private boolean ignoreFailedBuilds = true; // by default, failed builds are ignored
private String referenceJobName = StringUtils.EMPTY;
private int healthy;
private int unhealthy;
private Severity minimumSeverity = Severity.WARNING_LOW;
private List<QualityGate> qualityGates = new ArrayList<>();
private String id = StringUtils.EMPTY;
private String name = StringUtils.EMPTY;
/**
* Creates a new instance of {@link PublishIssuesStep}.
*
* @param issues
* the reports to publish as {@link Action} in the {@link Job}.
*
* @throws IllegalArgumentException
* if the array of issues is {@code null} or empty
*/
@DataBoundConstructor
public PublishIssuesStep(@Nullable final List<AnnotatedReport> issues) {
super();
if (issues == null) {
reports = new ArrayList<>();
}
else {
reports = new ArrayList<>(issues);
}
}
public List<AnnotatedReport> getIssues() {
return new ArrayList<>(reports);
}
/**
* Defines the ID of the results. The ID is used as URL of the results and as name in UI elements. If no ID is
* given, then the ID of the associated result object is used.
*
* @param id
* the ID of the results
*/
@DataBoundSetter
public void setId(final String id) {
this.id = id;
}
public String getId() {
return id;
}
/**
* Defines the name of the results. The name is used for all labels in the UI. If no name is given, then the name of
* the associated {@link StaticAnalysisLabelProvider} is used.
*
* @param name
* the name of the results
*/
@DataBoundSetter
public void setName(final String name) {
this.name = name;
}
public String getName() {
return name;
}
/**
* If {@code true}, then the result of the quality gate is ignored when selecting a reference build. This option is
* disabled by default so a failing quality gate will be passed from build to build until the original reason for
* the failure has been resolved.
*
* @param ignoreQualityGate
* if {@code true} then the result of the quality gate is ignored, otherwise only build with a successful
* quality gate are selected
*/
@DataBoundSetter
@SuppressWarnings("unused") // Used by Stapler
public void setIgnoreQualityGate(final boolean ignoreQualityGate) {
this.ignoreQualityGate = ignoreQualityGate;
}
@SuppressWarnings({"PMD.BooleanGetMethodName", "WeakerAccess"})
public boolean getIgnoreQualityGate() {
return ignoreQualityGate;
}
/**
* If {@code true}, then only successful or unstable reference builds will be considered. This option is enabled by
* default, since analysis results might be inaccurate if the build failed. If {@code false}, every build that
* contains a static analysis result is considered, even if the build failed.
*
* @param ignoreFailedBuilds
* if {@code true} then a stable build is used as reference
*/
@DataBoundSetter
@SuppressWarnings("unused") // Used by Stapler
public void setIgnoreFailedBuilds(final boolean ignoreFailedBuilds) {
this.ignoreFailedBuilds = ignoreFailedBuilds;
}
@SuppressWarnings({"PMD.BooleanGetMethodName", "WeakerAccess"})
public boolean getIgnoreFailedBuilds() {
return ignoreFailedBuilds;
}
/**
* Sets the reference job to get the results for the issue difference computation.
*
* @param referenceJobName
* the name of reference job
*/
@DataBoundSetter
@SuppressWarnings("unused") // Used by Stapler
public void setReferenceJobName(final String referenceJobName) {
this.referenceJobName = referenceJobName;
}
@SuppressWarnings("WeakerAccess") // Required by Stapler
public String getReferenceJobName() {
return referenceJobName;
}
@Nullable
public String getSourceCodeEncoding() {
return sourceCodeEncoding;
}
/**
* Sets the encoding to use to read source files.
*
* @param sourceCodeEncoding
* the encoding, e.g. "ISO-8859-1"
*/
@DataBoundSetter
public void setSourceCodeEncoding(final String sourceCodeEncoding) {
this.sourceCodeEncoding = sourceCodeEncoding;
}
public int getHealthy() {
return healthy;
}
/**
* Sets the healthy threshold, i.e. the number of issues when health is reported as 100%.
*
* @param healthy
* the number of issues when health is reported as 100%
*/
@DataBoundSetter
public void setHealthy(final int healthy) {
this.healthy = healthy;
}
public int getUnhealthy() {
return unhealthy;
}
/**
* Sets the healthy threshold, i.e. the number of issues when health is reported as 0%.
*
* @param unhealthy
* the number of issues when health is reported as 0%
*/
@DataBoundSetter
public void setUnhealthy(final int unhealthy) {
this.unhealthy = unhealthy;
}
@Nullable
@SuppressWarnings("unused") // Used by Stapler
public String getMinimumSeverity() {
return minimumSeverity.getName();
}
@Nullable
@SuppressWarnings("WeakerAccess") // Required by Stapler
public Severity getMinimumSeverityAsSeverity() {
return minimumSeverity;
}
/**
* Sets the minimum severity to consider when computing the health report. Issues with a severity less than this
* value will be ignored.
*
* @param minimumSeverity
* the severity to consider
*/
@DataBoundSetter
@SuppressWarnings("unused") // Used by Stapler
public void setMinimumSeverity(final String minimumSeverity) {
this.minimumSeverity = Severity.valueOf(minimumSeverity, Severity.WARNING_LOW);
}
/**
* Defines the optional list of quality gates.
*
* @param qualityGates the quality gates
*/
@DataBoundSetter
@SuppressWarnings("unused") // Used by Stapler
public void setQualityGates(final List<QualityGate> qualityGates) {
this.qualityGates = qualityGates;
}
@SuppressWarnings("WeakerAccess") // Required by Stapler
public List<QualityGate> getQualityGates() {
return qualityGates;
}
/**
* Appends the specified quality gates to the end of the list of quality gates.
*
* @param size
* the minimum number of issues that fails the quality gate
* @param type
* the type of the quality gate
* @param result
* determines whether the quality gate is a warning or failure
*/
private void addQualityGate(final int size, final QualityGateType type, final QualityGateResult result) {
qualityGates.add(new QualityGate(size, type, result));
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType,
* QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableTotalAll(final int size) {
addQualityGate(size, QualityGateType.TOTAL, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableTotalHigh(final int size) {
addQualityGate(size, QualityGateType.TOTAL_HIGH, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableNewAll(final int size) {
addQualityGate(size, QualityGateType.NEW, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableTotalNormal(final int size) {
addQualityGate(size, QualityGateType.TOTAL_NORMAL, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableTotalLow(final int size) {
addQualityGate(size, QualityGateType.TOTAL_LOW, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableNewHigh(final int size) {
addQualityGate(size, QualityGateType.NEW_HIGH, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableNewNormal(final int size) {
addQualityGate(size, QualityGateType.NEW_NORMAL, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setUnstableNewLow(final int size) {
addQualityGate(size, QualityGateType.NEW_LOW, QualityGateResult.UNSTABLE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedTotalAll(final int size) {
addQualityGate(size, QualityGateType.TOTAL, QualityGateResult.FAILURE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedTotalHigh(final int size) {
addQualityGate(size, QualityGateType.TOTAL_HIGH, QualityGateResult.FAILURE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedTotalNormal(final int size) {
addQualityGate(size, QualityGateType.TOTAL_NORMAL, QualityGateResult.FAILURE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedTotalLow(final int size) {
addQualityGate(size, QualityGateType.TOTAL_LOW, QualityGateResult.FAILURE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedNewAll(final int size) {
addQualityGate(size, QualityGateType.NEW, QualityGateResult.FAILURE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedNewHigh(final int size) {
addQualityGate(size, QualityGateType.NEW_HIGH, QualityGateResult.FAILURE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedNewNormal(final int size) {
addQualityGate(size, QualityGateType.NEW_NORMAL, QualityGateResult.FAILURE);
}
/**
* Sets the quality gate.
*
* @param size
* number of issues
*
* @deprecated replaced by {@link IssuesRecorder#addQualityGate(int, QualityGate.QualityGateType, QualityGate.QualityGateResult)}
*/
@Deprecated
@DataBoundSetter
public void setFailedNewLow(final int size) {
addQualityGate(size, QualityGateType.NEW_LOW, QualityGateResult.FAILURE);
}
@Override
public StepExecution start(final StepContext stepContext) {
return new Execution(stepContext, this);
}
/**
* Actually performs the execution of the associated step.
*/
static class Execution extends AnalysisExecution<ResultAction> {
private static final long serialVersionUID = 6438321240776419897L;
private final HealthDescriptor healthDescriptor;
private final boolean ignoreQualityGate;
private final boolean ignoreFailedBuilds;
private final String sourceCodeEncoding;
private final List<QualityGate> qualityGates;
private final String id;
private final String name;
private final String referenceJobName;
private final List<AnnotatedReport> reports;
/**
* Creates a new instance of the step execution object.
*
* @param context
* context for this step
* @param step
* the actual step to execute
*/
@SuppressWarnings("PMD.AvoidLiteralsInIfCondition")
Execution(@NonNull final StepContext context, final PublishIssuesStep step) {
super(context);
if (step.reports.isEmpty()) {
throw new IllegalArgumentException(
"No reports provided in publish issues step, parameter 'issues' must be set!");
}
ignoreQualityGate = step.getIgnoreQualityGate();
ignoreFailedBuilds = step.getIgnoreFailedBuilds();
referenceJobName = step.getReferenceJobName();
sourceCodeEncoding = step.getSourceCodeEncoding();
healthDescriptor = new HealthDescriptor(step.getHealthy(), step.getUnhealthy(),
step.getMinimumSeverityAsSeverity());
qualityGates = new ArrayList<>(step.getQualityGates());
name = StringUtils.defaultString(step.getName());
id = step.getId();
reports = step.reports;
}
@Override
protected ResultAction run() throws IOException, InterruptedException, IllegalStateException {
QualityGateEvaluator qualityGate = new QualityGateEvaluator();
qualityGate.addAll(qualityGates);
AnnotatedReport report = new AnnotatedReport(StringUtils.defaultIfEmpty(id, reports.get(0).getId()));
if (reports.size() > 1) {
report.logInfo("Aggregating reports of:");
LabelProviderFactory factory = new LabelProviderFactory();
for (AnnotatedReport subReport : reports) {
StaticAnalysisLabelProvider labelProvider = factory.create(subReport.getId());
report.logInfo("-> %s", labelProvider.getToolTip(subReport.size()));
}
}
report.addAll(reports);
QualityGateStatusHandler statusHandler = new QualityGateStatusHandler.PipelineStatusHandler(getRun(),
getContext().get(FlowNode.class));
IssuesPublisher publisher = new IssuesPublisher(getRun(), report, healthDescriptor, qualityGate,
name, referenceJobName, ignoreQualityGate, ignoreFailedBuilds,
getCharset(sourceCodeEncoding), getLogger(report), statusHandler);
ResultAction resultAction = publisher.attachAction();
return resultAction;
}
private LogHandler getLogger(final AnnotatedReport report) throws InterruptedException {
String toolName = new LabelProviderFactory().create(report.getId(), name).getName();
return new LogHandler(getTaskListener(), toolName, report.getReport());
}
}
/**
* Descriptor for this step: defines the context and the UI labels.
*/
@Extension
public static class Descriptor extends StepDescriptor {
private final ModelValidation model = new ModelValidation();
@Override
public Set<Class<?>> getRequiredContext() {
return Sets.immutable.of(FlowNode.class, Run.class, TaskListener.class).castToSet();
}
@Override
public String getFunctionName() {
return "publishIssues";
}
@NonNull
@Override
public String getDisplayName() {
return Messages.PublishIssues_DisplayName();
}
/**
* Returns a model with all available charsets.
*
* @return a model with all available charsets
*/
@SuppressWarnings("unused") // Used by view
public ComboBoxModel doFillSourceCodeEncodingItems() {
return model.getAllCharsets();
}
/**
* Returns a model with all available severity filters.
*
* @return a model with all available severity filters
*/
@SuppressWarnings("unused") // Used by view
public ListBoxModel doFillMinimumSeverityItems() {
return model.getAllSeverityFilters();
}
/**
* Returns the model with the possible reference jobs.
*
* @return the model with the possible reference jobs
*/
@SuppressWarnings("unused") // Used by view
public ComboBoxModel doFillReferenceJobNameItems() {
return model.getAllJobs();
}
/**
* Performs on-the-fly validation of the reference job.
*
* @param referenceJobName
* the reference job
*
* @return the validation result
*/
@SuppressWarnings("unused") // Used by view
public FormValidation doCheckReferenceJobName(@QueryParameter final String referenceJobName) {
return model.validateJob(referenceJobName);
}
/**
* Performs on-the-fly validation on the character encoding.
*
* @param sourceCodeEncoding
* the character encoding
*
* @return the validation result
*/
@SuppressWarnings("unused") // Used by view
public FormValidation doCheckSourceCodeEncoding(@QueryParameter final String sourceCodeEncoding) {
return model.validateCharset(sourceCodeEncoding);
}
/**
* Performs on-the-fly validation of the health report thresholds.
*
* @param healthy
* the healthy threshold
* @param unhealthy
* the unhealthy threshold
*
* @return the validation result
*/
@SuppressWarnings("unused") // Used by view
public FormValidation doCheckHealthy(@QueryParameter final int healthy, @QueryParameter final int unhealthy) {
return model.validateHealthy(healthy, unhealthy);
}
/**
* Performs on-the-fly validation of the health report thresholds.
*
* @param healthy
* the healthy threshold
* @param unhealthy
* the unhealthy threshold
*
* @return the validation result
*/
@SuppressWarnings("unused") // Used by view
public FormValidation doCheckUnhealthy(@QueryParameter final int healthy, @QueryParameter final int unhealthy) {
return model.validateUnhealthy(healthy, unhealthy);
}
}
}