forked from ConsoleCatzirl/jenkins-logstash-plugin
-
Notifications
You must be signed in to change notification settings - Fork 107
Expand file tree
/
Copy pathBuildData.java
More file actions
447 lines (372 loc) · 12.2 KB
/
Copy pathBuildData.java
File metadata and controls
447 lines (372 loc) · 12.2 KB
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
/*
* The MIT License
*
* Copyright 2014 Rusty Gerard
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package jenkins.plugins.logstash.persistence;
import hudson.model.Action;
import hudson.model.Environment;
import hudson.model.Executor;
import hudson.model.Result;
import hudson.model.AbstractBuild;
import hudson.model.TaskListener;
import hudson.model.Run;
import hudson.model.Node;
import hudson.tasks.test.AbstractTestResultAction;
import hudson.tasks.test.TestResult;
import jenkins.plugins.logstash.LogstashConfiguration;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.logging.Logger;
import static java.util.logging.Level.WARNING;
import java.io.IOException;
import java.lang.invoke.MethodHandles;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
/**
* POJO for mapping build info to JSON.
*
* @author Rusty Gerard
* @since 1.0.0
*/
public class BuildData implements Serializable {
// ISO 8601 date format
private final static Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass().getCanonicalName());
public static class TestData {
private int totalCount, skipCount, failCount, passCount;
private List<FailedTest> failedTestsWithErrorDetail;
private List<String> failedTests;
public static class FailedTest {
private final String fullName, errorDetails;
public FailedTest(String fullName, String errorDetails) {
super();
this.fullName = fullName;
this.errorDetails = errorDetails;
}
public String getFullName()
{
return fullName;
}
public String getErrorDetails()
{
return errorDetails;
}
}
public TestData() {
this(null);
}
public TestData(Action action) {
AbstractTestResultAction<?> testResultAction = null;
if (action instanceof AbstractTestResultAction) {
testResultAction = (AbstractTestResultAction<?>) action;
}
if (testResultAction == null) {
totalCount = skipCount = failCount = 0;
failedTests = Collections.emptyList();
failedTestsWithErrorDetail = Collections.emptyList();
return;
}
totalCount = testResultAction.getTotalCount();
skipCount = testResultAction.getSkipCount();
failCount = testResultAction.getFailCount();
passCount = totalCount - skipCount - failCount;
failedTests = new ArrayList<String>();
failedTestsWithErrorDetail = new ArrayList<FailedTest>();
for (TestResult result : testResultAction.getFailedTests()) {
failedTests.add(result.getFullName());
failedTestsWithErrorDetail.add(new FailedTest(result.getFullName(),result.getErrorDetails()));
}
}
public int getTotalCount()
{
return totalCount;
}
public int getSkipCount()
{
return skipCount;
}
public int getFailCount()
{
return failCount;
}
public int getPassCount()
{
return passCount;
}
public List<FailedTest> getFailedTestsWithErrorDetail()
{
return failedTestsWithErrorDetail;
}
public List<String> getFailedTests()
{
return failedTests;
}
}
private String id;
private String result;
private String projectName;
private String fullProjectName;
private String displayName;
private String fullDisplayName;
private String description;
private String url;
private String buildHost;
private String buildLabel;
private int buildNum;
private long buildDuration;
private transient String timestamp; // This belongs in the root object
private transient Run<?, ?> build;
private String rootProjectName;
private String rootFullProjectName;
private String rootProjectDisplayName;
private int rootBuildNum;
private Map<String, String> buildVariables;
private Set<String> sensitiveBuildVariables;
private TestData testResults = null;
// Freestyle project build
public BuildData(AbstractBuild<?, ?> build, Date currentTime, TaskListener listener) {
initData(build, currentTime);
// build.getDuration() is always 0 in Notifiers
rootProjectName = build.getRootBuild().getProject().getName();
rootFullProjectName = build.getRootBuild().getProject().getFullName();
rootProjectDisplayName = build.getRootBuild().getDisplayName();
rootBuildNum = build.getRootBuild().getNumber();
buildVariables = build.getBuildVariables();
sensitiveBuildVariables = build.getSensitiveBuildVariables();
// Get environment build variables and merge them into the buildVariables map
Map<String, String> buildEnvVariables = new HashMap<String, String>();
List<Environment> buildEnvironments = build.getEnvironments();
if (buildEnvironments != null) {
for (Environment env : buildEnvironments) {
if (env == null) {
continue;
}
env.buildEnvVars(buildEnvVariables);
if (!buildEnvVariables.isEmpty()) {
buildVariables.putAll(buildEnvVariables);
buildEnvVariables.clear();
}
}
}
try {
buildVariables.putAll(build.getEnvironment(listener));
} catch (Exception e) {
// no base build env vars to merge
LOGGER.log(WARNING,"Unable update logstash buildVariables with EnvVars from " + build.getDisplayName(),e);
}
for (String key : sensitiveBuildVariables) {
buildVariables.remove(key);
}
}
// Pipeline project build
public BuildData(Run<?, ?> build, Date currentTime, TaskListener listener) {
initData(build, currentTime);
rootProjectName = projectName;
rootFullProjectName = fullProjectName;
rootProjectDisplayName = displayName;
rootBuildNum = buildNum;
try {
// TODO: sensitive variables are not filtered, c.f. https://stackoverflow.com/questions/30916085
buildVariables = build.getEnvironment(listener);
} catch (IOException | InterruptedException e) {
LOGGER.log(WARNING,"Unable to get environment for " + build.getDisplayName(),e);
buildVariables = new HashMap<String, String>();
}
}
private void initData(Run<?, ?> build, Date currentTime) {
this.build = build;
Executor executor = build.getExecutor();
if (executor == null) {
buildHost = "master";
buildLabel = "master";
} else {
Node node = executor.getOwner().getNode();
if (node == null) {
buildHost = "master";
buildLabel = "master";
} else {
buildHost = StringUtils.isBlank(node.getDisplayName()) ? "master" : node.getDisplayName();
buildLabel = StringUtils.isBlank(node.getLabelString()) ? "master" : node.getLabelString();
}
}
id = build.getId();
projectName = build.getParent().getName();
fullProjectName = build.getParent().getFullName();
displayName = build.getDisplayName();
fullDisplayName = build.getFullDisplayName();
description = build.getDescription();
url = build.getUrl();
buildNum = build.getNumber();
buildDuration = currentTime.getTime() - build.getStartTimeInMillis();
timestamp = LogstashConfiguration.getInstance().getDateFormatter().format(build.getTimestamp().getTime());
updateResult();
}
public void updateResult()
{
if (result == null && build.getResult() != null)
{
Result result = build.getResult();
this.result = result == null ? null : result.toString();
}
Action testResultAction = build.getAction(AbstractTestResultAction.class);
if (testResults == null && testResultAction != null) {
testResults = new TestData(testResultAction);
}
}
@Override
public String toString() {
Gson gson = new GsonBuilder().create();
return gson.toJson(this);
}
public JSONObject toJson() {
String data = toString();
return JSONObject.fromObject(data);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getResult() {
return result;
}
public void setResult(Result result) {
this.result = result.toString();
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public String getFullProjectName() {
return fullProjectName;
}
public void setFullProjectName(String fullProjectName) {
this.fullProjectName = fullProjectName;
}
public String getDisplayName() {
return displayName;
}
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
public String getFullDisplayName() {
return fullDisplayName;
}
public void setFullDisplayName(String fullDisplayName) {
this.fullDisplayName = fullDisplayName;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getBuildHost() {
return buildHost;
}
public void setBuildHost(String buildHost) {
this.buildHost = buildHost;
}
public String getBuildLabel() {
return buildLabel;
}
public void setBuildLabel(String buildLabel) {
this.buildLabel = buildLabel;
}
public int getBuildNum() {
return buildNum;
}
public void setBuildNum(int buildNum) {
this.buildNum = buildNum;
}
public long getBuildDuration() {
return buildDuration;
}
public void setBuildDuration(long buildDuration) {
this.buildDuration = buildDuration;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(Calendar timestamp) {
this.timestamp = LogstashConfiguration.getInstance().getDateFormatter().format(timestamp.getTime());
}
public String getRootProjectName() {
return rootProjectName;
}
public void setRootProjectName(String rootProjectName) {
this.rootProjectName = rootProjectName;
}
public String getRootFullProjectName() {
return rootFullProjectName;
}
public void setRootFullProjectName(String rootFullProjectName) {
this.rootFullProjectName = rootFullProjectName;
}
public String getRootProjectDisplayName() {
return rootProjectDisplayName;
}
public void setRootProjectDisplayName(String rootProjectDisplayName) {
this.rootProjectDisplayName = rootProjectDisplayName;
}
public int getRootBuildNum() {
return rootBuildNum;
}
public void setRootBuildNum(int rootBuildNum) {
this.rootBuildNum = rootBuildNum;
}
public Map<String, String> getBuildVariables() {
return buildVariables;
}
public void setBuildVariables(Map<String, String> buildVariables) {
this.buildVariables = buildVariables;
}
public Set<String> getSensitiveBuildVariables() {
return sensitiveBuildVariables;
}
public void setSensitiveBuildVariables(Set<String> sensitiveBuildVariables) {
this.sensitiveBuildVariables = sensitiveBuildVariables;
}
public TestData getTestResults() {
return testResults;
}
public void setTestResults(TestData testResults) {
this.testResults = testResults;
}
}