-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
/
Copy pathResult.java
243 lines (215 loc) · 7.81 KB
/
Result.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
/*
* The MIT License
*
* Copyright (c) 2004-2010, Sun Microsystems, Inc., Kohsuke Kawaguchi
*
* 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 com.thoughtworks.xstream.converters.SingleValueConverter;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
import edu.umd.cs.findbugs.annotations.NonNull;
import hudson.cli.declarative.OptionHandlerExtension;
import hudson.init.Initializer;
import hudson.util.EditDistance;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.beanutils.Converter;
import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.export.CustomExportedBean;
/**
* The build outcome.
*
* @author Kohsuke Kawaguchi
*/
public final class Result implements Serializable, CustomExportedBean {
/**
* The build had no errors.
*/
public static final @NonNull Result SUCCESS = new Result("SUCCESS", BallColor.BLUE, 0, true);
/**
* The build had some errors but they were not fatal.
* For example, some tests failed.
*/
public static final @NonNull Result UNSTABLE = new Result("UNSTABLE", BallColor.YELLOW, 1, true);
/**
* The build had a fatal error.
*/
public static final @NonNull Result FAILURE = new Result("FAILURE", BallColor.RED, 2, true);
/**
* The module was not built.
* <p>
* This status code is used in a multi-stage build (like maven2)
* where a problem in earlier stage prevented later stages from building.
*/
public static final @NonNull Result NOT_BUILT = new Result("NOT_BUILT", BallColor.NOTBUILT, 3, false);
/**
* The build was manually aborted.
*
* If you are catching {@link InterruptedException} and interpreting it as {@link #ABORTED},
* you should check {@link Executor#abortResult()} instead (starting 1.417.)
*/
public static final @NonNull Result ABORTED = new Result("ABORTED", BallColor.ABORTED, 4, false);
private final @NonNull String name;
/**
* Bigger numbers are worse.
*/
public final /* @java.annotation.Nonnegative */ int ordinal;
/**
* Default ball color for this status.
*/
public final @NonNull BallColor color;
/**
* Is this a complete build - i.e. did it run to the end (not aborted)?
* @since 1.526
*/
public final boolean completeBuild;
private Result(@NonNull String name, @NonNull BallColor color, /*@java.annotation.Nonnegative */int ordinal, boolean complete) {
this.name = name;
this.color = color;
this.ordinal = ordinal;
this.completeBuild = complete;
}
/**
* Combines two {@link Result}s and returns the worse one.
*/
public @NonNull Result combine(@NonNull Result that) {
if (this.ordinal < that.ordinal)
return that;
else
return this;
}
/**
* Combines two {@link Result}s and returns the worse one.
* <p>
* This method is null-safe (any {@link Result} is "worse" than {@code null}, and {@code null} is returned if both
* parameters are {@code null}).
*
* @param r1
* a result (may be {@code null})
* @param r2
* a result (may be {@code null})
* @return the worst result (may be {@code null})
* @since 2.257
*/
public static Result combine(Result r1, Result r2) {
if (r1 == null) {
return r2;
} else if (r2 == null) {
return r1;
} else {
return r1.combine(r2);
}
}
public boolean isWorseThan(@NonNull Result that) {
return this.ordinal > that.ordinal;
}
public boolean isWorseOrEqualTo(@NonNull Result that) {
return this.ordinal >= that.ordinal;
}
public boolean isBetterThan(@NonNull Result that) {
return this.ordinal < that.ordinal;
}
public boolean isBetterOrEqualTo(@NonNull Result that) {
return this.ordinal <= that.ordinal;
}
/**
* Is this a complete build - i.e. did it run to the end (not aborted)?
* @since 1.526
*/
public boolean isCompleteBuild() {
return this.completeBuild;
}
@Override
public @NonNull String toString() {
return name;
}
@Override
public @NonNull String toExportedObject() {
return name;
}
public static @NonNull Result fromString(@NonNull String s) {
for (Result r : all)
if (s.equalsIgnoreCase(r.name))
return r;
return FAILURE;
}
private static @NonNull List<String> getNames() {
List<String> l = new ArrayList<>();
for (Result r : all)
l.add(r.name);
return l;
}
// Maintain each Result as a singleton deserialized (like build result from an agent node)
private Object readResolve() {
for (Result r : all)
if (ordinal == r.ordinal)
return r;
return FAILURE;
}
private static final long serialVersionUID = 1L;
private static final Result[] all = new Result[] {SUCCESS, UNSTABLE, FAILURE, NOT_BUILT, ABORTED};
public static final SingleValueConverter conv = new AbstractSingleValueConverter() {
@Override
public boolean canConvert(Class clazz) {
return clazz == Result.class;
}
@Override
public Object fromString(String s) {
return Result.fromString(s);
}
};
@OptionHandlerExtension
public static final class OptionHandlerImpl extends OptionHandler<Result> {
public OptionHandlerImpl(CmdLineParser parser, OptionDef option, Setter<? super Result> setter) {
super(parser, option, setter);
}
@Override
public int parseArguments(Parameters params) throws CmdLineException {
String param = params.getParameter(0);
Result v = fromString(param.replace('-', '_'));
if (v == FAILURE) {
throw new CmdLineException(owner, "No such status '" + param + "'. Did you mean " +
EditDistance.findNearest(param.replace('-', '_').toUpperCase(), getNames()));
}
setter.addValue(v);
return 1;
}
@Override
public String getDefaultMetaVariable() {
return "STATUS";
}
}
@Initializer
public static void init() {
Stapler.CONVERT_UTILS.register(new Converter() {
@Override
public Object convert(Class type, Object value) {
return Result.fromString(value.toString());
}
}, Result.class);
}
}