-
Notifications
You must be signed in to change notification settings - Fork 10
Add system groovy #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 3 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -18,6 +18,9 @@ | |
|
|
||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
|
|
||
| import org.json.JSONArray; | ||
| import org.json.JSONObject; | ||
|
|
@@ -37,6 +40,15 @@ public class JenkinsXmlData { | |
| private static final String XML_VERSION = "<?xml version='1.1' encoding='UTF-8'?>"; | ||
| private static final String HUDSON_PARAMETERS_DEFINITION_KEY = "hudson.model.ParametersDefinitionProperty"; | ||
| private static final String PARAMETER_DEFINITION_KEY = "parameterDefinitions"; | ||
| private static final String GROOVY_SCRIPT_SCRIPT_PLUGIN_VERSION = "2.1"; | ||
| private static final String GROOVY_SCRIPT_SECURITY_PLUGIN_VERSION = "1.51"; | ||
|
|
||
| private String hudsonPluginsGroovyGroovyKey = "hudson.plugins.groovy.Groovy plugin='groovy@%s'"; | ||
| private String hudsonPluginGroovySystemGroovyKey = "hudson.plugins.groovy.SystemGroovy plugin='groovy@%s'"; | ||
| private String scriptPluginForScriptSecurityKey = "script plugin='script-security@%s'"; | ||
|
|
||
| private String sourceForGroovyKey = "source class='hudson.plugins.groovy.StringSystemScriptSource'"; | ||
| private String scriptSourceforGroovyKey = "scriptSource class='hudson.plugins.groovy.StringScriptSource'"; | ||
|
|
||
| private JSONObject xmlJsonData; | ||
| private JSONObject builders; | ||
|
|
@@ -113,21 +125,52 @@ public JenkinsXmlData addBashScript(String script) { | |
| * @return this JenkinsXmlData | ||
| */ | ||
| public JenkinsXmlData addGrovyScript(String script) { | ||
| String hudsonGroovyKey = "hudson.plugins.groovy.Groovy plugin='groovy@2.1'"; | ||
| boolean hasKeyGroovy = builders.has(hudsonGroovyKey); | ||
| hudsonPluginsGroovyGroovyKey = String.format(hudsonPluginsGroovyGroovyKey, GROOVY_SCRIPT_SCRIPT_PLUGIN_VERSION); | ||
|
||
|
|
||
| boolean hasKeyGroovy = builders.has(hudsonPluginsGroovyGroovyKey); | ||
| if (!hasKeyGroovy) { | ||
| JSONArray hudsonTasksGroovy = new JSONArray(); | ||
| builders.put(hudsonGroovyKey, hudsonTasksGroovy); | ||
| builders.put(hudsonPluginsGroovyGroovyKey, hudsonTasksGroovy); | ||
| } | ||
|
|
||
| JSONObject newGroovyCommand = BuildGroovyObject(script); | ||
|
|
||
| builders.getJSONArray(hudsonGroovyKey) | ||
| builders.getJSONArray(hudsonPluginsGroovyGroovyKey) | ||
| .put(newGroovyCommand); | ||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * This function adds a system groovy script to the XML data. | ||
| * Only one system groovy script may be added to a single jenkins job at the moment. | ||
| * | ||
| * @param script | ||
| * @param sandbox | ||
| * @return this JenkinsXmlData | ||
| * @throws Exception | ||
| */ | ||
| public JenkinsXmlData addSystemGrovyScript(String script, boolean sandbox) throws Exception { | ||
| hudsonPluginGroovySystemGroovyKey = String.format(hudsonPluginGroovySystemGroovyKey, | ||
| GROOVY_SCRIPT_SCRIPT_PLUGIN_VERSION); | ||
|
||
|
|
||
| boolean hasKeyGroovy = builders.has(hudsonPluginGroovySystemGroovyKey); | ||
| if (!hasKeyGroovy) { | ||
| JSONArray hudsonSystemGroovy = new JSONArray(); | ||
| builders.put(hudsonPluginGroovySystemGroovyKey, hudsonSystemGroovy); | ||
| } else { | ||
| throw new Exception("Currently only one system Groovy script supported."); | ||
| } | ||
| JSONObject systemGroovyScript = buildSystemGroovyObject(script, sandbox); | ||
|
|
||
| JSONArray systemGroovyContainer = new JSONArray(); | ||
| systemGroovyContainer.put(systemGroovyScript); | ||
|
|
||
| builders.getJSONArray(hudsonPluginGroovySystemGroovyKey) | ||
| .put(systemGroovyScript); | ||
|
|
||
| return this; | ||
| } | ||
|
|
||
| /** | ||
| * This function adds a parameter key to the job data, the user must specify what type the parameter will receive, | ||
| * currently only String.class and boolean.class is supported. | ||
|
|
@@ -210,19 +253,18 @@ private void validatePropertiesObject(String parametertypeKey) { | |
| } | ||
|
|
||
| /** | ||
| * This function creates the croovy script structure needed by jenkins. | ||
| * This function creates the Groovy script structure needed by jenkins. | ||
| * | ||
| * @param script | ||
| * @return | ||
| */ | ||
| private JSONObject BuildGroovyObject(String script) { | ||
| JSONObject groovyScriptSource = new JSONObject(); | ||
| groovyScriptSource.put("command", script); | ||
|
|
||
| JSONObject groovyScript = new JSONObject(); | ||
| groovyScript.put("scriptParameters", ""); | ||
| groovyScript.put("javaOpts", ""); | ||
| groovyScript.put("scriptSource class='hudson.plugins.groovy.StringScriptSource'", groovyScriptSource); | ||
| groovyScript.put(scriptSourceforGroovyKey, groovyScriptSource); | ||
| groovyScript.put("classPath", ""); | ||
| groovyScript.put("groovyName", "(Default)"); | ||
| groovyScript.put("parameters", ""); | ||
|
|
@@ -231,6 +273,33 @@ private JSONObject BuildGroovyObject(String script) { | |
| return groovyScript; | ||
| } | ||
|
|
||
| /** | ||
| * This function creates the System Groovy script structure needed by jenkins. | ||
| * | ||
| * @param script | ||
| * @param sandbox | ||
| * @return | ||
| */ | ||
| private JSONObject buildSystemGroovyObject(String script, boolean sandbox) { | ||
| scriptPluginForScriptSecurityKey = String.format(scriptPluginForScriptSecurityKey, | ||
| GROOVY_SCRIPT_SECURITY_PLUGIN_VERSION); | ||
|
|
||
| JSONObject systemGroovyScriptValues = new JSONObject(); | ||
| systemGroovyScriptValues.put("script", script); | ||
| systemGroovyScriptValues.put("sandbox", sandbox); | ||
|
|
||
| JSONArray systemGroovyScriptArray = new JSONArray(); | ||
| systemGroovyScriptArray.put(systemGroovyScriptValues); | ||
|
|
||
| JSONObject systemGroovyScriptObject = new JSONObject(); | ||
| systemGroovyScriptObject.put(scriptPluginForScriptSecurityKey, systemGroovyScriptArray); | ||
|
|
||
| JSONObject systemGroovySource = new JSONObject(); | ||
| systemGroovySource.put(sourceForGroovyKey, systemGroovyScriptObject); | ||
|
|
||
| return systemGroovySource; | ||
| } | ||
|
|
||
| /** | ||
| * This function removes the params given in the groove start node from tne end node | ||
| * <abc param='abc'></abc param='def'> becomes <abc param='def'></abc> | ||
|
|
@@ -239,13 +308,56 @@ private JSONObject BuildGroovyObject(String script) { | |
| * @return | ||
| */ | ||
| private String removeExtraGroovyParams(String xmlDataString) { | ||
| xmlDataString = xmlDataString.replaceAll( | ||
| "\\<\\/hudson\\.plugins\\.groovy\\.Groovy\\ plugin\\=\\'groovy\\@2\\.1\\'", | ||
| "</hudson.plugins.groovy.Groovy>"); | ||
| xmlDataString = xmlDataString.replaceAll( | ||
| "\\<\\/scriptSource\\ class\\=\\'hudson\\.plugins\\.groovy\\.StringScriptSource\\'\\>", | ||
| "</scriptSource>"); | ||
| // Groovy script keys | ||
| String hudsonPluginsGroovyGroovyKeyEnd = convertToEndNode(hudsonPluginsGroovyGroovyKey); | ||
| String scriptSourceforGroovyKeyEnd = convertToEndNode(scriptSourceforGroovyKey); | ||
|
|
||
| hudsonPluginsGroovyGroovyKeyEnd = regexify(hudsonPluginsGroovyGroovyKeyEnd); | ||
| scriptSourceforGroovyKeyEnd = regexify(scriptSourceforGroovyKeyEnd); | ||
|
|
||
| // System groovy script keys | ||
| String hudsonSystemGroovyKeyEnd = convertToEndNode(hudsonPluginGroovySystemGroovyKey); | ||
| String systemGroovyScriptKeyEnd = convertToEndNode(scriptPluginForScriptSecurityKey); | ||
| String sourceForGroovyKeyEnd = convertToEndNode(sourceForGroovyKey); | ||
|
|
||
| hudsonSystemGroovyKeyEnd = regexify(hudsonSystemGroovyKeyEnd); | ||
| systemGroovyScriptKeyEnd = regexify(systemGroovyScriptKeyEnd); | ||
| sourceForGroovyKeyEnd = regexify(sourceForGroovyKeyEnd); | ||
|
|
||
| // String replace section | ||
| xmlDataString = xmlDataString.replaceAll(hudsonPluginsGroovyGroovyKeyEnd, "</hudson.plugins.groovy.Groovy>"); | ||
| xmlDataString = xmlDataString.replaceAll(scriptSourceforGroovyKeyEnd, "</scriptSource>"); | ||
|
|
||
| xmlDataString = xmlDataString.replaceAll(hudsonSystemGroovyKeyEnd, "</hudson.plugins.groovy.SystemGroovy>"); | ||
| xmlDataString = xmlDataString.replaceAll(sourceForGroovyKeyEnd, "</source>"); | ||
| xmlDataString = xmlDataString.replaceAll(systemGroovyScriptKeyEnd, "</script>"); | ||
|
|
||
| return xmlDataString; | ||
| } | ||
|
|
||
| /** | ||
| * Function that adds </ and > to a string | ||
| * | ||
| * @param string | ||
| * @return | ||
| */ | ||
| private String convertToEndNode(String string) { | ||
| return "</" + string + ">"; | ||
| } | ||
|
|
||
| /** | ||
| * Function that makes a string usable as regex. | ||
| * | ||
| * @param string | ||
| * @return | ||
| */ | ||
| private String regexify(String string) { | ||
| String backSlash = "\\\\"; | ||
| List<String> charsToRegexify = Arrays.asList("'", "\\.", " ", "=", "/", "<", ">"); | ||
| for (String charcters : charsToRegexify) { | ||
| string = string.replaceAll(charcters, backSlash + charcters); | ||
| } | ||
| return string; | ||
| } | ||
|
|
||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why groovy two times in the name?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
it reflects the xml node name, it has groovy 2 times.