From 7b8f989e40bec6327cb354bfb33493860993a455 Mon Sep 17 00:00:00 2001 From: Kien Ha Date: Mon, 20 Jun 2016 18:14:33 -0400 Subject: [PATCH] Update ScriptTrigger plugin - Update to use convert_mapping_to_xml - Add minimal tests - Update docs Change-Id: I803de8b7f9195caac06fa393634c36d799179cc3 Signed-off-by: Kien Ha --- jenkins_jobs/modules/triggers.py | 66 +++++++++++-------- .../triggers/fixtures/groovy-script-full.xml | 15 +++++ .../triggers/fixtures/groovy-script-full.yaml | 9 +++ .../fixtures/groovy-script-minimal.xml | 14 ++++ .../fixtures/groovy-script-minimal.yaml | 2 + tests/triggers/fixtures/groovy-script.xml | 7 +- tests/triggers/fixtures/groovy-script.yaml | 1 - .../{script002.xml => script-full.xml} | 6 +- .../{script002.yaml => script-full.yaml} | 2 +- tests/triggers/fixtures/script-minimal.xml | 13 ++++ tests/triggers/fixtures/script-minimal.yaml | 2 + tests/triggers/fixtures/script001.xml | 7 +- 12 files changed, 107 insertions(+), 37 deletions(-) create mode 100644 tests/triggers/fixtures/groovy-script-full.xml create mode 100644 tests/triggers/fixtures/groovy-script-full.yaml create mode 100644 tests/triggers/fixtures/groovy-script-minimal.xml create mode 100644 tests/triggers/fixtures/groovy-script-minimal.yaml rename tests/triggers/fixtures/{script002.xml => script-full.xml} (74%) rename tests/triggers/fixtures/{script002.yaml => script-full.yaml} (83%) create mode 100644 tests/triggers/fixtures/script-minimal.xml create mode 100644 tests/triggers/fixtures/script-minimal.yaml diff --git a/jenkins_jobs/modules/triggers.py b/jenkins_jobs/modules/triggers.py index 6c01416ee..0da8eb6ba 100644 --- a/jenkins_jobs/modules/triggers.py +++ b/jenkins_jobs/modules/triggers.py @@ -40,6 +40,7 @@ from jenkins_jobs.errors import JenkinsJobsException from jenkins_jobs.errors import MissingAttributeError import jenkins_jobs.modules.base from jenkins_jobs.modules import hudson_model +from jenkins_jobs.modules.helpers import convert_mapping_to_xml logger = logging.getLogger(str(__name__)) @@ -1483,35 +1484,41 @@ def script(parser, xml_parent, data): :arg str label: Restrict where the polling should run. (default '') :arg str script: A shell or batch script. (default '') - :arg str script-file-path: A shell or batch script path. (optional) + :arg str script-file-path: A shell or batch script path. (default '') :arg str cron: cron syntax of when to run (default '') :arg bool enable-concurrent: Enables triggering concurrent builds. (default false) :arg int exit-code: If the exit code of the script execution returns this expected exit code, a build is scheduled. (default 0) - Example: + Full Example: - .. literalinclude:: /../../tests/triggers/fixtures/script001.yaml + .. literalinclude:: /../../tests/triggers/fixtures/script-full.yaml + :language: yaml + + Minimal Example: + + .. literalinclude:: /../../tests/triggers/fixtures/script-minimal.yaml + :language: yaml """ - data = data if data else {} st = XML.SubElement( xml_parent, 'org.jenkinsci.plugins.scripttrigger.ScriptTrigger' ) + st.set('plugin', 'scripttrigger') label = data.get('label') + mappings = [ + ('script', 'script', ''), + ('script-file-path', 'scriptFilePath', ''), + ('cron', 'spec', ''), + ('enable-concurrent', 'enableConcurrentBuild', False), + ('exit-code', 'exitCode', 0) + ] + convert_mapping_to_xml(st, data, mappings, fail_required=True) - XML.SubElement(st, 'script').text = str(data.get('script', '')) - if 'script-file-path' in data: - XML.SubElement(st, 'scriptFilePath').text = str( - data.get('script-file-path')) - XML.SubElement(st, 'spec').text = str(data.get('cron', '')) XML.SubElement(st, 'labelRestriction').text = str(bool(label)).lower() if label: XML.SubElement(st, 'triggerLabel').text = label - XML.SubElement(st, 'enableConcurrentBuild').text = str( - data.get('enable-concurrent', False)).lower() - XML.SubElement(st, 'exitCode').text = str(data.get('exit-code', 0)) def groovy_script(parser, xml_parent, data): @@ -1528,35 +1535,42 @@ def groovy_script(parser, xml_parent, data): evaluated to true, a build is scheduled. (default '') :arg str script-file-path: Groovy script path. (default '') :arg str property-file-path: Property file path. All properties will be set - as parameters for the triggered build. (optional) + as parameters for the triggered build. (default '') :arg bool enable-concurrent: Enable concurrent build. (default false) :arg str label: Restrict where the polling should run. (default '') :arg str cron: cron syntax of when to run (default '') - Example: + Full Example: - .. literalinclude:: /../../tests/triggers/fixtures/groovy-script.yaml + .. literalinclude:: /../../tests/triggers/fixtures/groovy-script-full.yaml + :language: yaml + + Minimal Example: + + .. literalinclude:: + /../../tests/triggers/fixtures/groovy-script-minimal.yaml + :language: yaml """ gst = XML.SubElement( xml_parent, 'org.jenkinsci.plugins.scripttrigger.groovy.GroovyScriptTrigger' ) + gst.set('plugin', 'scripttrigger') + + mappings = [ + ('system-script', 'groovySystemScript', False), + ('script', 'groovyExpression', ''), + ('script-file-path', 'groovyFilePath', ''), + ('property-file-path', 'propertiesFilePath', ''), + ('enable-concurrent', 'enableConcurrentBuild', False), + ('cron', 'spec', ''), + ] + convert_mapping_to_xml(gst, data, mappings, fail_required=True) - XML.SubElement(gst, 'groovySystemScript').text = str( - data.get('system-script', False)).lower() - XML.SubElement(gst, 'groovyExpression').text = str(data.get('script', '')) - XML.SubElement(gst, 'groovyFilePath').text = str(data.get( - 'script-file-path', '')) - if 'property-file-path' in data: - XML.SubElement(gst, 'propertiesFilePath').text = str( - data.get('property-file-path')) - XML.SubElement(gst, 'enableConcurrentBuild').text = str( - data.get('enable-concurrent', False)).lower() label = data.get('label') XML.SubElement(gst, 'labelRestriction').text = str(bool(label)).lower() if label: XML.SubElement(gst, 'triggerLabel').text = label - XML.SubElement(gst, 'spec').text = str(data.get('cron', '')) def rabbitmq(parser, xml_parent, data): diff --git a/tests/triggers/fixtures/groovy-script-full.xml b/tests/triggers/fixtures/groovy-script-full.xml new file mode 100644 index 000000000..bb9543111 --- /dev/null +++ b/tests/triggers/fixtures/groovy-script-full.xml @@ -0,0 +1,15 @@ + + + + + true + groovy-content + path/to/filename + /path/to/properties/file + true + H/15 * * * * + true + master + + + diff --git a/tests/triggers/fixtures/groovy-script-full.yaml b/tests/triggers/fixtures/groovy-script-full.yaml new file mode 100644 index 000000000..c327e1c99 --- /dev/null +++ b/tests/triggers/fixtures/groovy-script-full.yaml @@ -0,0 +1,9 @@ +triggers: + - groovy-script: + script: groovy-content + script-file-path: path/to/filename + property-file-path: /path/to/properties/file + cron: H/15 * * * * + enable-concurrent: true + label: master + system-script: true diff --git a/tests/triggers/fixtures/groovy-script-minimal.xml b/tests/triggers/fixtures/groovy-script-minimal.xml new file mode 100644 index 000000000..92b9e6841 --- /dev/null +++ b/tests/triggers/fixtures/groovy-script-minimal.xml @@ -0,0 +1,14 @@ + + + + + false + + + + false + + false + + + diff --git a/tests/triggers/fixtures/groovy-script-minimal.yaml b/tests/triggers/fixtures/groovy-script-minimal.yaml new file mode 100644 index 000000000..59c0fa6a6 --- /dev/null +++ b/tests/triggers/fixtures/groovy-script-minimal.yaml @@ -0,0 +1,2 @@ +triggers: + - groovy-script diff --git a/tests/triggers/fixtures/groovy-script.xml b/tests/triggers/fixtures/groovy-script.xml index fa8ba54dc..09c97d226 100644 --- a/tests/triggers/fixtures/groovy-script.xml +++ b/tests/triggers/fixtures/groovy-script.xml @@ -1,14 +1,15 @@ - + true path/to/filename + true + H/15 * * * * true master - H/15 * * * * - \ No newline at end of file + diff --git a/tests/triggers/fixtures/groovy-script.yaml b/tests/triggers/fixtures/groovy-script.yaml index efd6a551d..af496a39a 100644 --- a/tests/triggers/fixtures/groovy-script.yaml +++ b/tests/triggers/fixtures/groovy-script.yaml @@ -5,4 +5,3 @@ triggers: enable-concurrent: true label: master system-script: true - diff --git a/tests/triggers/fixtures/script002.xml b/tests/triggers/fixtures/script-full.xml similarity index 74% rename from tests/triggers/fixtures/script002.xml rename to tests/triggers/fixtures/script-full.xml index 46e22bed6..e486e678a 100644 --- a/tests/triggers/fixtures/script002.xml +++ b/tests/triggers/fixtures/script-full.xml @@ -1,14 +1,14 @@ - + $WORKSPACE/scripts H/15 * * * * + true + 0 true master - false - 0 diff --git a/tests/triggers/fixtures/script002.yaml b/tests/triggers/fixtures/script-full.yaml similarity index 83% rename from tests/triggers/fixtures/script002.yaml rename to tests/triggers/fixtures/script-full.yaml index 12b07297b..f7739b00b 100644 --- a/tests/triggers/fixtures/script002.yaml +++ b/tests/triggers/fixtures/script-full.yaml @@ -3,7 +3,7 @@ triggers: script: 'exit 0' script-file-path: '$WORKSPACE/scripts' cron: 'H/15 * * * *' - enable-concurrent: False + enable-concurrent: true label: master exit-code: 0 diff --git a/tests/triggers/fixtures/script-minimal.xml b/tests/triggers/fixtures/script-minimal.xml new file mode 100644 index 000000000..dc0c5464a --- /dev/null +++ b/tests/triggers/fixtures/script-minimal.xml @@ -0,0 +1,13 @@ + + + + + + H/15 * * * * - true - master false 0 + true + master