This repository has been archived by the owner on Apr 6, 2022. It is now read-only.
/
QualityGate.java
335 lines (298 loc) · 14.6 KB
/
QualityGate.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
package io.jenkins.plugins.analysis.core.quality;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.kohsuke.stapler.export.ExportedBean;
import io.jenkins.plugins.analysis.core.model.AnalysisResult;
import io.jenkins.plugins.analysis.core.quality.ThresholdSet.ThresholdResult;
import io.jenkins.plugins.analysis.core.quality.ThresholdSet.ThresholdSetBuilder;
import hudson.model.Result;
/**
* Defines quality gates for a static analysis run.
*
* @author Michael Schmid
*/
@ExportedBean
public class QualityGate implements Serializable {
private final ThresholdSet totalUnstableThreshold;
private final ThresholdSet totalFailedThreshold;
private final ThresholdSet newUnstableThreshold;
private final ThresholdSet newFailedThreshold;
/**
* Creates a new instance of {@link QualityGate}.
*
* @param thresholds
* the thresholds to apply
*/
public QualityGate(final hudson.plugins.analysis.core.Thresholds thresholds) {
ThresholdSetBuilder builder = new ThresholdSetBuilder();
builder.setTotalThreshold(thresholds.failedTotalAll);
builder.setHighThreshold(thresholds.failedTotalHigh);
builder.setNormalThreshold(thresholds.failedTotalNormal);
builder.setLowThreshold(thresholds.failedTotalLow);
totalFailedThreshold = builder.build();
builder.setTotalThreshold(thresholds.unstableTotalAll);
builder.setHighThreshold(thresholds.unstableTotalHigh);
builder.setNormalThreshold(thresholds.unstableTotalNormal);
builder.setLowThreshold(thresholds.unstableTotalLow);
totalUnstableThreshold = builder.build();
builder.setTotalThreshold(thresholds.failedNewAll);
builder.setHighThreshold(thresholds.failedNewHigh);
builder.setNormalThreshold(thresholds.failedNewNormal);
builder.setLowThreshold(thresholds.failedNewLow);
newFailedThreshold = builder.build();
builder.setTotalThreshold(thresholds.unstableNewAll);
builder.setHighThreshold(thresholds.unstableNewHigh);
builder.setNormalThreshold(thresholds.unstableNewNormal);
builder.setLowThreshold(thresholds.unstableNewLow);
newUnstableThreshold = builder.build();
}
/**
* Creates a new instance of {@link QualityGate}.
*
* @param thresholds
* the thresholds to apply
*/
public QualityGate(final Thresholds thresholds) {
ThresholdSetBuilder builder = new ThresholdSetBuilder();
builder.setTotalThreshold(thresholds.failedTotalAll);
builder.setHighThreshold(thresholds.failedTotalHigh);
builder.setNormalThreshold(thresholds.failedTotalNormal);
builder.setLowThreshold(thresholds.failedTotalLow);
totalFailedThreshold = builder.build();
builder.setTotalThreshold(thresholds.unstableTotalAll);
builder.setHighThreshold(thresholds.unstableTotalHigh);
builder.setNormalThreshold(thresholds.unstableTotalNormal);
builder.setLowThreshold(thresholds.unstableTotalLow);
totalUnstableThreshold = builder.build();
builder.setTotalThreshold(thresholds.failedNewAll);
builder.setHighThreshold(thresholds.failedNewHigh);
builder.setNormalThreshold(thresholds.failedNewNormal);
builder.setLowThreshold(thresholds.failedNewLow);
newFailedThreshold = builder.build();
builder.setTotalThreshold(thresholds.unstableNewAll);
builder.setHighThreshold(thresholds.unstableNewHigh);
builder.setNormalThreshold(thresholds.unstableNewNormal);
builder.setLowThreshold(thresholds.unstableNewLow);
newUnstableThreshold = builder.build();
}
private QualityGate(final ThresholdSet totalFailedThreshold, final ThresholdSet totalUnstableThreshold,
final ThresholdSet newFailedThreshold, final ThresholdSet newUnstableThreshold) {
this.totalFailedThreshold = totalFailedThreshold;
this.totalUnstableThreshold = totalUnstableThreshold;
this.newFailedThreshold = newFailedThreshold;
this.newUnstableThreshold = newUnstableThreshold;
}
private ThresholdSet getTotalUnstableThreshold() {
return totalUnstableThreshold;
}
private ThresholdSet getTotalFailedThreshold() {
return totalFailedThreshold;
}
private ThresholdSet getNewUnstableThreshold() {
return newUnstableThreshold;
}
private ThresholdSet getNewFailedThreshold() {
return newFailedThreshold;
}
/**
* Enforces this quality gate for the specified run.
*
* @param run
* the run to evaluate
*
* @return result of the evaluation, expressed by a build state
*/
public QualityGateResult evaluate(final AnalysisResult run) {
return new QualityGateResult(
getTotalFailedThreshold().evaluate(
run.getTotalSize(),
run.getTotalHighPrioritySize(),
run.getTotalNormalPrioritySize(),
run.getTotalLowPrioritySize()),
getTotalUnstableThreshold().evaluate(
run.getTotalSize(),
run.getTotalHighPrioritySize(),
run.getTotalNormalPrioritySize(),
run.getTotalLowPrioritySize()),
getNewFailedThreshold().evaluate(
run.getNewSize(),
run.getNewHighPrioritySize(),
run.getNewNormalPrioritySize(),
run.getNewLowPrioritySize()),
getNewUnstableThreshold().evaluate(
run.getNewSize(),
run.getNewHighPrioritySize(),
run.getNewNormalPrioritySize(),
run.getNewLowPrioritySize()));
}
@Override
public boolean equals(final Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
QualityGate that = (QualityGate) o;
if (!totalUnstableThreshold.equals(that.totalUnstableThreshold)) {
return false;
}
if (!totalFailedThreshold.equals(that.totalFailedThreshold)) {
return false;
}
if (!newUnstableThreshold.equals(that.newUnstableThreshold)) {
return false;
}
return newFailedThreshold.equals(that.newFailedThreshold);
}
@Override
public int hashCode() {
int result = totalUnstableThreshold.hashCode();
result = 31 * result + totalFailedThreshold.hashCode();
result = 31 * result + newUnstableThreshold.hashCode();
result = 31 * result + newFailedThreshold.hashCode();
return result;
}
public boolean isEnabled() {
return totalFailedThreshold.isEnabled()
|| totalUnstableThreshold.isEnabled()
|| newFailedThreshold.isEnabled()
|| newUnstableThreshold.isEnabled();
}
/**
* Result of the {@link QualityGate} evaluation.
*/
public static class QualityGateResult implements Serializable {
private static final long serialVersionUID = -629019781304545613L;
private final ThresholdResult totalFailed;
private final ThresholdResult totalUnstable;
private final ThresholdResult newFailed;
private final ThresholdResult newUnstable;
QualityGateResult(final ThresholdResult totalFailed,
final ThresholdResult totalUnstable, final ThresholdResult newFailed,
final ThresholdResult newUnstable) {
this.totalFailed = totalFailed;
this.totalUnstable = totalUnstable;
this.newFailed = newFailed;
this.newUnstable = newUnstable;
}
/**
* Returns the overall result for the set quality gates.
*
* @return the overall result
*/
public Result getOverallResult() {
if (!totalFailed.isSuccess() || !newFailed.isSuccess()) {
return Result.FAILURE;
}
if (!totalUnstable.isSuccess() || !newUnstable.isSuccess()) {
return Result.UNSTABLE;
}
return Result.SUCCESS;
}
public Collection<String> getEvaluations(final AnalysisResult result, final QualityGate thresholds) {
List<String> messages = new ArrayList<>();
if (totalFailed.isTotalReached()) {
messages.add(String.format("FAILURE -> Total number of issues: %d - Quality Gate: %d",
result.getTotalSize(), thresholds.getTotalFailedThreshold().getTotalThreshold()));
}
if (totalFailed.isHighReached()) {
messages.add(String.format("FAILURE -> Number of high priority issues: %d - Quality Gate: %d",
result.getTotalHighPrioritySize(), thresholds.getTotalFailedThreshold().getHighThreshold()));
}
if (totalFailed.isNormalReached()) {
messages.add(String.format("FAILURE -> Number of normal priority issues: %d - Quality Gate: %d",
result.getTotalNormalPrioritySize(), thresholds.getTotalFailedThreshold().getNormalThreshold()));
}
if (totalFailed.isLowReached()) {
messages.add(String.format("FAILURE -> Number of low priority issues: %d - Quality Gate: %d",
result.getTotalLowPrioritySize(), thresholds.getTotalFailedThreshold().getLowThreshold()));
}
if (totalUnstable.isTotalReached()) {
messages.add(String.format("UNSTABLE -> Total number of issues: %d - Quality Gate: %d",
result.getTotalSize(), thresholds.getTotalUnstableThreshold().getTotalThreshold()));
}
if (totalUnstable.isHighReached()) {
messages.add(String.format("UNSTABLE -> Number of high priority issues: %d - Quality Gate: %d",
result.getTotalHighPrioritySize(), thresholds.getTotalUnstableThreshold().getHighThreshold()));
}
if (totalUnstable.isNormalReached()) {
messages.add(String.format("UNSTABLE -> Number of normal priority issues: %d - Quality Gate: %d",
result.getTotalNormalPrioritySize(), thresholds.getTotalUnstableThreshold().getNormalThreshold()));
}
if (totalUnstable.isLowReached()) {
messages.add(String.format("UNSTABLE -> Number of low priority issues: %d - Quality Gate: %d",
result.getTotalLowPrioritySize(), thresholds.getTotalUnstableThreshold().getLowThreshold()));
}
if (newFailed.isTotalReached()) {
messages.add(String.format("FAILURE -> Number of new issues: %d - Quality Gate: %d",
result.getNewSize(), thresholds.getNewFailedThreshold().getTotalThreshold()));
}
if (newFailed.isHighReached()) {
messages.add(String.format("FAILURE -> Number of new high priority issues: %d - Quality Gate: %d",
result.getNewHighPrioritySize(), thresholds.getNewFailedThreshold().getHighThreshold()));
}
if (newFailed.isNormalReached()) {
messages.add(String.format("FAILURE -> Number of new normal priority issues: %d - Quality Gate: %d",
result.getNewNormalPrioritySize(), thresholds.getNewFailedThreshold().getNormalThreshold()));
}
if (newFailed.isLowReached()) {
messages.add(String.format("FAILURE -> Number of new low priority issues: %d - Quality Gate: %d",
result.getNewLowPrioritySize(), thresholds.getNewFailedThreshold().getLowThreshold()));
}
if (newUnstable.isTotalReached()) {
messages.add(String.format("UNSTABLE -> New number of new issues: %d - Quality Gate: %d",
result.getNewSize(), thresholds.getNewUnstableThreshold().getTotalThreshold()));
}
if (newUnstable.isHighReached()) {
messages.add(String.format("UNSTABLE -> Number of new high priority issues: %d - Quality Gate: %d",
result.getNewHighPrioritySize(), thresholds.getNewUnstableThreshold().getHighThreshold()));
}
if (newUnstable.isNormalReached()) {
messages.add(String.format("UNSTABLE -> Number of new normal priority issues: %d - Quality Gate: %d",
result.getNewNormalPrioritySize(), thresholds.getNewUnstableThreshold().getNormalThreshold()));
}
if (newUnstable.isLowReached()) {
messages.add(String.format("UNSTABLE -> Number of new low priority issues: %d - Quality Gate: %d",
result.getNewLowPrioritySize(), thresholds.getNewUnstableThreshold().getLowThreshold()));
}
return messages;
}
}
/**
* Creates {@link QualityGate} instances using the builder pattern.
*/
public static class QualityGateBuilder {
private ThresholdSet totalUnstableThreshold = new ThresholdSet(0, 0, 0, 0);
private ThresholdSet totalFailedThreshold = new ThresholdSet(0, 0, 0, 0);
private ThresholdSet newUnstableThreshold = new ThresholdSet(0, 0, 0, 0);
private ThresholdSet newFailedThreshold = new ThresholdSet(0, 0, 0, 0);
/**
* Creates a new {@link QualityGate} instance using the defined parameters.
*
* @return the created quality gate
*/
public QualityGate build() {
return new QualityGate(totalFailedThreshold, totalUnstableThreshold, newFailedThreshold,
newUnstableThreshold);
}
public QualityGateBuilder setTotalUnstableThreshold(final ThresholdSet totalUnstableThreshold) {
this.totalUnstableThreshold = totalUnstableThreshold;
return this;
}
public QualityGateBuilder setTotalFailedThreshold(final ThresholdSet totalFailedThreshold) {
this.totalFailedThreshold = totalFailedThreshold;
return this;
}
public QualityGateBuilder setNewUnstableThreshold(final ThresholdSet newUnstableThreshold) {
this.newUnstableThreshold = newUnstableThreshold;
return this;
}
public QualityGateBuilder setNewFailedThreshold(final ThresholdSet newFailedThreshold) {
this.newFailedThreshold = newFailedThreshold;
return this;
}
}
}