18
18
19
19
import java .io .FileNotFoundException ;
20
20
import java .io .IOException ;
21
+ import java .util .ArrayList ;
22
+ import java .util .Arrays ;
23
+ import java .util .List ;
21
24
22
25
import org .json .JSONArray ;
23
26
import org .json .JSONObject ;
@@ -37,6 +40,15 @@ public class JenkinsXmlData {
37
40
private static final String XML_VERSION = "<?xml version='1.1' encoding='UTF-8'?>" ;
38
41
private static final String HUDSON_PARAMETERS_DEFINITION_KEY = "hudson.model.ParametersDefinitionProperty" ;
39
42
private static final String PARAMETER_DEFINITION_KEY = "parameterDefinitions" ;
43
+ private static final String GROOVY_SCRIPT_PLUGIN_VERSION = "2.1" ;
44
+ private static final String GROOVY_SCRIPT_SECURITY_PLUGIN_VERSION = "1.51" ;
45
+
46
+ private String hudsonPluginsGroovyGroovyKey = "hudson.plugins.groovy.Groovy plugin='groovy@%s'" ;
47
+ private String hudsonPluginGroovySystemGroovyKey = "hudson.plugins.groovy.SystemGroovy plugin='groovy@%s'" ;
48
+ private String scriptPluginForScriptSecurityKey = "script plugin='script-security@%s'" ;
49
+
50
+ private String sourceForGroovyKey = "source class='hudson.plugins.groovy.StringSystemScriptSource'" ;
51
+ private String scriptSourceforGroovyKey = "scriptSource class='hudson.plugins.groovy.StringScriptSource'" ;
40
52
41
53
private JSONObject xmlJsonData ;
42
54
private JSONObject builders ;
@@ -113,21 +125,52 @@ public JenkinsXmlData addBashScript(String script) {
113
125
* @return this JenkinsXmlData
114
126
*/
115
127
public JenkinsXmlData addGrovyScript (String script ) {
116
- String hudsonGroovyKey = "hudson.plugins.groovy.Groovy plugin='groovy@2.1'" ;
117
- boolean hasKeyGroovy = builders .has (hudsonGroovyKey );
128
+ hudsonPluginsGroovyGroovyKey = String .format (hudsonPluginsGroovyGroovyKey , GROOVY_SCRIPT_PLUGIN_VERSION );
118
129
130
+ boolean hasKeyGroovy = builders .has (hudsonPluginsGroovyGroovyKey );
119
131
if (!hasKeyGroovy ) {
120
132
JSONArray hudsonTasksGroovy = new JSONArray ();
121
- builders .put (hudsonGroovyKey , hudsonTasksGroovy );
133
+ builders .put (hudsonPluginsGroovyGroovyKey , hudsonTasksGroovy );
122
134
}
123
135
124
136
JSONObject newGroovyCommand = BuildGroovyObject (script );
125
137
126
- builders .getJSONArray (hudsonGroovyKey )
138
+ builders .getJSONArray (hudsonPluginsGroovyGroovyKey )
127
139
.put (newGroovyCommand );
128
140
return this ;
129
141
}
130
142
143
+ /**
144
+ * This function adds a system groovy script to the XML data.
145
+ * Only one system groovy script may be added to a single jenkins job at the moment.
146
+ *
147
+ * @param script
148
+ * @param sandbox
149
+ * @return this JenkinsXmlData
150
+ * @throws Exception
151
+ */
152
+ public JenkinsXmlData addSystemGrovyScript (String script , boolean sandbox ) throws Exception {
153
+ hudsonPluginGroovySystemGroovyKey = String .format (hudsonPluginGroovySystemGroovyKey ,
154
+ GROOVY_SCRIPT_PLUGIN_VERSION );
155
+
156
+ boolean hasKeyGroovy = builders .has (hudsonPluginGroovySystemGroovyKey );
157
+ if (!hasKeyGroovy ) {
158
+ JSONArray hudsonSystemGroovy = new JSONArray ();
159
+ builders .put (hudsonPluginGroovySystemGroovyKey , hudsonSystemGroovy );
160
+ } else {
161
+ throw new Exception ("Currently only one system Groovy script supported." );
162
+ }
163
+ JSONObject systemGroovyScript = buildSystemGroovyObject (script , sandbox );
164
+
165
+ JSONArray systemGroovyContainer = new JSONArray ();
166
+ systemGroovyContainer .put (systemGroovyScript );
167
+
168
+ builders .getJSONArray (hudsonPluginGroovySystemGroovyKey )
169
+ .put (systemGroovyScript );
170
+
171
+ return this ;
172
+ }
173
+
131
174
/**
132
175
* This function adds a parameter key to the job data, the user must specify what type the parameter will receive,
133
176
* currently only String.class and boolean.class is supported.
@@ -210,19 +253,18 @@ private void validatePropertiesObject(String parametertypeKey) {
210
253
}
211
254
212
255
/**
213
- * This function creates the croovy script structure needed by jenkins.
256
+ * This function creates the Groovy script structure needed by jenkins.
214
257
*
215
258
* @param script
216
259
* @return
217
260
*/
218
261
private JSONObject BuildGroovyObject (String script ) {
219
262
JSONObject groovyScriptSource = new JSONObject ();
220
263
groovyScriptSource .put ("command" , script );
221
-
222
264
JSONObject groovyScript = new JSONObject ();
223
265
groovyScript .put ("scriptParameters" , "" );
224
266
groovyScript .put ("javaOpts" , "" );
225
- groovyScript .put ("scriptSource class='hudson.plugins.groovy.StringScriptSource'" , groovyScriptSource );
267
+ groovyScript .put (scriptSourceforGroovyKey , groovyScriptSource );
226
268
groovyScript .put ("classPath" , "" );
227
269
groovyScript .put ("groovyName" , "(Default)" );
228
270
groovyScript .put ("parameters" , "" );
@@ -231,6 +273,33 @@ private JSONObject BuildGroovyObject(String script) {
231
273
return groovyScript ;
232
274
}
233
275
276
+ /**
277
+ * This function creates the System Groovy script structure needed by jenkins.
278
+ *
279
+ * @param script
280
+ * @param sandbox
281
+ * @return
282
+ */
283
+ private JSONObject buildSystemGroovyObject (String script , boolean sandbox ) {
284
+ scriptPluginForScriptSecurityKey = String .format (scriptPluginForScriptSecurityKey ,
285
+ GROOVY_SCRIPT_SECURITY_PLUGIN_VERSION );
286
+
287
+ JSONObject systemGroovyScriptValues = new JSONObject ();
288
+ systemGroovyScriptValues .put ("script" , script );
289
+ systemGroovyScriptValues .put ("sandbox" , sandbox );
290
+
291
+ JSONArray systemGroovyScriptArray = new JSONArray ();
292
+ systemGroovyScriptArray .put (systemGroovyScriptValues );
293
+
294
+ JSONObject systemGroovyScriptObject = new JSONObject ();
295
+ systemGroovyScriptObject .put (scriptPluginForScriptSecurityKey , systemGroovyScriptArray );
296
+
297
+ JSONObject systemGroovySource = new JSONObject ();
298
+ systemGroovySource .put (sourceForGroovyKey , systemGroovyScriptObject );
299
+
300
+ return systemGroovySource ;
301
+ }
302
+
234
303
/**
235
304
* This function removes the params given in the groove start node from tne end node
236
305
* <abc param='abc'></abc param='def'> becomes <abc param='def'></abc>
@@ -239,13 +308,56 @@ private JSONObject BuildGroovyObject(String script) {
239
308
* @return
240
309
*/
241
310
private String removeExtraGroovyParams (String xmlDataString ) {
242
- xmlDataString = xmlDataString .replaceAll (
243
- "\\ <\\ /hudson\\ .plugins\\ .groovy\\ .Groovy\\ plugin\\ =\\ 'groovy\\ @2\\ .1\\ '" ,
244
- "</hudson.plugins.groovy.Groovy>" );
245
- xmlDataString = xmlDataString .replaceAll (
246
- "\\ <\\ /scriptSource\\ class\\ =\\ 'hudson\\ .plugins\\ .groovy\\ .StringScriptSource\\ '\\ >" ,
247
- "</scriptSource>" );
311
+ // Groovy script keys
312
+ String hudsonPluginsGroovyGroovyKeyEnd = convertToEndNode (hudsonPluginsGroovyGroovyKey );
313
+ String scriptSourceforGroovyKeyEnd = convertToEndNode (scriptSourceforGroovyKey );
314
+
315
+ hudsonPluginsGroovyGroovyKeyEnd = regexify (hudsonPluginsGroovyGroovyKeyEnd );
316
+ scriptSourceforGroovyKeyEnd = regexify (scriptSourceforGroovyKeyEnd );
317
+
318
+ // System groovy script keys
319
+ String hudsonSystemGroovyKeyEnd = convertToEndNode (hudsonPluginGroovySystemGroovyKey );
320
+ String systemGroovyScriptKeyEnd = convertToEndNode (scriptPluginForScriptSecurityKey );
321
+ String sourceForGroovyKeyEnd = convertToEndNode (sourceForGroovyKey );
322
+
323
+ hudsonSystemGroovyKeyEnd = regexify (hudsonSystemGroovyKeyEnd );
324
+ systemGroovyScriptKeyEnd = regexify (systemGroovyScriptKeyEnd );
325
+ sourceForGroovyKeyEnd = regexify (sourceForGroovyKeyEnd );
326
+
327
+ // String replace section
328
+ xmlDataString = xmlDataString .replaceAll (hudsonPluginsGroovyGroovyKeyEnd , "</hudson.plugins.groovy.Groovy>" );
329
+ xmlDataString = xmlDataString .replaceAll (scriptSourceforGroovyKeyEnd , "</scriptSource>" );
330
+
331
+ xmlDataString = xmlDataString .replaceAll (hudsonSystemGroovyKeyEnd , "</hudson.plugins.groovy.SystemGroovy>" );
332
+ xmlDataString = xmlDataString .replaceAll (sourceForGroovyKeyEnd , "</source>" );
333
+ xmlDataString = xmlDataString .replaceAll (systemGroovyScriptKeyEnd , "</script>" );
334
+
248
335
return xmlDataString ;
249
336
}
250
337
338
+ /**
339
+ * Function that adds </ and > to a string
340
+ *
341
+ * @param string
342
+ * @return
343
+ */
344
+ private String convertToEndNode (String string ) {
345
+ return "</" + string + ">" ;
346
+ }
347
+
348
+ /**
349
+ * Function that makes a string usable as regex.
350
+ *
351
+ * @param string
352
+ * @return
353
+ */
354
+ private String regexify (String string ) {
355
+ String backSlash = "\\ \\ " ;
356
+ List <String > charsToRegexify = Arrays .asList ("'" , "\\ ." , " " , "=" , "/" , "<" , ">" );
357
+ for (String charcters : charsToRegexify ) {
358
+ string = string .replaceAll (charcters , backSlash + charcters );
359
+ }
360
+ return string ;
361
+ }
362
+
251
363
}
0 commit comments