-
-
Notifications
You must be signed in to change notification settings - Fork 100
Expand file tree
/
Copy pathRebuildAction.java
More file actions
445 lines (408 loc) · 17.2 KB
/
RebuildAction.java
File metadata and controls
445 lines (408 loc) · 17.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
/*
* The MIT License
*
* Copyright 2010 Sony Ericsson Mobile Communications. All rights reservered.
* Copyright 2012 Sony Mobile Communications AB. All rights reservered.
*
* 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 com.sonyericsson.rebuild;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.Extension;
import hudson.ExtensionList;
import hudson.model.Action;
import jakarta.servlet.ServletException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import hudson.model.BooleanParameterValue;
import hudson.model.Cause;
import hudson.model.CauseAction;
import hudson.model.Item;
import hudson.model.Job;
import hudson.model.ParameterValue;
import hudson.model.ParametersAction;
import hudson.model.ParametersDefinitionProperty;
import hudson.model.Queue;
import hudson.model.Run;
import hudson.model.SimpleParameterDefinition;
import hudson.model.ParameterDefinition;
import hudson.model.PasswordParameterValue;
import hudson.model.RunParameterValue;
import hudson.model.StringParameterValue;
import jenkins.model.Jenkins;
import jenkins.model.RunAction2;
import net.sf.json.JSONArray;
import net.sf.json.JSONNull;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest2;
import org.kohsuke.stapler.StaplerResponse2;
import org.kohsuke.stapler.interceptor.RequirePOST;
/**
* Rebuild RootAction implementation class. This class will basically reschedule
* the build with existing parameters.
*
* @author Shemeer S;
*/
public class RebuildAction extends AbstractRebuildAction implements RunAction2 {
private static final String SVN_TAG_PARAM_CLASS = "hudson.scm.listtagsparameter.ListSubversionTagsParameterValue";
private /* quasi-final */ Run<?, ?> run;
/*
* All the below transient variables are declared only for backward
* compatibility of the rebuild plugin.
*/
private static final String PARAMETERIZED_URL = "parameterized";
/**
* Rebuild Descriptor.
*/
@Extension
public static final RebuildDescriptor DESCRIPTOR = new RebuildDescriptor();
/**
* RebuildAction constructor.
*
* @param run The run.
*/
public RebuildAction(Run<?, ?> run) {
this.run = run;
}
/**
* True if the password fields should be pre-filled.
*
* @return True if the password fields should be pre-filled.
*/
public boolean isRememberPasswordEnabled() {
return DESCRIPTOR.getRebuildConfiguration().isRememberPasswordEnabled();
}
@Override
public String getDisplayName() {
if (isRebuildAvailable()) {
return "Rebuild";
} else {
return null;
}
}
@Override
public String getUrlName() {
return "rebuild";
}
@Override
public Run<?, ?> getRun() {
return run;
}
/**
* Decouple {@link #getUrlName()} from the actual URL, otherwise we cannot customize the target.
* @return
*/
@Override
public String getTaskUrl() {
if (isRebuildAvailable()) {
if (!isRequiresPOST()) {
return "rebuild/parameterized";
}
return "rebuild/"; // trailing / needed to prevent redirect to 405
} else {
return null;
}
}
@Override
public Job<?, ?> getProject() {
return run.getParent();
}
public boolean isRequiresPOST() {
if (run != null) {
ParametersAction paramAction = run.getAction(ParametersAction.class);
if (paramAction != null) {
RebuildSettings settings = getProject().getProperty(RebuildSettings.class);
return settings != null && settings.getAutoRebuild();
}
}
return true;
}
/**
* Handles the rebuild request and redirects to parameterized
* and non parameterized build when needed.
*
* @param request StaplerRequest2 the request.
* @param response StaplerResponse2 the response handler.
* @throws java.io.IOException in case of Stapler issues
*/
@RequirePOST
public void doIndex(StaplerRequest2 request, StaplerResponse2 response) throws IOException {
if (run != null) {
ParametersAction paramAction = run.getAction(ParametersAction.class);
if (paramAction != null) {
RebuildSettings settings = getProject().getProperty(RebuildSettings.class);
if (settings != null && settings.getAutoRebuild() || request.getParameter("autorebuild") != null) {
parameterizedRebuild(run, response);
} else {
response.sendRedirect(PARAMETERIZED_URL);
}
} else {
nonParameterizedRebuild(run, response);
}
}
}
/**
* Handles the rebuild request with parameter.
*
* @param currentBuild the build.
* @param response StaplerResponse2 the response handler.
* @throws IOException in case of Stapler issues
*/
public void parameterizedRebuild(Run currentBuild, StaplerResponse2 response) throws IOException {
Job project = getProject();
project.checkPermission(Item.BUILD);
if (isRebuildAvailable()) {
List<Action> actions = constructRebuildActions(run, currentBuild.getAction(ParametersAction.class));
Jenkins.get().getQueue().schedule2((Queue.Task)run.getParent(), 0, actions);
response.sendRedirect("../../");
}
}
/**
* Call this method while rebuilding
* non parameterized build. .
*
* @param currentBuild current build.
* @param response current response object.
* @throws IOException if something unfortunate happens.
*/
public void nonParameterizedRebuild(Run currentBuild, StaplerResponse2
response) throws IOException {
getProject().checkPermission(Item.BUILD);
List<Action> actions = constructRebuildActions(run, null);
Jenkins.get().getQueue().schedule2((Queue.Task)currentBuild.getParent(), 0, actions);
response.sendRedirect("../../");
}
/**
* Saves the form to the configuration and disk.
*
* @param req StaplerRequest2
* @param rsp StaplerResponse2
* @throws ServletException if something unfortunate happens.
* @throws IOException if something unfortunate happens.
*/
public void doConfigSubmit(StaplerRequest2 req, StaplerResponse2 rsp) throws ServletException, IOException {
Job project = getProject();
project.checkPermission(Item.BUILD);
if (isRebuildAvailable()) {
if (!req.getMethod().equals("POST")) {
// show the parameter entry form.
req.getView(this, "index.jelly").forward(req, rsp);
return;
}
ParametersDefinitionProperty paramDefProp = run.getParent().getProperty(
ParametersDefinitionProperty.class);
List<ParameterValue> values = new ArrayList<>();
ParametersAction paramAction = run.getAction(ParametersAction.class);
JSONObject formData = req.getSubmittedForm();
if (!formData.isEmpty()) {
JSONArray a = JSONArray.fromObject(formData.get("parameter"));
for (Object o : a) {
if (o instanceof JSONNull) {
continue;
}
JSONObject jo = (JSONObject)o;
String name = jo.getString("name");
ParameterValue parameterValue = getParameterValue(paramDefProp, name, paramAction, req, jo);
if (parameterValue != null) {
values.add(parameterValue);
}
}
}
for (ParameterValue source : paramAction.getParameters()) {
boolean alreadyAdded = false;
for (ParameterValue dest : values) {
if (source.getName().equals(dest.getName())) {
alreadyAdded = true;
break;
}
}
if (!alreadyAdded) {
values.add(source);
}
}
List<Action> actions = constructRebuildActions(run, new ParametersAction(values));
Jenkins.get().getQueue().schedule2((Queue.Task)run.getParent(), 0, actions);
rsp.sendRedirect("../../");
}
}
/**
* Extracts the build causes and adds or replaces the {@link hudson.model.Cause.UserIdCause}. The result is a
* list of all build causes from the original build (might be an empty list), plus a
* {@link hudson.model.Cause.UserIdCause} for the user who started the rebuild, plus a
* {@link RebuildCause} for this rebuild.
*
* @param fromBuild the build to copy the causes from.
* @return list with all original causes and a {@link hudson.model.Cause.UserIdCause}.
*/
private List<Cause> constructRebuildCauses(Run<?, ?> fromBuild) {
List<Cause> currentBuildCauses = new ArrayList<>(fromBuild.getCauses());
List<Cause> newBuildCauses = new ArrayList<>();
for (Cause buildCause : currentBuildCauses) {
if (!(buildCause instanceof Cause.UserIdCause) && !(buildCause instanceof RebuildCause)) {
newBuildCauses.add(buildCause);
}
}
newBuildCauses.add(new Cause.UserIdCause());
newBuildCauses.add(new RebuildCause(fromBuild));
return newBuildCauses;
}
/**
* Loops over all the RebuildActionDispatchers and adds any actions to the rebuild that they want included.
* Always copies the {@link hudson.model.ParametersAction} if it is present.
*
* @param fromBuild the build to copy the actions from
* @param actions the list to append additional copied actions
*/
private void copyRebuildDispatcherActions(Run<?, ?> fromBuild, List<Action> actions) {
Set<Action> propagatingActions = new HashSet<>();
// Get all RebuildActionsDispatchers that implement our extension point
ExtensionList<RebuildActionDispatcher> rebuildActionDispatchers = RebuildActionDispatcher.all();
for (RebuildActionDispatcher dispatcher : rebuildActionDispatchers) {
propagatingActions.addAll(dispatcher.getPropagatingActions(fromBuild));
}
actions.addAll(propagatingActions);
}
/**
* Method for getting the ParameterValue instance from ParameterDefinition
* or ParamterAction.
*
* @param paramDefProp ParametersDefinitionProperty
* @param parameterName Name of the Parameter.
* @param paramAction ParametersAction
* @param req StaplerRequest2
* @param jo JSONObject
* @return ParameterValue instance of subclass of ParameterValue
*/
@SuppressFBWarnings("NP_NULL_ON_SOME_PATH_FROM_RETURN_VALUE") // see https://github.com/spotbugs/spotbugs/issues/651
public ParameterValue getParameterValue(ParametersDefinitionProperty paramDefProp,
String parameterName, ParametersAction paramAction, StaplerRequest2 req, JSONObject jo) {
ParameterDefinition paramDef;
// this is normal case when user try to rebuild a parameterized job.
if (paramDefProp != null) {
paramDef = paramDefProp.getParameterDefinition(parameterName);
if (paramDef != null) {
// The copy artifact plugin throws an exception when using createValue(req, jo)
// If the parameter comes from the copy artifact plugin, then use the single argument createValue
if (jo.toString().contains("BuildSelector") || jo.toString().contains("WorkspaceSelector")) {
SimpleParameterDefinition parameterDefinition =
(SimpleParameterDefinition)paramDefProp.getParameterDefinition(parameterName);
return parameterDefinition.createValue(jo.getString("value"));
}
return paramDef.createValue(req, jo);
}
}
/*
* when user try to rebuild a build that was invoked by
* parameterized trigger plugin in that case ParameterDefinition
* is null for that parametername that is paased by parameterize
* trigger plugin,so for handling that scenario, we need to
* create an instance of that specific ParameterValue with
* passed parameter value by form.
*
* In contrast to all other parameterActions, ListSubversionTagsParameterValue uses "tag" instead of "value"
*/
if (jo.containsKey("value")) {
return cloneParameter(paramAction.getParameter(parameterName), jo.getString("value"));
} else {
return cloneParameter(paramAction.getParameter(parameterName), jo.getString("tag"));
}
}
/**
* Method for replacing the old parametervalue with new parameter value
*
* @param oldValue ParameterValue
* @param newValue The value that is submitted by user using form.
* @return ParameterValue
*/
private ParameterValue cloneParameter(ParameterValue oldValue, String newValue) {
if (oldValue instanceof StringParameterValue) {
return new StringParameterValue(oldValue.getName(), newValue, oldValue.getDescription());
} else if (oldValue instanceof BooleanParameterValue) {
return new BooleanParameterValue(oldValue.getName(), Boolean.parseBoolean(newValue),
oldValue.getDescription());
} else if (oldValue instanceof RunParameterValue) {
return new RunParameterValue(oldValue.getName(), newValue, oldValue.getDescription());
} else if (oldValue instanceof PasswordParameterValue) {
return new PasswordParameterValue(oldValue.getName(), newValue,
oldValue.getDescription());
} else if (oldValue.getClass().getName().equals(SVN_TAG_PARAM_CLASS)) {
/*
* getClass().getName() to avoid dependency on svn plugin.
*/
return new StringParameterValue(oldValue.getName(), newValue, oldValue.getDescription());
}
throw new IllegalArgumentException("Unrecognized parameter type: " + oldValue.getClass());
}
/**
* Method for constructing Rebuild actions.
*
* @param up AbstractBuild
* @param paramAction ParametersAction.
* @return actions List<Action>
*/
private List<Action> constructRebuildActions(Run up, ParametersAction paramAction) {
List<Cause> causes = constructRebuildCauses(up);
List<Action> actions = new ArrayList<>();
actions.add(new CauseAction(causes));
copyRebuildDispatcherActions(up, actions);
if (paramAction != null) {
actions.add(paramAction);
}
return actions;
}
/**
* @param value the parameter value to show to rebuild.
* @return page for the parameter value, or null if no suitable option found.
*/
public RebuildParameterPage getRebuildParameterPage(ParameterValue value) {
for (RebuildParameterProvider provider: RebuildParameterProvider.all()) {
RebuildParameterPage page = provider.getRebuildPage(value);
if (page != null) {
return page;
}
}
// Check if we have a branched Jelly in the plugin.
if (getClass()
.getResource(String.format(
"/%s/%s.jelly",
getClass().getCanonicalName().replace('.', '/'),
value.getClass().getSimpleName()))
!= null) {
// No provider available, use an existing view provided by rebuild plugin.
return new RebuildParameterPage(
getClass(),
String.format("%s.jelly", value.getClass().getSimpleName())
);
}
// Else we return that we haven't found anything.
// So Jelly fallback could occur.
return null;
}
@Override
public void onAttached(Run<?, ?> r) {
this.run = r;
}
@Override
public void onLoad(Run<?, ?> r) {
this.run = r;
}
}