diff --git a/jenkins_jobs/modules/parameters.py b/jenkins_jobs/modules/parameters.py index 4961487d2..accd49d77 100644 --- a/jenkins_jobs/modules/parameters.py +++ b/jenkins_jobs/modules/parameters.py @@ -1437,6 +1437,19 @@ class Parameters(jenkins_jobs.modules.base.Base): component_type = "parameter" component_list_type = "parameters" + @staticmethod + def _extend_uno_choice_param_data(param, param_type, data): + """Pass job name to the uno-choice plugin""" + if param_type in ( + "active-choices", + "active-choices-reactive", + "dynamic-reference", + ): + # Extend uno-choice plugin parameter data with mandatory properties: + # `project-name` and `project-full-name` + param[param_type]["_project-name"] = data["name"].split("/")[-1] + param[param_type]["_project-full-name"] = data["name"] + def gen_xml(self, xml_parent, data): properties = xml_parent.find("properties") if properties is None: @@ -1459,36 +1472,20 @@ class Parameters(jenkins_jobs.modules.base.Base): if pdefs is None: pdefs = XML.SubElement(pdefp, "parameterDefinitions") for param in parameters: - # Pass job name to the uno-choice plugin - if isinstance(param, dict): - param_type = next(iter(param)) - 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"] + if not isinstance(param, dict): + # Macro parameter without arguments + param = {param: {}} + param_type = next(iter(param)) + component = self.registry.parser_data.get("parameter", {}).get( + param_type + ) + if component is None: + self._extend_uno_choice_param_data(param, param_type, data) else: # Process macro case. - # TODO: Find a way to do it more properly. - # It's possible has an issue with macro parameter chain, - # when a macro calls another macro with uno-choice plugin parameters. - component = self.registry.parser_data.get("parameter", {}).get( - param - ) for macro_param in component.get("parameters", []): for macro_param_type in macro_param: - if macro_param_type in ( - "active-choices", - "active-choices-reactive", - "dynamic-reference", - ): - macro_param[macro_param_type]["_project-name"] = data[ - "name" - ].split("/")[-1] - macro_param[macro_param_type][ - "_project-full-name" - ] = data["name"] - + self._extend_uno_choice_param_data( + macro_param, macro_param_type, data + ) self.registry.dispatch("parameter", pdefs, param) diff --git a/tests/macros/fixtures/parameter/active-choices-macro002.xml b/tests/macros/fixtures/parameter/active-choices-macro002.xml new file mode 100644 index 000000000..750e51092 --- /dev/null +++ b/tests/macros/fixtures/parameter/active-choices-macro002.xml @@ -0,0 +1,39 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + + true + + + ACTIVE_CHOICES + Active choices. + PT_SINGLE_SELECT + false + 1 + my-job + my-job + + + + + + + + + diff --git a/tests/macros/fixtures/parameter/active-choices-macro002.yaml b/tests/macros/fixtures/parameter/active-choices-macro002.yaml new file mode 100644 index 000000000..7953033fa --- /dev/null +++ b/tests/macros/fixtures/parameter/active-choices-macro002.yaml @@ -0,0 +1,16 @@ +- parameter: + name: p_ACTIVE_CHOICES + parameters: + - active-choices: + name: ACTIVE_CHOICES + description: >- + Active choices. + script: + groovy: !j2: | + return ['{{ default }}'] + +- job: + name: my-job + parameters: + - p_ACTIVE_CHOICES: + default: default_value diff --git a/tests/macros/fixtures/parameter/active-choices-macro003.xml b/tests/macros/fixtures/parameter/active-choices-macro003.xml new file mode 100644 index 000000000..b5c05c85c --- /dev/null +++ b/tests/macros/fixtures/parameter/active-choices-macro003.xml @@ -0,0 +1,40 @@ + + + + <!-- Managed by Jenkins Job Builder --> + false + false + false + false + true + + + + + + true + + + ACTIVE_CHOICES + Active choices. + PT_SINGLE_SELECT + false + 1 + my-job + my-job + + + + + + + + + diff --git a/tests/macros/fixtures/parameter/active-choices-macro003.yaml b/tests/macros/fixtures/parameter/active-choices-macro003.yaml new file mode 100644 index 000000000..cd3879b07 --- /dev/null +++ b/tests/macros/fixtures/parameter/active-choices-macro003.yaml @@ -0,0 +1,16 @@ +- parameter: + name: p_ACTIVE_CHOICES + parameters: + - active-choices: + name: ACTIVE_CHOICES + description: >- + Active choices. + script: + groovy: | + return ['{default}'] + +- job: + name: my-job + parameters: + - p_ACTIVE_CHOICES: + default: default_value