Merge "Active Choices parameters support"

This commit is contained in:
Zuul 2020-11-16 14:01:32 +00:00 committed by Gerrit Code Review
commit 8b2092770b
11 changed files with 510 additions and 0 deletions

View File

@ -1143,6 +1143,192 @@ def param_separator(registry, xml_parent, data):
helpers.convert_mapping_to_xml(pdef, data, mapping, fail_required=True)
def __handle_unochoice_script(data, pdef, script_type, main_script_xml):
if script_type == "script":
secure_script_xml = XML.SubElement(main_script_xml, "secureScript")
elif script_type == "fallback-script":
secure_script_xml = XML.SubElement(main_script_xml, "secureFallbackScript")
sub_script_xml = XML.SubElement(secure_script_xml, "script")
groovy_sandbox_xml = XML.SubElement(secure_script_xml, "sandbox")
script = data.get(script_type, {})
sub_script_xml.text = script.get("groovy", "")
groovy_sandbox_xml.text = str(script.get("use-groovy-sandbox", True)).lower()
if "script-additional-classpath" in script:
classpath_xml = XML.SubElement(secure_script_xml, "classpath")
for additional_classpath in script.get("script-additional-classpath"):
entry_xml = XML.SubElement(classpath_xml, "entry")
url_xml = XML.SubElement(entry_xml, "url")
url_xml.text = additional_classpath
def active_choices_param(registry, xml_parent, data):
"""yaml: active-choices
Active Choices Parameter
Requires the Jenkins :jenkins-wiki:`Active Choices Plug-in
<active+choices+plugin>`.
: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**
Minimal Example:
.. literalinclude::
/../../tests/yamlparser/fixtures/active-choices-param001.yaml
:language: yaml
Full Example:
.. literalinclude::
/../../tests/yamlparser/fixtures/active-choices-param002.yaml
:language: yaml
"""
element_name = "org.biouno.unochoice.ChoiceParameter"
pdef = XML.SubElement(xml_parent, element_name)
valid_choice_types_dict = {
"single-select": "PT_SINGLE_SELECT",
"multi-select": "PT_MULTI_SELECT",
"radio-buttons": "PT_RADIO",
"checkboxes": "PT_CHECKBOX",
}
mapping = [
("name", "name", None),
("description", "description", ""),
("choice-type", "choiceType", "single-select", valid_choice_types_dict),
("enable-filters", "filterable", False),
("filter-starts-at", "filterLength", 1),
("_project-name", "projectName", None),
("_project-full-name", "projectFullName", None),
]
main_script_xml = XML.SubElement(pdef, "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)
helpers.convert_mapping_to_xml(pdef, data, mapping, fail_required=True)
def dynamic_reference_param(registry, xml_parent, data):
"""yaml: dynamic-reference
Active Choices Reactive Reference Parameter
Requires the Jenkins :jenkins-wiki:`Active Choices Plug-in
<active+choices+plugin>`.
: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 omit-value-field: By default Dynamic Reference Parameters always
include a hidden input for the value. If your script creates an input
HTML element, you can check this option and the value input field will
be omitted (default False).
:arg str referenced-parameters: Comma separated list of other job
parameters referenced in the uno-choice script. When any of the
referenced parameters are updated, the Groovy script will
re-evaluate the choice list using the updated values of referenced
parameters.
:arg str choice-type: type of the choices. (default 'input-text-box')
:Allowed Values: * **input-text-box**
* **numbered-list**
* **bullet-items-list**
* **formatted-html**
* **formatted-hidden-html**
Minimal Example:
.. literalinclude::
/../../tests/yamlparser/fixtures/dynamic-reference-param001.yaml
:language: yaml
Full Example:
.. literalinclude::
/../../tests/yamlparser/fixtures/dynamic-reference-param002.yaml
:language: yaml
"""
element_name = "org.biouno.unochoice.DynamicReferenceParameter"
pdef = XML.SubElement(xml_parent, element_name)
valid_choice_types_dict = {
"input-text-box": "ET_TEXT_BOX",
"numbered-list": "ET_ORDERED_LIST",
"bullet-items-list": "ET_UNORDERED_LIST",
"formatted-html": "ET_FORMATTED_HTML",
"formatted-hidden-html": "ET_FORMATTED_HIDDEN_HTML",
}
mapping = [
("name", "name", None),
("description", "description", ""),
("choice-type", "choiceType", "input-text-box", valid_choice_types_dict),
("_project-name", "projectName", None),
("_project-full-name", "projectFullName", None),
("referenced-parameters", "referencedParameters", ""),
("omit-value-field", "omitValueField", False),
]
main_script_xml = XML.SubElement(pdef, "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)
XML.SubElement(pdef, "parameters") # Empty parameters tag
helpers.convert_mapping_to_xml(pdef, data, mapping, fail_required=True)
class Parameters(jenkins_jobs.modules.base.Base):
sequence = 21
@ -1171,4 +1357,9 @@ 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
param_type = next(iter(param))
if param_type in ("active-choices", "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)

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<org.biouno.unochoice.ChoiceParameter>
<script class="org.biouno.unochoice.model.GroovyScript">
<secureFallbackScript>
<script/>
<sandbox>true</sandbox>
</secureFallbackScript>
<secureScript>
<script/>
<sandbox>true</sandbox>
</secureScript>
</script>
<name>lorem</name>
<description/>
<choiceType>PT_SINGLE_SELECT</choiceType>
<filterable>false</filterable>
<filterLength>1</filterLength>
<projectName>active-choices-job</projectName>
<projectFullName>active-choices-job</projectFullName>
</org.biouno.unochoice.ChoiceParameter>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,5 @@
- job:
name: active-choices-job
parameters:
- active-choices:
name: lorem

View File

@ -0,0 +1,61 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<org.biouno.unochoice.ChoiceParameter>
<script class="org.biouno.unochoice.model.GroovyScript">
<secureFallbackScript>
<script>return [
'param3',
'param4'
]</script>
<sandbox>false</sandbox>
<classpath>
<entry>
<url>file:/jar-file-path</url>
</entry>
<entry>
<url>file:/jar-file-path2</url>
</entry>
</classpath>
</secureFallbackScript>
<secureScript>
<script>return [
'param1',
'param2'
]</script>
<sandbox>false</sandbox>
<classpath>
<entry>
<url>file:/jar-file-path</url>
</entry>
<entry>
<url>file:/jar-file-path2</url>
</entry>
</classpath>
</secureScript>
</script>
<name>lorem</name>
<description>ipsum</description>
<choiceType>PT_MULTI_SELECT</choiceType>
<filterable>true</filterable>
<filterLength>1</filterLength>
<projectName>active-choices-job</projectName>
<projectFullName>active-choices-job</projectFullName>
</org.biouno.unochoice.ChoiceParameter>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,29 @@
- job:
name: active-choices-job
parameters:
- active-choices:
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

View File

@ -0,0 +1,9 @@
- job:
name: unochoice/active-choices-job
parameters:
- active-choices:
name: lorem
description: ipsum
choice-type: multi-select
enable-filters: False
filter-starts-at: 1

View File

@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<org.biouno.unochoice.DynamicReferenceParameter>
<script class="org.biouno.unochoice.model.GroovyScript">
<secureFallbackScript>
<script/>
<sandbox>true</sandbox>
</secureFallbackScript>
<secureScript>
<script/>
<sandbox>true</sandbox>
</secureScript>
</script>
<parameters/>
<name>lorem</name>
<description/>
<choiceType>ET_TEXT_BOX</choiceType>
<projectName>dynamic-reference-job</projectName>
<projectFullName>dynamic-reference-job</projectFullName>
<referencedParameters/>
<omitValueField>false</omitValueField>
</org.biouno.unochoice.DynamicReferenceParameter>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,5 @@
- job:
name: dynamic-reference-job
parameters:
- dynamic-reference:
name: lorem

View File

@ -0,0 +1,30 @@
- job:
name: dynamic-reference-job
folder: unochoice
parameters:
- dynamic-reference:
name: lorem
description: ipsum
script:
groovy: |-
return [
'param1',
'param2'
]
use-groovy-sandbox: false
script-additional-classpath:
- file:/path
- file:/path2
fallback-script:
groovy: |-
return [
'param3',
'param4'
]
use-groovy-sandbox: false
script-additional-classpath:
- file:/path
- file:/path2
choice-type: numbered-list
omit-value-field: True
referenced-parameters: dolor

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<org.biouno.unochoice.ChoiceParameter>
<script class="org.biouno.unochoice.model.GroovyScript">
<secureFallbackScript>
<script/>
<sandbox>true</sandbox>
</secureFallbackScript>
<secureScript>
<script/>
<sandbox>true</sandbox>
</secureScript>
</script>
<name>lorem</name>
<description>ipsum</description>
<choiceType>PT_MULTI_SELECT</choiceType>
<filterable>false</filterable>
<filterLength>1</filterLength>
<projectName>active-choices-job</projectName>
<projectFullName>unochoice/active-choices-job</projectFullName>
</org.biouno.unochoice.ChoiceParameter>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<project>
<actions/>
<description>&lt;!-- Managed by Jenkins Job Builder --&gt;</description>
<keepDependencies>false</keepDependencies>
<blockBuildWhenDownstreamBuilding>false</blockBuildWhenDownstreamBuilding>
<blockBuildWhenUpstreamBuilding>false</blockBuildWhenUpstreamBuilding>
<concurrentBuild>false</concurrentBuild>
<canRoam>true</canRoam>
<properties>
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<org.biouno.unochoice.DynamicReferenceParameter>
<script class="org.biouno.unochoice.model.GroovyScript">
<secureFallbackScript>
<script>return [
'param3',
'param4'
]</script>
<sandbox>false</sandbox>
<classpath>
<entry>
<url>file:/path</url>
</entry>
<entry>
<url>file:/path2</url>
</entry>
</classpath>
</secureFallbackScript>
<secureScript>
<script>return [
'param1',
'param2'
]</script>
<sandbox>false</sandbox>
<classpath>
<entry>
<url>file:/path</url>
</entry>
<entry>
<url>file:/path2</url>
</entry>
</classpath>
</secureScript>
</script>
<parameters/>
<name>lorem</name>
<description>ipsum</description>
<choiceType>ET_ORDERED_LIST</choiceType>
<projectName>dynamic-reference-job</projectName>
<projectFullName>unochoice/dynamic-reference-job</projectFullName>
<referencedParameters>dolor</referencedParameters>
<omitValueField>true</omitValueField>
</org.biouno.unochoice.DynamicReferenceParameter>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>
</properties>
<scm class="hudson.scm.NullSCM"/>
<builders/>
<publishers/>
<buildWrappers/>
</project>