-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathBatchTaskInvoker.java
239 lines (208 loc) · 8.06 KB
/
BatchTaskInvoker.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
package hudson.plugins.batch_task;
import hudson.Extension;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.AbstractDescribableImpl;
import hudson.model.AbstractProject;
import hudson.model.Action;
import hudson.model.BuildListener;
import hudson.model.Cause.UpstreamCause;
import hudson.model.CauseAction;
import hudson.model.Descriptor;
import hudson.model.Item;
import hudson.model.ItemGroup;
import hudson.model.Result;
import hudson.model.Run;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Notifier;
import hudson.tasks.Publisher;
import hudson.util.FormValidation;
import hudson.util.ListBoxModel;
import jenkins.model.Jenkins;
import net.sf.json.JSONObject;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.AncestorInPath;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.QueryParameter;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
/**
* {@link Publisher} that triggers batch tasks of other projects.
*
* @author Kohsuke Kawaguchi
*/
public class BatchTaskInvoker extends Notifier {
/**
* What task to invoke?
*/
public static final class Config extends AbstractDescribableImpl<Config> {
public final String project;
public final String task;
@DataBoundConstructor
public Config(String project, String task) {
this.project = project;
this.task = task;
}
/**
* Finds the target {@link BatchTaskProperty}.
*/
public BatchTaskProperty resolveProperty(ItemGroup context) {
AbstractProject<?,?> p = Jenkins.getInstance().getItem(project, context, AbstractProject.class);
if(p==null) return null;
return p.getProperty(BatchTaskProperty.class);
}
/**
* Finds the target {@link BatchTask} that this configuration points to,
* or null if not found.
*/
public BatchTask resolve(ItemGroup context) {
BatchTaskProperty bp = resolveProperty(context);
if(bp==null) return null;
return bp.getTask(this.task);
}
/**
* @deprecated as of 1.15
*/
@Deprecated
public BatchTask resolve() {
return resolve(Jenkins.getInstance());
}
/**
* @deprecated as of 1.15
*/
@Deprecated
public BatchTaskProperty resolveProperty() {
return resolveProperty(null);
}
public boolean invoke(AbstractBuild<?,?> build, BuildListener listener, HashSet<String> seenJobs) {
PrintStream logger = listener.getLogger();
AbstractProject<?,?> p = Jenkins.getInstance().getItem(project, build.getProject(), AbstractProject.class);
if(p==null) {
listener.error(Messages.BatchTaskInvoker_NoSuchProject(project));
return false;
}
BatchTaskProperty bp = p.getProperty(BatchTaskProperty.class);
if(bp==null) {
listener.error(Messages.BatchTaskInvoker_NoBatchTaskExists(task));
return false;
}
BatchTask taskObj = bp.getTask(task);
if(taskObj==null) {
listener.error(Messages.BatchTaskInvoker_NoSuchTask(task,bp.findNearestTask(task).name));
return false;
}
// Only report nextBuildNumber once per project
String buildNum = "";
if (!seenJobs.contains(project)) {
buildNum = " #" + taskObj.getNextBuildNumber();
seenJobs.add(project);
}
logger.println(Messages.BatchTaskInvoker_Invoking(project,task,buildNum));
Jenkins.getInstance().getQueue().schedule(taskObj,0,
new CauseAction(new UpstreamCause((Run)build)));
return true;
}
@Extension
public static class DescriptorImpl extends Descriptor<Config> {
@Override
public String getDisplayName() {
return "";
}
public ListBoxModel doFillTaskItems(@QueryParameter String project, @AncestorInPath AbstractProject context) {
// when the item is not found, the user should be getting an error from elsewhere.
ListBoxModel r = new ListBoxModel();
AbstractProject<?,?> p = Jenkins.getInstance().getItem(project, context, AbstractProject.class);
if(p!=null) {
BatchTaskProperty bp = p.getProperty(BatchTaskProperty.class);
if(bp!=null) {
for(BatchTask task : bp.getTasks())
r.add(task.getDisplayName(), task.getName());
}
}
return r;
}
@Restricted(NoExternalUse.class)
public FormValidation doCheckProject(@QueryParameter String project) {
if (project.startsWith("/")) {
return FormValidation.warning(Messages.BatchTaskInvoker_ForwardSlash());
}
Jenkins jenkins = Jenkins.getInstance();
if (jenkins != null) {
Item item = jenkins.getItemByFullName(project);
if (item == null) {
return FormValidation.warning(Messages.BatchTaskInvoker_NoSuchProject(project));
}
}
return FormValidation.ok();
}
@Restricted(NoExternalUse.class)
public FormValidation doCheckTask(@QueryParameter String project, @QueryParameter String task) {
if (!project.isEmpty() && task.isEmpty()) {
return FormValidation.warning(Messages.BatchTaskInvoker_NoBatchTaskExists(task));
}
return FormValidation.ok();
}
}
}
private final Config[] configs;
private /*almost final*/ Result threshold;
private Object readResolve() {
if (threshold==null) threshold = Result.UNSTABLE;
return this;
}
@DataBoundConstructor
public BatchTaskInvoker(Config[] configs, boolean evenIfUnstable) {
this(configs,evenIfUnstable ? Result.UNSTABLE : Result.SUCCESS);
}
public BatchTaskInvoker(Config[] configs, Result threshold) {
this.configs = configs;
this.threshold = threshold;
}
public List<Config> getConfigs() {
return Collections.unmodifiableList(Arrays.asList(configs));
}
public Result getThreshold() {
return threshold;
}
public boolean isEvenIfUnstable() {
return threshold.isWorseOrEqualTo(Result.UNSTABLE);
}
@Override
public boolean perform(AbstractBuild<?,?> build, Launcher launcher, BuildListener listener) throws InterruptedException, IOException {
HashSet<String> seenJobs = new HashSet<String>();
if (build != null) {
Result result = build.getResult();
if (result != null) {
if (result.isBetterOrEqualTo(threshold)) {
for (Config config : configs)
config.invoke(build, listener, seenJobs);
}
}
}
return true;
}
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
@Override
public Collection<? extends Action> getProjectActions(AbstractProject<?, ?> project) {
return Collections.singletonList(new DownstreamTasksAction(this));
}
@Extension
public static final class DescriptorImpl extends BuildStepDescriptor<Publisher> {
public String getDisplayName() {
return Messages.BatchTaskInvoker_DisplayName();
}
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
}
}