-
Notifications
You must be signed in to change notification settings - Fork 168
/
Status.java
345 lines (303 loc) · 10.6 KB
/
Status.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
package hudson.plugins.promoted_builds;
import hudson.EnvVars;
import hudson.Util;
import hudson.model.AbstractBuild;
import hudson.model.AbstractProject;
import hudson.model.Cause.UserCause;
import hudson.model.ParameterDefinition;
import hudson.model.ParameterValue;
import hudson.model.Result;
import hudson.plugins.promoted_builds.conditions.ManualCondition;
import hudson.util.Iterators;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.StaplerResponse;
import javax.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.GregorianCalendar;
import java.util.List;
import java.util.concurrent.Future;
import java.util.logging.Logger;
import jenkins.model.Jenkins;
import org.kohsuke.stapler.export.Exported;
import org.kohsuke.stapler.export.ExportedBean;
/**
* Promotion status of a build wrt a specific {@link PromotionProcess}.
*
* @author Kohsuke Kawaguchi
* @see PromotedBuildAction#statuses
*/
@ExportedBean
public final class Status {
/**
* Matches with {@link PromotionProcess#name}.
*/
public final String name;
private final PromotionBadge[] badges;
/**
* When did the build qualify for a promotion?
*/
public final Calendar timestamp = new GregorianCalendar();
/**
* If the build is successfully promoted, the build number of {@link Promotion}
* that represents that record.
*
* -1 to indicate that the promotion was not successful yet.
*/
private int promotion = -1;
/**
* Bulid numbers of {@link Promotion}s that are attempted.
* If {@link Promotion} fails, this field can have multiple values.
* Sorted in the ascending order.
*/
private List<Integer> promotionAttempts = new ArrayList<Integer>();
/*package*/ transient PromotedBuildAction parent;
public Status(PromotionProcess process, Collection<? extends PromotionBadge> badges) {
this.name = process.getName();
this.badges = badges.toArray(new PromotionBadge[badges.size()]);
}
@Exported
public String getName() {
return name;
}
/**
* Gets the parent {@link Status} that owns this object.
*/
public PromotedBuildAction getParent() {
if (parent==null){
parent=getTarget().getAction(PromotedBuildAction.class);
}
return parent;
}
/**
* Gets the {@link PromotionProcess} that this object deals with.
*/
@Exported
public PromotionProcess getProcess() {
assert parent != null : name;
AbstractProject<?,?> project = parent.getProject();
assert project != null : parent;
JobPropertyImpl jp = project.getProperty(JobPropertyImpl.class);
if(jp==null) return null;
return jp.getItem(name);
}
/**
* Gets the icon that should represent this promotion (that is potentially attempted but failed.)
*/
public String getIcon(String size) {
String baseName;
PromotionProcess p = getProcess();
if (p == null) {
// promotion process undefined (perhaps deleted?). fallback to the default icon
baseName = "star-gold";
} else {
Promotion l = getLast();
if (l!=null && l.getResult()!= Result.SUCCESS)
return Jenkins.RESOURCE_PATH+"/images/"+size+"/error.png";
baseName = p.getIcon();
}
return Jenkins.RESOURCE_PATH+"/plugin/promoted-builds/icons/"+size+"/"+ baseName +".png";
}
/**
* Gets the build that was qualified for a promotion.
*/
public AbstractBuild<?,?> getTarget() {
return getParent().owner;
}
/**
* Called by {@link Promotion} to allow status to contribute environment variables.
*
* @param build
* The calling build. Never null.
* @param env
* Environment variables should be added to this map.
*/
public void buildEnvVars(AbstractBuild<?,?> build, EnvVars env) {
for (PromotionBadge badge : badges) {
badge.buildEnvVars(build, env);
}
}
/**
* Gets the string that says how long since this promotion had happened.
*
* @return
* string like "3 minutes" "1 day" etc.
*/
public String getTimestampString() {
long duration = new GregorianCalendar().getTimeInMillis()-timestamp.getTimeInMillis();
return Util.getTimeSpanString(duration);
}
/**
* Gets the string that says how long did it toook for this build to be promoted.
*/
public String getDelayString(AbstractBuild<?,?> owner) {
long duration = timestamp.getTimeInMillis() - owner.getTimestamp().getTimeInMillis() - owner.getDuration();
return Util.getTimeSpanString(duration);
}
public boolean isFor(PromotionProcess process) {
return process.getName().equals(this.name);
}
/**
* Returns the {@link Promotion} object that represents the successful promotion.
*
* @return
* null if the promotion has never been successful, or if it was but
* the record is already lost.
*/
public Promotion getSuccessfulPromotion(JobPropertyImpl jp) {
if(promotion>=0) {
PromotionProcess p = jp.getItem(name);
if(p!=null)
return p.getBuildByNumber(promotion);
}
return null;
}
/**
* Returns true if the promotion was successfully completed.
*/
public boolean isPromotionSuccessful() {
return promotion>=0;
}
/**
* Returns true if at least one {@link Promotion} activity is attempted.
* False if none is executed yet (this includes the case where it's in the queue.)
*/
public boolean isPromotionAttempted() {
return !promotionAttempts.isEmpty();
}
/**
* Returns true if the promotion for this is pending in the queue,
* waiting to be executed.
*/
public boolean isInQueue() {
PromotionProcess p = getProcess();
return p!=null && p.isInQueue(getTarget());
}
/**
* Gets the badges indicating how did a build qualify for a promotion.
*/
@Exported
public List<PromotionBadge> getBadges() {
return Arrays.asList(badges);
}
/**
* Called when a new promotion attempts for this build starts.
*/
/*package*/ void addPromotionAttempt(Promotion p) {
promotionAttempts.add(p.getNumber());
}
/**
* Called when a promotion succeeds.
*/
/*package*/ void onSuccessfulPromotion(Promotion p) {
promotion = p.getNumber();
}
//
// web bound methods
//
/**
* Gets the last successful {@link Promotion}.
*/
public Promotion getLastSuccessful() {
PromotionProcess p = getProcess();
for( Integer n : Iterators.reverse(promotionAttempts) ) {
Promotion b = p.getBuildByNumber(n);
if(b!=null && b.getResult()== Result.SUCCESS)
return b;
}
return null;
}
/**
* Gets the last successful {@link Promotion}.
*/
public Promotion getLastFailed() {
PromotionProcess p = getProcess();
for( Integer n : Iterators.reverse(promotionAttempts) ) {
Promotion b = p.getBuildByNumber(n);
if(b!=null && b.getResult()!=Result.SUCCESS)
return b;
}
return null;
}
public Promotion getLast() {
PromotionProcess p = getProcess();
for( Integer n : Iterators.reverse(promotionAttempts) ) {
Promotion b = p.getBuildByNumber(n);
if(b!=null)
return b;
}
return null;
}
/**
* Gets all the promotion builds.
*/
@Exported
public List<Promotion> getPromotionBuilds() {
List<Promotion> builds = new ArrayList<Promotion>();
PromotionProcess p = getProcess();
if (p!=null) {
for( Integer n : Iterators.reverse(promotionAttempts) ) {
Promotion b = p.getBuildByNumber(n);
if (b != null) {
builds.add(b);
}
}
}
return builds;
}
/**
* Gets the promotion build by build number.
*
* @param number build number
* @return promotion build
*/
public Promotion getPromotionBuild(int number) {
PromotionProcess p = getProcess();
return p.getBuildByNumber(number);
}
public boolean isManuallyApproved(){
ManualCondition manualCondition=(ManualCondition) getProcess().getPromotionCondition(ManualCondition.class.getName());
return manualCondition!=null;
}
/**
* Schedules a new build.
*/
public void doBuild(StaplerRequest req, StaplerResponse rsp) throws IOException, ServletException {
if(!getTarget().hasPermission(Promotion.PROMOTE))
return;
JSONObject formData = req.getSubmittedForm();
List<ParameterValue> paramValues=null;
if (formData!=null){
paramValues = new ArrayList<ParameterValue>();
ManualCondition manualCondition=(ManualCondition) getProcess().getPromotionCondition(ManualCondition.class.getName());
if (manualCondition!=null){
List<ParameterDefinition> parameterDefinitions=manualCondition.getParameterDefinitions();
if (parameterDefinitions != null && !parameterDefinitions.isEmpty()) {
JSONArray a = JSONArray.fromObject(formData.get("parameter"));
for (Object o : a) {
JSONObject jo = (JSONObject) o;
String name = jo.getString("name");
ParameterDefinition d = manualCondition.getParameterDefinition(name);
if (d==null)
throw new IllegalArgumentException("No such parameter definition: " + name);
paramValues.add(d.createValue(req, jo));
}
}
}
}
if (paramValues==null){
paramValues = new ArrayList<ParameterValue>();
}
Future<Promotion> f = getProcess().scheduleBuild2(getTarget(), new UserCause(), paramValues);
if (f==null)
LOGGER.warning("Failing to schedule the promotion of "+getTarget());
// TODO: we need better visual feed back so that the user knows that the build happened.
rsp.forwardToPreviousPage(req);
}
private static final Logger LOGGER = Logger.getLogger(Status.class.getName());
}