/
ViolationsReport.java
611 lines (559 loc) · 17.3 KB
/
ViolationsReport.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
package hudson.plugins.violations;
import static java.util.logging.Level.SEVERE;
import hudson.model.HealthReport;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Project;
/** cannot be used in a slave
import hudson.maven.MavenModule;
/**/
import hudson.plugins.violations.hudson.AbstractViolationsBuildAction;
/** cannot be used in a slave
import hudson.plugins.violations.hudson.maven.*;
/**/
import hudson.plugins.violations.model.BuildModel;
import hudson.plugins.violations.model.FileModel;
import hudson.plugins.violations.model.Suppression;
import hudson.plugins.violations.parse.BuildModelParser;
import hudson.plugins.violations.parse.ParseXML;
import hudson.plugins.violations.render.FileModelProxy;
import hudson.plugins.violations.render.NoViolationsFile;
import hudson.plugins.violations.util.HelpHudson;
import hudson.plugins.violations.util.RecurDynamic;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
/**
* This contains the report for the violations of a particular build.
*/
public class ViolationsReport implements Serializable {
private static final Logger LOG = Logger.getLogger(ViolationsReport.class.getName());
private AbstractBuild<?, ?> build;
private ViolationsConfig config;
private final Map<String, Integer> violations = new TreeMap<String, Integer>();
private final Map<String, TypeSummary> typeSummaries = new TreeMap<String, TypeSummary>();
private transient WeakReference<BuildModel> modelReference;
/**
* Set the build.
*
* @param build
* the current build.
*/
public void setBuild(AbstractBuild<?, ?> build) {
this.build = build;
}
/**
* Get the build.
*
* @return the build.
*/
public AbstractBuild<?, ?> getBuild() {
return build;
}
/**
* Set the config.
*
* @param config
* the config.
*/
public void setConfig(ViolationsConfig config) {
this.config = config;
}
/**
* Get the config.
*
* @return the config.
*/
public ViolationsConfig getConfig() {
return config;
}
/**
* Get the violation counts for the build.
*
* @return a map of type to count.
*/
public Map<String, Integer> getViolations() {
return violations;
}
/**
* Get the overall health for the build.
*
* @return the health report, null if there are no counts.
*/
public HealthReport getBuildHealth() {
List<HealthReport> reports = getBuildHealths();
HealthReport ret = null;
for (HealthReport report : reports) {
ret = HealthReport.min(ret, report);
}
return ret;
}
/**
* Get a health report for each type.
*
* @return a list of health reports.
*/
public List<HealthReport> getBuildHealths() {
List<HealthReport> ret = new ArrayList<HealthReport>();
for (String type : config.getTypeConfigs().keySet()) {
HealthReport health = getHealthReportFor(type);
if (health != null) {
ret.add(health);
}
}
return ret;
}
/**
* Get the health for a particulat type.
*
* @param type
* the type to get the health for.
* @return the health report.
*/
public HealthReport getHealthReportFor(String type) {
Integer count = violations.get(type);
if (count == null || config.getTypeConfigs() == null) {
return null;
}
int h = config.getTypeConfigs().get(type).getHealthFor(count);
if (h < 0) {
return new HealthReport(0, Messages._ViolationsReport_NoReport(type));
} else {
return new HealthReport(h, Messages._ViolationsReport_ViolationsCount(type, count));
}
}
/**
* Get the detailed model for the build. This is lazily build from an xml
* created during publisher action.
*
* @return the build model.
*/
public BuildModel getModel() {
BuildModel model = null;
if (modelReference != null) {
model = modelReference.get();
if (model != null) {
return model;
}
}
File xmlFile = new File(build.getRootDir(), MagicNames.VIOLATIONS + "/" + MagicNames.VIOLATIONS + ".xml");
try {
model = new BuildModel(xmlFile);
ParseXML.parse(xmlFile, new BuildModelParser().buildModel(model));
} catch (Exception ex) {
LOG.log(Level.WARNING, "Unable to parse " + xmlFile, ex);
return null;
}
modelReference = new WeakReference<BuildModel>(model);
return model;
}
/**
* Get the file model proxt for a file name.
*
* @param name
* the name to use.
* @return the file model proxy.
*/
public FileModelProxy getFileModelProxy(String name) {
BuildModel model = getModel();
if (model == null) {
return null;
}
return model.getFileModelMap().get(name);
}
/**
* This gets called to display a particular violation file report.
*
* @param token
* the current token in the path being parsed.
* @param req
* the http/stapler request.
* @param rsp
* the http/stapler response.
* @return an object to handle the token.
*/
public Object getDynamic(String token, StaplerRequest req, StaplerResponse rsp) {
// System.out.println("LOOKING for " + req.getRestOfPath());
String name = req.getRestOfPath();
if (name.equals("")) {
return null;
}
if (name.startsWith("/")) {
name = name.substring(1);
}
FileModelProxy proxy = getFileModelProxy(name);
if (proxy != null) {
return new RecurDynamic("", name, proxy.build(build).contextPath(""));
} else {
return new RecurDynamic("", name, new NoViolationsFile(name, build));
}
}
/**
* get the configuration for this job.
*
* @return the configuration of the job.
*/
public ViolationsConfig getLiveConfig() {
AbstractProject<?, ?> abstractProject = build.getProject();
if (abstractProject instanceof Project) {
Project project = (Project) abstractProject;
ViolationsPublisher publisher = (ViolationsPublisher) project.getPublisher(ViolationsPublisher.DESCRIPTOR);
return publisher == null ? null : publisher.getConfig();
}
return null;
}
/**
* Add a suppression to the set of suppressions.
*
* @param suppression
* the suppression to add.
*/
public void addSuppression(Suppression suppression) throws IOException {
ViolationsConfig config = getLiveConfig();
if (config != null) {
config.getSuppressions().add(suppression);
((AbstractProject) build.getParent()).save();
}
}
/**
* Remove a suppression to the set of suppressions.
*
* @param suppression
* the suppression to remove.
*/
public void removeSuppression(Suppression suppression) throws IOException {
ViolationsConfig config = getLiveConfig();
if (config != null) {
config.getSuppressions().remove(suppression);
((AbstractProject) build.getParent()).save();
}
}
/**
* Get a map of type to type summary report.
*
* @return a map.
*/
public Map<String, TypeSummary> getTypeSummaries() {
return typeSummaries;
}
/**
* Get a type summary for a particular type.
*
* @param type
* the violation type.
* @return the type summary.
*/
public TypeSummary getTypeSummary(String type) {
TypeSummary ret = typeSummaries.get(type);
if (ret == null) {
ret = new TypeSummary();
typeSummaries.put(type, ret);
}
return ret;
}
/**
* Get a map of type to type reports.
*
* @return a map of type to type reports.
*/
public Map<String, TypeReport> getTypeReports() {
Map<String, TypeReport> ret = new TreeMap<String, TypeReport>();
for (String t : violations.keySet()) {
int c = violations.get(t);
HealthReport health = getHealthReportFor(t);
ret.put(t, new TypeReport(t, health.getIconUrl(), c));
}
return ret;
}
/**
* Graph this report. Note that for some reason, yet unknown, hudson seems
* to pick an in memory ViolationsReport object and not the report for the
* build. (Reason may be related to the fact that serialized builds may not
* be the same as in-memory builds). Need to find the correct build from the
* URI.
*
* @param req
* the request paramters
* @param rsp
* the response.
* @throws IOException
* if there is an error writing the graph.
*/
public void doGraph(StaplerRequest req, StaplerResponse rsp) throws IOException {
AbstractBuild<?, ?> tBuild = build;
int buildNumber = HelpHudson.findBuildNumber(req);
if (buildNumber != 0) {
tBuild = build.getParent().getBuildByNumber(buildNumber);
if (tBuild == null) {
tBuild = build;
}
}
AbstractViolationsBuildAction r = tBuild.getAction(AbstractViolationsBuildAction.class);
if (r == null) {
return;
}
r.doGraph(req, rsp);
}
/**
* Get the string number for a particular type.
*
* @param t
* the type
* @return the string - a number for a value type, "" for not found and
* "No reports" for < 0.
*/
public String getNumberString(String t) {
Integer v = violations.get(t);
if (v == null) {
return "";
}
if (v < 0) {
return "<span style='color:red'>No reports</span>";
}
return "" + v;
}
/**
* Get the icon for a type.
*
* @param t
* the type
* @return the icon name.
*/
public String getIcon(String t) {
violations.get(t);
HealthReport h = getHealthReportFor(t);
if (h == null) {
return null;
}
return h.getIconUrl();
}
/**
* Report class for a particular type.
*/
public static class TypeReport {
private final String type;
private final String icon;
private final int number;
/**
* Create the report class for a type.
*
* @param type
* the violation type.
* @param icon
* the health icon to display.
* @param number
* the number of violations.
*/
public TypeReport(String type, String icon, int number) {
this.type = type;
this.icon = icon;
this.number = number;
}
/**
* Get the violation type.
*
* @return the violation type.
*/
public String getType() {
return type;
}
/**
* Get the health icon to display.
*
* @return the health icon.
*/
public String getIcon() {
return icon;
}
/**
* Get the number of violations.
*
* @return the number.
*/
public int getNumber() {
return number;
}
/**
* Get the number of violations as a string.
*
* @return the number if >= 0 othersise an error string.
*/
public String getNumberString() {
if (number >= 0) {
return "" + number;
} else {
return "<span style='color:red'>No reports</span>";
}
}
}
/**
* Get the previous ViolationsReport
*
* @return the previous report if present, null otherwise.
*/
public ViolationsReport previous() {
return findViolationsReport(build.getPreviousBuild());
}
/**
* Get the number of violations for a particular type.
*
* @param type
* the violation type.
* @return the number of violations.
*/
public int typeCount(String type) {
if (getModel() == null) {
return 0;
}
return getModel().getTypeCountMap().get(type).getCount();
}
/**
* Get the number of files in violation for a particular type.
*
* @param type
* the violation type.
* @return the number of files.
*/
public int fileCount(String type) {
if (getModel() == null) {
return 0;
}
return getModel().getTypeCountMap().get(type).getNumberFiles();
}
/**
* Get the number of violations of a type for a file.
*
* @param type
* the type in question.
* @param filename
* the name of the file.
* @return the number found (0 for none and for failures).
*/
public int violationCount(String type, String filename) {
FileModelProxy proxy = getFileModelProxy(filename);
if (proxy == null) {
return 0;
}
FileModel fileModel = proxy.getFileModel();
if (fileModel == null) {
return 0;
}
FileModel.LimitType limit = fileModel.getLimitTypeMap().get(type);
if (limit == null) {
return 0;
}
return limit.getNumber();
}
/**
* Get the unstable status for this report.
*
* @return true if one of the violations equals or exceed the unstable
* threshold for that violations type.
*/
private boolean isUnstable() {
for (String t : violations.keySet()) {
int count = violations.get(t);
Integer unstableLimit = config.getTypeConfigs().get(t).getUnstable();
if (unstableLimit == null) {
continue;
}
if (count >= unstableLimit) {
return true;
}
}
return false;
}
/**
* Get the failed status for this report.
*
* @return true if one of the violations equals or exceed the failed
* threshold of that violations type.
*/
private boolean isFailed() {
for (String t : violations.keySet()) {
int count = violations.get(t);
Integer failCount = config.getTypeConfigs().get(t).getFail();
if (failCount == null) {
continue;
}
if (count >= failCount) {
return true;
}
}
return false;
}
/**
* Set the unstable/failed status of a build based on this violations
* report.
*/
public void setBuildResult() {
if (isFailed()) {
build.setResult(Result.FAILURE);
return;
}
if (isUnstable()) {
try {
build.setResult(Result.UNSTABLE);
} catch (IllegalStateException e) {
LOG.log(SEVERE,
"Cannot set build to unstable for maven jobs, only for freestyle jobs. Issue: JENKINS-25406", e);
}
}
}
private static final long serialVersionUID = 1L;
public static ViolationsReport findViolationsReport(AbstractBuild<?, ?> b) {
for (; b != null; b = b.getPreviousBuild()) {
if (b.getResult().isWorseOrEqualTo(Result.FAILURE)) {
continue;
}
AbstractViolationsBuildAction action = b.getAction(AbstractViolationsBuildAction.class);
if (action == null || action.getReport() == null) {
continue;
}
ViolationsReport ret = action.getReport();
ret.setBuild(b);
return ret;
}
return null;
}
public static ViolationsReportIterator iteration(AbstractBuild<?, ?> build) {
return new ViolationsReportIterator(build);
}
public static class ViolationsReportIterator implements Iterator<ViolationsReport>, Iterable<ViolationsReport> {
private AbstractBuild<?, ?> curr;
public ViolationsReportIterator(AbstractBuild<?, ?> curr) {
this.curr = curr;
}
@Override
public Iterator<ViolationsReport> iterator() {
return this;
}
@Override
public boolean hasNext() {
return findViolationsReport(curr) != null;
}
@Override
public ViolationsReport next() {
ViolationsReport ret = findViolationsReport(curr);
if (ret != null) {
curr = ret.getBuild().getPreviousBuild();
}
return ret;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
}
}