From f810d8bc308a8d5234e183012725bf3a47399e27 Mon Sep 17 00:00:00 2001 From: Igor Lakhtenkov Date: Wed, 7 Apr 2021 01:33:01 +0300 Subject: [PATCH] Added support of Active Choice Reactive Parameter Change-Id: I25e926603682db543658aad82efb6eb41d1b5cb4 Task: #42221 --- jenkins_jobs/modules/parameters.py | 81 +++++++++++++++++-- .../active-choices-reactive-param001.xml | 41 ++++++++++ .../active-choices-reactive-param001.yaml | 5 ++ .../active-choices-reactive-param002.xml | 63 +++++++++++++++ .../active-choices-reactive-param002.yaml | 30 +++++++ 5 files changed, 215 insertions(+), 5 deletions(-) create mode 100644 tests/yamlparser/fixtures/active-choices-reactive-param001.xml create mode 100644 tests/yamlparser/fixtures/active-choices-reactive-param001.yaml create mode 100644 tests/yamlparser/fixtures/active-choices-reactive-param002.xml create mode 100644 tests/yamlparser/fixtures/active-choices-reactive-param002.yaml diff --git a/jenkins_jobs/modules/parameters.py b/jenkins_jobs/modules/parameters.py index 057c89faf..3a6c39b1e 100644 --- a/jenkins_jobs/modules/parameters.py +++ b/jenkins_jobs/modules/parameters.py @@ -1249,6 +1249,73 @@ def active_choices_param(registry, xml_parent, data): element_name = "org.biouno.unochoice.ChoiceParameter" pdef = XML.SubElement(xml_parent, element_name) + __active_choice_param_internal(pdef, data) + + +def active_choices_reactive_param(registry, xml_parent, data): + """yaml: active-choices-reactive + Active Choices Reactive Parameter + + Requires the Jenkins :jenkins-wiki:`Active Choices Plug-in + `. + + :arg str name: Name of the parameter (required). + :arg str description: Description of the parameter. + :arg list script: Use a Groovy script to define the parameter. + + :Parameter: * **groovy** (`str`) Groovy DSL Script + * **use-groovy-sandbox** (`bool`) To run this + Groovy script in a sandbox with limited abilities + (default True) + * **script-additional-classpath** (`list`) Additional + classpath entries accessible from the script. + :arg list fallback-script: Use a Fallback script. If the script + (specified above) fails, the fallback script will be used as a fallback. + + :Parameter: * **groovy** (`str`) Groovy DSL Script + * **use-groovy-sandbox** (`bool`) To run this Groovy + script in a sandbox with limited abilities. + (default True) + * **script-additional-classpath** (`list`) Additional + classpath entries accessible from the script. + :arg bool enable-filters: If enabled a text box will appear next to + this element and will permit the user to filter its entries. The + list values never get re-evaluated (default False). + :arg int filter-starts-at: How many characters a user must enter + before the filter is applied (default 1). + :arg str choice-type: type of the choices. (default 'single-select') + + :Allowed Values: * **single-select** + * **multi-select** + * **radio-buttons** + * **checkboxes** + :arg str referenced-parameters: Comma separated list of other job + parameters referenced in the uno-choice script + + Minimal Example: + + .. literalinclude:: + /../../tests/yamlparser/fixtures/active-choices-reactive-param001.yaml + :language: yaml + + Full Example: + + .. literalinclude:: + /../../tests/yamlparser/fixtures/active-choices-reactive-param002.yaml + :language: yaml + """ + + element_name = "org.biouno.unochoice.CascadeChoiceParameter" + pdef = XML.SubElement(xml_parent, element_name) + + __active_choice_param_internal(pdef, data) + + mapping = [("referenced-parameters", "referencedParameters", "")] + helpers.convert_mapping_to_xml(pdef, data, mapping, fail_required=False) + XML.SubElement(pdef, "parameters", {"class": "linked-hash-map"}) + + +def __active_choice_param_internal(xml_parent, data): valid_choice_types_dict = { "single-select": "PT_SINGLE_SELECT", "multi-select": "PT_MULTI_SELECT", @@ -1266,12 +1333,12 @@ def active_choices_param(registry, xml_parent, data): ("_project-full-name", "projectFullName", None), ] - main_script_xml = XML.SubElement(pdef, "script") + main_script_xml = XML.SubElement(xml_parent, "script") main_script_xml.set("class", "org.biouno.unochoice.model.GroovyScript") - __handle_unochoice_script(data, pdef, "fallback-script", main_script_xml) - __handle_unochoice_script(data, pdef, "script", main_script_xml) + __handle_unochoice_script(data, xml_parent, "fallback-script", main_script_xml) + __handle_unochoice_script(data, xml_parent, "script", main_script_xml) - helpers.convert_mapping_to_xml(pdef, data, mapping, fail_required=True) + helpers.convert_mapping_to_xml(xml_parent, data, mapping, fail_required=True) def dynamic_reference_param(registry, xml_parent, data): @@ -1390,7 +1457,11 @@ class Parameters(jenkins_jobs.modules.base.Base): for param in parameters: # Pass job name to the uno-choice plugin param_type = next(iter(param)) - if param_type in ("active-choices", "dynamic-reference"): + if param_type in ( + "active-choices", + "active-choices-reactive", + "dynamic-reference", + ): param[param_type]["_project-name"] = data["name"].split("/")[-1] param[param_type]["_project-full-name"] = data["name"] self.registry.dispatch("parameter", pdefs, param) diff --git a/tests/yamlparser/fixtures/active-choices-reactive-param001.xml b/tests/yamlparser/fixtures/active-choices-reactive-param001.xml new file mode 100644 index 000000000..c4f951f51 --- /dev/null +++ b/tests/yamlparser/fixtures/active-choices-reactive-param001.xml @@ -0,0 +1,41 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + + foo + + PT_SINGLE_SELECT + false + 1 + active-choices-job + active-choices-job + + + + + + + + + + + diff --git a/tests/yamlparser/fixtures/active-choices-reactive-param001.yaml b/tests/yamlparser/fixtures/active-choices-reactive-param001.yaml new file mode 100644 index 000000000..2805be888 --- /dev/null +++ b/tests/yamlparser/fixtures/active-choices-reactive-param001.yaml @@ -0,0 +1,5 @@ +- job: + name: active-choices-job + parameters: + - active-choices-reactive: + name: foo diff --git a/tests/yamlparser/fixtures/active-choices-reactive-param002.xml b/tests/yamlparser/fixtures/active-choices-reactive-param002.xml new file mode 100644 index 000000000..b9db3ec0c --- /dev/null +++ b/tests/yamlparser/fixtures/active-choices-reactive-param002.xml @@ -0,0 +1,63 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + + false + + + file:/jar-file-path + + + file:/jar-file-path2 + + + + + + false + + + file:/jar-file-path + + + file:/jar-file-path2 + + + + + lorem + ipsum + PT_MULTI_SELECT + true + 1 + active-choices-job + active-choices-job + foo,bar + + + + + + + + + + diff --git a/tests/yamlparser/fixtures/active-choices-reactive-param002.yaml b/tests/yamlparser/fixtures/active-choices-reactive-param002.yaml new file mode 100644 index 000000000..083466855 --- /dev/null +++ b/tests/yamlparser/fixtures/active-choices-reactive-param002.yaml @@ -0,0 +1,30 @@ +- job: + name: active-choices-job + parameters: + - active-choices-reactive: + name: lorem + description: ipsum + script: + groovy: |- + return [ + 'param1', + 'param2' + ] + use-groovy-sandbox: false + script-additional-classpath: + - file:/jar-file-path + - file:/jar-file-path2 + fallback-script: + groovy: |- + return [ + 'param3', + 'param4' + ] + use-groovy-sandbox: false + script-additional-classpath: + - file:/jar-file-path + - file:/jar-file-path2 + choice-type: multi-select + enable-filters: true + filter-starts-at: 1 + referenced-parameters: foo,bar