/
ModelASTPipelineDef.java
350 lines (309 loc) · 11 KB
/
ModelASTPipelineDef.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
package org.jenkinsci.plugins.pipeline.modeldefinition.ast;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.plugins.pipeline.modeldefinition.validator.ModelValidator;
/**
* Represents the parsed pipeline definition for visual pipeline editor. Corresponds to {@code Root}.
*
* @author Kohsuke Kawaguchi
* @author Andrew Bayer
*/
public final class ModelASTPipelineDef extends ModelASTElement {
private ModelASTStages stages;
private ModelASTPostBuild postBuild;
private ModelASTEnvironment environment;
private ModelASTAgent agent;
private ModelASTTools tools;
private ModelASTJobProperties jobProperties;
private ModelASTBuildParameters parameters;
private ModelASTTriggers triggers;
private ModelASTWrappers wrappers;
public ModelASTPipelineDef(Object sourceLocation) {
super(sourceLocation);
}
@Override
public JSONObject toJSON() {
JSONObject a = new JSONObject();
a.put("stages", stages != null ? stages.toJSON() : null);
a.put("post", postBuild != null ? postBuild.toJSON() : null);
a.put("environment", environment != null ? environment.toJSON() : null);
a.put("agent", agent != null ? agent.toJSON() : null);
a.put("tools", tools != null ? tools.toJSON() : null);
if (jobProperties != null && !jobProperties.getProperties().isEmpty()) {
a.put("jobProperties", jobProperties.toJSON());
} else {
a.put("jobProperties", null);
}
if (parameters != null && !parameters.getParameters().isEmpty()) {
a.put("parameters", parameters.toJSON());
} else {
a.put("parameters", null);
}
if (triggers != null && !triggers.getTriggers().isEmpty()) {
a.put("triggers", triggers.toJSON());
} else {
a.put("triggers", null);
}
if (wrappers != null && !wrappers.getWrappers().isEmpty()) {
a.put("wrappers", wrappers.toJSON());
} else {
a.put("wrappers", null);
}
return new JSONObject().accumulate("pipeline", a);
}
@Override
public void validate(ModelValidator validator) {
validator.validateElement(this);
if (stages != null) {
stages.validate(validator);
}
if (postBuild != null) {
postBuild.validate(validator);
}
if (environment != null) {
environment.validate(validator);
}
if (agent != null) {
agent.validate(validator);
}
if (tools != null) {
tools.validate(validator);
}
if (jobProperties != null) {
jobProperties.validate(validator);
}
if (parameters != null) {
parameters.validate(validator);
}
if (triggers != null) {
triggers.validate(validator);
}
if (wrappers != null) {
wrappers.validate(validator);
}
}
@Override
public String toGroovy() {
StringBuilder result = new StringBuilder();
result.append("pipeline {\n");
if (agent != null) {
result.append(agent.toGroovy()).append('\n');
}
if (stages != null) {
result.append(stages.toGroovy()).append('\n');
}
if (tools != null) {
result.append(tools.toGroovy()).append('\n');
}
if (environment != null) {
result.append(environment.toGroovy()).append('\n');
}
if (postBuild != null) {
result.append(postBuild.toGroovy()).append('\n');
}
if (jobProperties != null && !jobProperties.getProperties().isEmpty()) {
result.append(jobProperties.toGroovy()).append('\n');
}
if (parameters != null && !parameters.getParameters().isEmpty()) {
result.append(parameters.toGroovy()).append('\n');
}
if (triggers != null && !triggers.getTriggers().isEmpty()) {
result.append(triggers.toGroovy()).append('\n');
}
if (wrappers != null && !wrappers.getWrappers().isEmpty()) {
result.append(wrappers.toGroovy()).append('\n');
}
result.append("}\n");
return result.toString();
}
/**
* Helper method to pretty-print the generated Groovy from this and its children.
*
* @return An indented string of Groovy, suitable for use in a Jenkinsfile.
*/
public String toPrettyGroovy() {
StringBuilder result = new StringBuilder();
int indentCount = 0;
boolean tripleSingleQuotedString = false;
boolean first = true;
for (String r : toGroovy().split("\n")) {
if (first) {
first = false;
} else {
result.append('\n');
}
if (tripleSingleQuotedString) {
result.append(r);
} else {
if (r.startsWith("}") || r.startsWith(")") || r.startsWith("]")) {
indentCount--;
}
result.append(indent(indentCount)).append(r);
if (r.endsWith("{") || r.endsWith("(") || r.endsWith("[")) {
indentCount++;
}
}
int index = r.indexOf("\'\'\'");
while (index != -1) {
tripleSingleQuotedString = !tripleSingleQuotedString;
index = r.indexOf("\'\'\'", index + 3);
}
}
return result.toString();
}
@Override
public void removeSourceLocation() {
super.removeSourceLocation();
if (stages != null) {
stages.removeSourceLocation();
}
if (postBuild != null) {
postBuild.removeSourceLocation();
}
if (environment != null) {
environment.removeSourceLocation();
}
if (tools != null) {
tools.removeSourceLocation();
}
if (jobProperties != null) {
jobProperties.removeSourceLocation();
}
if (parameters != null) {
parameters.removeSourceLocation();
}
if (triggers != null) {
triggers.removeSourceLocation();
}
if (wrappers != null) {
wrappers.removeSourceLocation();
}
}
private String indent(int count) {
return StringUtils.repeat(" ", count);
}
public ModelASTStages getStages() {
return stages;
}
public void setStages(ModelASTStages stages) {
this.stages = stages;
}
public ModelASTPostBuild getPostBuild() {
return postBuild;
}
public void setPostBuild(ModelASTPostBuild postBuild) {
this.postBuild = postBuild;
}
public ModelASTEnvironment getEnvironment() {
return environment;
}
public void setEnvironment(ModelASTEnvironment environment) {
this.environment = environment;
}
public ModelASTAgent getAgent() {
return agent;
}
public void setAgent(ModelASTAgent agent) {
this.agent = agent;
}
public ModelASTTools getTools() {
return tools;
}
public void setTools(ModelASTTools tools) {
this.tools = tools;
}
public ModelASTJobProperties getJobProperties() {
return jobProperties;
}
public void setJobProperties(ModelASTJobProperties jobProperties) {
this.jobProperties = jobProperties;
}
public ModelASTBuildParameters getParameters() {
return parameters;
}
public void setParameters(ModelASTBuildParameters parameters) {
this.parameters = parameters;
}
public ModelASTTriggers getTriggers() {
return triggers;
}
public void setTriggers(ModelASTTriggers triggers) {
this.triggers = triggers;
}
public ModelASTWrappers getWrappers() {
return wrappers;
}
public void setWrappers(ModelASTWrappers wrappers) {
this.wrappers = wrappers;
}
@Override
public String toString() {
return "ModelASTPipelineDef{" +
"stages=" + stages +
", post=" + postBuild +
", environment=" + environment +
", agent=" + agent +
", tools=" + tools +
", jobProperties=" + jobProperties +
", parameters=" + parameters +
", triggers=" + triggers +
", wrappers=" + wrappers +
"}";
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (o == null || getClass() != o.getClass()) {
return false;
}
if (!super.equals(o)) {
return false;
}
ModelASTPipelineDef that = (ModelASTPipelineDef) o;
if (getStages() != null ? !getStages().equals(that.getStages()) : that.getStages() != null) {
return false;
}
if (getPostBuild() != null ? !getPostBuild().equals(that.getPostBuild()) : that.getPostBuild() != null) {
return false;
}
if (getEnvironment() != null
? !getEnvironment().equals(that.getEnvironment())
: that.getEnvironment() != null) {
return false;
}
if (getAgent() != null ? !getAgent().equals(that.getAgent()) : that.getAgent() != null) {
return false;
}
if (getTools() != null ? !getTools().equals(that.getTools()) : that.getTools() != null) {
return false;
}
if (getJobProperties() != null
? !getJobProperties().equals(that.getJobProperties())
: that.getJobProperties() != null) {
return false;
}
if (getParameters() != null ? !getParameters().equals(that.getParameters()) : that.getParameters() != null) {
return false;
}
if (getTriggers() != null ? !getTriggers().equals(that.getTriggers()) : that.getTriggers() != null) {
return false;
}
return getWrappers() != null ? getWrappers().equals(that.getWrappers()) : that.getWrappers() == null;
}
@Override
public int hashCode() {
int result = super.hashCode();
result = 31 * result + (getStages() != null ? getStages().hashCode() : 0);
result = 31 * result + (getPostBuild() != null ? getPostBuild().hashCode() : 0);
result = 31 * result + (getEnvironment() != null ? getEnvironment().hashCode() : 0);
result = 31 * result + (getAgent() != null ? getAgent().hashCode() : 0);
result = 31 * result + (getTools() != null ? getTools().hashCode() : 0);
result = 31 * result + (getJobProperties() != null ? getJobProperties().hashCode() : 0);
result = 31 * result + (getParameters() != null ? getParameters().hashCode() : 0);
result = 31 * result + (getTriggers() != null ? getTriggers().hashCode() : 0);
result = 31 * result + (getWrappers() != null ? getWrappers().hashCode() : 0);
return result;
}
}