/
Cause.java
517 lines (453 loc) · 16.6 KB
/
Cause.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
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Michael B. Donohue, Seiji Sogabe
*
* 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 hudson.model;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import hudson.console.ModelHyperlinkNote;
import hudson.diagnosis.OldDataMonitor;
import hudson.util.XStream2;
import jenkins.model.Jenkins;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.DoNotUse;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import hudson.Util;
import java.io.IOException;
import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
/**
* Cause object base class. This class hierarchy is used to keep track of why
* a given build was started. This object encapsulates the UI rendering of the cause,
* as well as providing more useful information in respective subypes.
*
* The Cause object is connected to a build via the {@link CauseAction} object.
*
* <h2>Views</h2>
* <dl>
* <dt>description.jelly
* <dd>Renders the cause to HTML. By default, it puts the short description.
* </dl>
*
* @author Michael Donohue
* @see Run#getCauses()
* @see Queue.Item#getCauses()
*/
@ExportedBean
public abstract class Cause {
/**
* One-line human-readable text of the cause.
*
* <p>
* By default, this method is used to render HTML as well.
*/
@Exported(visibility=3)
public abstract String getShortDescription();
/**
* Called when the cause is registered.
* @since 1.568
*/
public void onAddedTo(@Nonnull Run build) {
if (build instanceof AbstractBuild) {
onAddedTo((AbstractBuild) build);
}
}
@Deprecated
public void onAddedTo(AbstractBuild build) {
if (Util.isOverridden(Cause.class, getClass(), "onAddedTo", Run.class)) {
onAddedTo((Run) build);
}
}
/**
* Called when a build is loaded from disk.
* Useful in case the cause needs to keep a build reference;
* this ought to be {@code transient}.
* @since 1.568
*/
public void onLoad(@Nonnull Run<?,?> build) {
if (build instanceof AbstractBuild) {
onLoad((AbstractBuild) build);
}
}
void onLoad(@Nonnull Job<?,?> job, int buildNumber) {
Run<?,?> build = job.getBuildByNumber(buildNumber);
if (build != null) {
onLoad(build);
}
}
@Deprecated
public void onLoad(AbstractBuild<?,?> build) {
if (Util.isOverridden(Cause.class, getClass(), "onLoad", Run.class)) {
onLoad((Run) build);
}
}
/**
* Report a line to the listener about this cause.
* @since 1.362
*/
public void print(TaskListener listener) {
listener.getLogger().println(getShortDescription());
}
/**
* Fall back implementation when no other type is available.
* @deprecated since 2009-02-08
*/
@Deprecated
public static class LegacyCodeCause extends Cause {
private StackTraceElement [] stackTrace;
public LegacyCodeCause() {
stackTrace = new Exception().getStackTrace();
}
@Override
public String getShortDescription() {
return Messages.Cause_LegacyCodeCause_ShortDescription();
}
}
/**
* A build is triggered by another build (AKA upstream build.)
*/
public static class UpstreamCause extends Cause {
/**
* Maximum depth of transitive upstream causes we want to record.
*/
private static final int MAX_DEPTH = 10;
/**
* Maximum number of transitive upstream causes we want to record.
*/
private static final int MAX_LEAF = 25;
private String upstreamProject, upstreamUrl;
private int upstreamBuild;
/**
* @deprecated since 2009-02-28
*/
@Deprecated
private transient Cause upstreamCause;
private @Nonnull List<Cause> upstreamCauses;
/**
* @deprecated since 2009-02-28
*/
// for backward bytecode compatibility
@Deprecated
public UpstreamCause(AbstractBuild<?,?> up) {
this((Run<?,?>)up);
}
public UpstreamCause(Run<?, ?> up) {
upstreamBuild = up.getNumber();
upstreamProject = up.getParent().getFullName();
upstreamUrl = up.getParent().getUrl();
upstreamCauses = new ArrayList<Cause>();
Set<String> traversed = new HashSet<String>();
for (Cause c : up.getCauses()) {
upstreamCauses.add(trim(c, MAX_DEPTH, traversed));
}
}
private UpstreamCause(String upstreamProject, int upstreamBuild, String upstreamUrl, @Nonnull List<Cause> upstreamCauses) {
this.upstreamProject = upstreamProject;
this.upstreamBuild = upstreamBuild;
this.upstreamUrl = upstreamUrl;
this.upstreamCauses = upstreamCauses;
}
@Override
public void onLoad(@Nonnull Job<?,?> _job, int _buildNumber) {
Item i = Jenkins.getInstance().getItemByFullName(this.upstreamProject);
if (i == null || !(i instanceof Job)) {
// cannot initialize upstream causes
return;
}
Job j = (Job)i;
for (Cause c : this.upstreamCauses) {
c.onLoad(j, upstreamBuild);
}
}
/**
* @since 1.515
*/
@Override
public boolean equals(Object rhs) {
if (this == rhs) return true;
if (!(rhs instanceof UpstreamCause)) return false;
final UpstreamCause o = (UpstreamCause) rhs;
return Objects.equals(upstreamBuild, o.upstreamBuild) &&
Objects.equals(upstreamCauses, o.upstreamCauses) &&
Objects.equals(upstreamUrl, o.upstreamUrl) &&
Objects.equals(upstreamProject, o.upstreamProject);
}
/**
* @since 1.515
*/
@Override
public int hashCode() {
return Objects.hash(upstreamCauses, upstreamBuild, upstreamUrl, upstreamProject);
}
private @Nonnull Cause trim(@Nonnull Cause c, int depth, Set<String> traversed) {
if (!(c instanceof UpstreamCause)) {
return c;
}
UpstreamCause uc = (UpstreamCause) c;
List<Cause> cs = new ArrayList<Cause>();
if (depth > 0) {
if (traversed.add(uc.upstreamUrl + uc.upstreamBuild)) {
for (Cause c2 : uc.upstreamCauses) {
cs.add(trim(c2, depth - 1, traversed));
}
}
} else if (traversed.size() < MAX_LEAF) {
cs.add(new DeeplyNestedUpstreamCause());
}
return new UpstreamCause(uc.upstreamProject, uc.upstreamBuild, uc.upstreamUrl, cs);
}
/**
* Returns true if this cause points to a build in the specified job.
*/
public boolean pointsTo(Job<?,?> j) {
return j.getFullName().equals(upstreamProject);
}
/**
* Returns true if this cause points to the specified build.
*/
public boolean pointsTo(Run<?,?> r) {
return r.getNumber()==upstreamBuild && pointsTo(r.getParent());
}
@Exported(visibility=3)
public String getUpstreamProject() {
return upstreamProject;
}
@Exported(visibility=3)
public int getUpstreamBuild() {
return upstreamBuild;
}
/**
* @since 1.505
*/
public @CheckForNull Run<?,?> getUpstreamRun() {
Job<?,?> job = Jenkins.getInstance().getItemByFullName(upstreamProject, Job.class);
return job != null ? job.getBuildByNumber(upstreamBuild) : null;
}
@Exported(visibility=3)
public String getUpstreamUrl() {
return upstreamUrl;
}
public List<Cause> getUpstreamCauses() {
return upstreamCauses;
}
@Override
public String getShortDescription() {
return Messages.Cause_UpstreamCause_ShortDescription(upstreamProject, upstreamBuild);
}
@Override
public void print(TaskListener listener) {
print(listener, 0);
}
private void indent(TaskListener listener, int depth) {
for (int i = 0; i < depth; i++) {
listener.getLogger().print(' ');
}
}
private void print(TaskListener listener, int depth) {
indent(listener, depth);
listener.getLogger().println(
Messages.Cause_UpstreamCause_ShortDescription(
ModelHyperlinkNote.encodeTo('/' + upstreamUrl, upstreamProject),
ModelHyperlinkNote.encodeTo('/'+upstreamUrl+upstreamBuild, Integer.toString(upstreamBuild)))
);
if (upstreamCauses != null && !upstreamCauses.isEmpty()) {
indent(listener, depth);
listener.getLogger().println(Messages.Cause_UpstreamCause_CausedBy());
for (Cause cause : upstreamCauses) {
if (cause instanceof UpstreamCause) {
((UpstreamCause) cause).print(listener, depth + 1);
} else {
indent(listener, depth + 1);
cause.print(listener);
}
}
}
}
@Override public String toString() {
return upstreamUrl + upstreamBuild + upstreamCauses;
}
public static class ConverterImpl extends XStream2.PassthruConverter<UpstreamCause> {
public ConverterImpl(XStream2 xstream) { super(xstream); }
@Override protected void callback(UpstreamCause uc, UnmarshallingContext context) {
if (uc.upstreamCause != null) {
if (uc.upstreamCauses == null) uc.upstreamCauses = new ArrayList<Cause>();
uc.upstreamCauses.add(uc.upstreamCause);
uc.upstreamCause = null;
OldDataMonitor.report(context, "1.288");
}
}
}
public static class DeeplyNestedUpstreamCause extends Cause {
@Override public String getShortDescription() {
return "(deeply nested causes)";
}
@Override public String toString() {
return "JENKINS-14814";
}
@Override public void onLoad(@Nonnull Job<?,?> _job, int _buildNumber) {}
}
}
/**
* A build is started by an user action.
*
* @deprecated 1.428
* use {@link UserIdCause}
*/
@Deprecated
public static class UserCause extends Cause {
private String authenticationName;
public UserCause() {
this.authenticationName = Jenkins.getAuthentication().getName();
}
/**
* Gets user display name when possible.
* @return User display name.
* If the User does not exist, returns its ID.
*/
@Exported(visibility=3)
public String getUserName() {
final User user = User.getById(authenticationName, false);
return user != null ? user.getDisplayName() : authenticationName;
}
@Override
public String getShortDescription() {
return Messages.Cause_UserCause_ShortDescription(authenticationName);
}
@Override
public boolean equals(Object o) {
return o instanceof UserCause && Arrays.equals(new Object[] {authenticationName},
new Object[] {((UserCause)o).authenticationName});
}
@Override
public int hashCode() {
return 295 + (this.authenticationName != null ? this.authenticationName.hashCode() : 0);
}
}
/**
* A build is started by an user action.
*
* @since 1.427
*/
public static class UserIdCause extends Cause {
@CheckForNull
private String userId;
/**
* Constructor, which uses the current {@link User}.
*/
public UserIdCause() {
User user = User.current();
this.userId = (user == null) ? null : user.getId();
}
/**
* Constructor.
* @param userId User ID. {@code null} if the user is unknown.
* @since 2.96
*/
public UserIdCause(@CheckForNull String userId) {
this.userId = userId;
}
@Exported(visibility = 3)
@CheckForNull
public String getUserId() {
return userId;
}
@Nonnull
private String getUserIdOrUnknown() {
return userId != null ? userId : User.getUnknown().getId();
}
@Exported(visibility = 3)
public String getUserName() {
final User user = userId == null ? null : User.getById(userId, false);
return user == null ? "anonymous" : user.getDisplayName();
}
@Restricted(DoNotUse.class) // for Jelly
@CheckForNull
public String getUserUrl() {
final User user = userId == null ? null : User.getById(userId, false);
return user != null ? user.getUrl() : null;
}
@Override
public String getShortDescription() {
return Messages.Cause_UserIdCause_ShortDescription(getUserName());
}
@Override
public void print(TaskListener listener) {
User user = getUserId() == null ? null : User.getById(getUserId(), false);
if (user != null) {
listener.getLogger().println(Messages.Cause_UserIdCause_ShortDescription(
ModelHyperlinkNote.encodeTo(user)));
} else {
listener.getLogger().println(Messages.Cause_UserIdCause_ShortDescription(
"unknown or anonymous"));
}
}
@Override
public boolean equals(Object o) {
return o instanceof UserIdCause && Objects.equals(userId, ((UserIdCause) o).userId);
}
@Override
public int hashCode() {
return Objects.hash(userId);
}
}
public static class RemoteCause extends Cause {
private String addr;
private String note;
public RemoteCause(String host, String note) {
this.addr = host;
this.note = note;
}
@Override
public String getShortDescription() {
if(note != null) {
try {
return Messages.Cause_RemoteCause_ShortDescriptionWithNote(addr, Jenkins.getInstance().getMarkupFormatter().translate(note));
} catch (IOException x) {
// ignore
}
}
return Messages.Cause_RemoteCause_ShortDescription(addr);
}
@Exported(visibility = 3)
public String getAddr() {
return addr;
}
@Exported(visibility = 3)
public String getNote() {
return note;
}
@Override
public boolean equals(Object o) {
return o instanceof RemoteCause && Objects.equals(addr, ((RemoteCause) o).addr) && Objects.equals(note, ((RemoteCause) o).note);
}
@Override
public int hashCode() {
return Objects.hash(addr, note);
}
}
}