1818
1919import java .io .FileNotFoundException ;
2020import java .io .IOException ;
21+ import java .util .ArrayList ;
22+ import java .util .Arrays ;
23+ import java .util .List ;
2124
2225import org .json .JSONArray ;
2326import org .json .JSONObject ;
@@ -37,6 +40,15 @@ public class JenkinsXmlData {
3740 private static final String XML_VERSION = "<?xml version='1.1' encoding='UTF-8'?>" ;
3841 private static final String HUDSON_PARAMETERS_DEFINITION_KEY = "hudson.model.ParametersDefinitionProperty" ;
3942 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'" ;
4052
4153 private JSONObject xmlJsonData ;
4254 private JSONObject builders ;
@@ -113,21 +125,52 @@ public JenkinsXmlData addBashScript(String script) {
113125 * @return this JenkinsXmlData
114126 */
115127 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 );
118129
130+ boolean hasKeyGroovy = builders .has (hudsonPluginsGroovyGroovyKey );
119131 if (!hasKeyGroovy ) {
120132 JSONArray hudsonTasksGroovy = new JSONArray ();
121- builders .put (hudsonGroovyKey , hudsonTasksGroovy );
133+ builders .put (hudsonPluginsGroovyGroovyKey , hudsonTasksGroovy );
122134 }
123135
124136 JSONObject newGroovyCommand = BuildGroovyObject (script );
125137
126- builders .getJSONArray (hudsonGroovyKey )
138+ builders .getJSONArray (hudsonPluginsGroovyGroovyKey )
127139 .put (newGroovyCommand );
128140 return this ;
129141 }
130142
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+
131174 /**
132175 * This function adds a parameter key to the job data, the user must specify what type the parameter will receive,
133176 * currently only String.class and boolean.class is supported.
@@ -210,19 +253,18 @@ private void validatePropertiesObject(String parametertypeKey) {
210253 }
211254
212255 /**
213- * This function creates the croovy script structure needed by jenkins.
256+ * This function creates the Groovy script structure needed by jenkins.
214257 *
215258 * @param script
216259 * @return
217260 */
218261 private JSONObject BuildGroovyObject (String script ) {
219262 JSONObject groovyScriptSource = new JSONObject ();
220263 groovyScriptSource .put ("command" , script );
221-
222264 JSONObject groovyScript = new JSONObject ();
223265 groovyScript .put ("scriptParameters" , "" );
224266 groovyScript .put ("javaOpts" , "" );
225- groovyScript .put ("scriptSource class='hudson.plugins.groovy.StringScriptSource'" , groovyScriptSource );
267+ groovyScript .put (scriptSourceforGroovyKey , groovyScriptSource );
226268 groovyScript .put ("classPath" , "" );
227269 groovyScript .put ("groovyName" , "(Default)" );
228270 groovyScript .put ("parameters" , "" );
@@ -231,6 +273,33 @@ private JSONObject BuildGroovyObject(String script) {
231273 return groovyScript ;
232274 }
233275
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+
234303 /**
235304 * This function removes the params given in the groove start node from tne end node
236305 * <abc param='abc'></abc param='def'> becomes <abc param='def'></abc>
@@ -239,13 +308,56 @@ private JSONObject BuildGroovyObject(String script) {
239308 * @return
240309 */
241310 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+
248335 return xmlDataString ;
249336 }
250337
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+
251363}
0 commit comments