Fix system_groovy issue

System groovy has difference with the simple groovy command. System
groovy result XML for system groovy command (script), looks like this:

    <hudson.plugins.groovy.SystemGroovy plugin="groovy@2.3">
      <source class="hudson.plugins.groovy.StringSystemScriptSource">
        <script plugin="script-security@1.75">
          <script>Groovy command</script>
          <sandbox>[true|false]</sandbox>
          <classpath>
            <entry>
              <url>...</url>
            </entry>
            ...
            <entry>
              <url>...</url>
            </entry>
          </classpath>
        </script>
      </source>
      <bindings>...</bindings>
    </hudson.plugins.groovy.SystemGroovy>

Change-Id: I85eb5848de2a817ab132559b53cbeb316931195f
This commit is contained in:
Artem Nikitin 2020-11-13 13:49:28 +03:00 committed by Thanh Ha (zxiiro)
parent 09613c84f7
commit 3abe808991
4 changed files with 38 additions and 16 deletions

View File

@ -1249,13 +1249,15 @@ def system_groovy(registry, xml_parent, data):
:arg str file: Groovy file to run. (Alternative: you can chose a command
instead)
:arg str command: Groovy command to run. (Alternative: you can chose a
:arg str command: Groovy command to run. (Alternative: you can choose a
script file instead)
:arg bool sandbox: Execute script inside of groovy sandbox (>=2.0)
(default false)
:arg str bindings: Define variable bindings (in the properties file
format). Specified variables can be addressed from the script.
(optional)
:arg str class-path: Specify script classpath here. Each line is one class
path item. (optional)
:arg (list str) class-path: List of script class paths.
(optional)
Examples:
@ -1264,12 +1266,25 @@ def system_groovy(registry, xml_parent, data):
.. literalinclude:: ../../tests/builders/fixtures/system-groovy002.yaml
:language: yaml
"""
root_tag = "hudson.plugins.groovy.SystemGroovy"
sysgroovy = XML.SubElement(xml_parent, root_tag)
sysgroovy.append(_groovy_common_scriptSource(data))
mapping = [("bindings", "bindings", ""), ("class-path", "classpath", "")]
if "file" in data:
scriptSource = _groovy_common_scriptSource(data)
if "command" in data:
scriptSource = XML.Element("source")
scriptSource.set("class", "hudson.plugins.groovy.StringSystemScriptSource")
script = XML.SubElement(scriptSource, "script")
mapping = [("command", "script", None), ("sandbox", "sandbox", False)]
helpers.convert_mapping_to_xml(script, data, mapping, fail_required=True)
classpath = XML.SubElement(script, "classpath")
classpaths = data.get("class-path", [])
if isinstance(classpaths, str):
classpaths = [classpaths]
for path in classpaths:
entry = XML.SubElement(classpath, "entry")
XML.SubElement(entry, "url").text = path
sysgroovy.append(scriptSource)
mapping = [("bindings", "bindings", "")]
helpers.convert_mapping_to_xml(sysgroovy, data, mapping, fail_required=True)

View File

@ -6,7 +6,6 @@
<scriptFile>test.groovy</scriptFile>
</scriptSource>
<bindings/>
<classpath/>
</hudson.plugins.groovy.SystemGroovy>
</builders>
</project>

View File

@ -2,11 +2,18 @@
<project>
<builders>
<hudson.plugins.groovy.SystemGroovy>
<scriptSource class="hudson.plugins.groovy.StringScriptSource">
<command>Some command</command>
</scriptSource>
<bindings>Some bindings</bindings>
<classpath>Some classpath</classpath>
<source class="hudson.plugins.groovy.StringSystemScriptSource">
<script>
<script>println 'Hello'</script>
<sandbox>true</sandbox>
<classpath>
<entry>
<url>file:/home/user/example.jar</url>
</entry>
</classpath>
</script>
</source>
<bindings>EXAMPLE=foo-bar</bindings>
</hudson.plugins.groovy.SystemGroovy>
</builders>
</project>

View File

@ -1,5 +1,6 @@
builders:
- system-groovy:
command: "Some command"
bindings: "Some bindings"
class-path: "Some classpath"
command: "println 'Hello'"
bindings: "EXAMPLE=foo-bar"
class-path: "file:/home/user/example.jar"
sandbox: true