From ba913ea8008778d0ed01c9eaab5e15423843ad19 Mon Sep 17 00:00:00 2001 From: Daniel Watkins Date: Thu, 5 Oct 2017 14:20:18 -0400 Subject: [PATCH] Add support for unstable return parameter for shell builders This continues to support the current string form for job configuration, as well as adding a form that supports a mapping. This mapping has two keys: "command", containing the command to be executed (i.e. the current string parameter); and "unstable-return", containing the exit code that configures the exit code that should cause a build to be marked unstable. Change-Id: I43ecc883236bbf8fc6de7ed8992e6d90da7d48ac --- jenkins_jobs/modules/builders.py | 24 ++++++++++++++++++- tests/builders/fixtures/shell-mapping.xml | 9 +++++++ tests/builders/fixtures/shell-mapping.yaml | 3 +++ .../fixtures/shell-unstable-return.xml | 9 +++++++ .../fixtures/shell-unstable-return.yaml | 4 ++++ 5 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 tests/builders/fixtures/shell-mapping.xml create mode 100644 tests/builders/fixtures/shell-mapping.yaml create mode 100644 tests/builders/fixtures/shell-unstable-return.xml create mode 100644 tests/builders/fixtures/shell-unstable-return.yaml diff --git a/jenkins_jobs/modules/builders.py b/jenkins_jobs/modules/builders.py index 294f7df66..6c249d3f2 100644 --- a/jenkins_jobs/modules/builders.py +++ b/jenkins_jobs/modules/builders.py @@ -40,6 +40,8 @@ import logging import sys import xml.etree.ElementTree as XML +import six + from jenkins_jobs.errors import is_sequence from jenkins_jobs.errors import InvalidAttributeError from jenkins_jobs.errors import JenkinsJobsException @@ -65,16 +67,36 @@ def shell(registry, xml_parent, data): """yaml: shell Execute a shell command. + There are two ways of configuring the builder, with a plain string to + execute: + :arg str parameter: the shell command to execute + Or with a mapping that allows other parameters to be passed: + + :arg str command: the shell command to execute + :arg int unstable-return: + the shell exit code to interpret as an unstable build result + Example: .. literalinclude:: /../../tests/builders/fixtures/shell.yaml :language: yaml + .. literalinclude:: + /../../tests/builders/fixtures/shell-unstable-return.yaml + :language: yaml """ shell = XML.SubElement(xml_parent, 'hudson.tasks.Shell') - XML.SubElement(shell, 'command').text = data + if isinstance(data, six.string_types): + XML.SubElement(shell, 'command').text = data + else: + mappings = [ + ('command', 'command', None), + ('unstable-return', 'unstableReturn', 0), + + ] + convert_mapping_to_xml(shell, data, mappings, fail_required=True) def python(registry, xml_parent, data): diff --git a/tests/builders/fixtures/shell-mapping.xml b/tests/builders/fixtures/shell-mapping.xml new file mode 100644 index 000000000..ed9383959 --- /dev/null +++ b/tests/builders/fixtures/shell-mapping.xml @@ -0,0 +1,9 @@ + + + + + make test + 0 + + + diff --git a/tests/builders/fixtures/shell-mapping.yaml b/tests/builders/fixtures/shell-mapping.yaml new file mode 100644 index 000000000..a949efe08 --- /dev/null +++ b/tests/builders/fixtures/shell-mapping.yaml @@ -0,0 +1,3 @@ +builders: + - shell: + command: "make test" diff --git a/tests/builders/fixtures/shell-unstable-return.xml b/tests/builders/fixtures/shell-unstable-return.xml new file mode 100644 index 000000000..8daf8f492 --- /dev/null +++ b/tests/builders/fixtures/shell-unstable-return.xml @@ -0,0 +1,9 @@ + + + + + make test + 3 + + + diff --git a/tests/builders/fixtures/shell-unstable-return.yaml b/tests/builders/fixtures/shell-unstable-return.yaml new file mode 100644 index 000000000..6db7c1183 --- /dev/null +++ b/tests/builders/fixtures/shell-unstable-return.yaml @@ -0,0 +1,4 @@ +builders: + - shell: + command: "make test" + unstable-return: 3