Added support of json type for extended choice parameter and related parameters.

Added support of multilevel type for extended choice parameter
dded jenkins_jobs.modules.helpers.check_mutual_exclusive_data_args decorator that used for validating data arguments
in jenkins_jobs.modules.parameters.extended_choice_param method.

Change-Id: Id8c0f8090dd99022501558cafa5b1c27e6675425
Task: #41320
This commit is contained in:
Igor Lakhtenkov 2020-12-29 10:41:56 +03:00
parent 118ac858c4
commit 2e988b3f26
7 changed files with 244 additions and 6 deletions

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations
# under the License.
from functools import wraps
import logging
import sys
@ -726,3 +727,23 @@ def jms_messaging_common(parent, subelement, data):
("msg-content", "messageContent", ""),
]
convert_mapping_to_xml(namespace, data, mapping, fail_required=True)
def check_mutual_exclusive_data_args(data_arg_position, *args):
mutual_exclusive_args = set(args)
def validator(f):
@wraps(f)
def wrap(*args):
actual_args = set(args[data_arg_position].keys())
if len(actual_args & mutual_exclusive_args) > 1:
raise JenkinsJobsException(
"Args: {} in {} are mutual exclusive. Please define one of it.".format(
mutual_exclusive_args, f.__name__
)
)
return f(*args)
return wrap
return validator

View File

@ -422,6 +422,20 @@ def run_param(registry, xml_parent, data):
helpers.convert_mapping_to_xml(pdef, data, mapping, fail_required=True)
@helpers.check_mutual_exclusive_data_args(
2, "value", "property-file", "groovy-script", "groovy-script-file"
)
@helpers.check_mutual_exclusive_data_args(
2, "default-value", "default-property-file", "default-groovy-script"
)
@helpers.check_mutual_exclusive_data_args(
2,
"value-description",
"description-property-file",
"description-groovy-script",
"description-groovy-script-file",
)
@helpers.check_mutual_exclusive_data_args(2, "javascript", "javascript-file")
def extended_choice_param(registry, xml_parent, data):
"""yaml: extended-choice
Creates an extended choice parameter where values can be read from a file
@ -440,7 +454,8 @@ def extended_choice_param(registry, xml_parent, data):
:arg str visible-items: number of items to show in the list
(optional, default 5)
:arg str type: type of select, can be single-select, multi-select,
radio, checkbox or textbox (optional, default single-select)
multi-level-single-select, multi-level-multi-select,
radio, checkbox, textbox, json (optional, default single-select)
:arg str value: comma separated list of values for the single select
or multi-select box (optional, default '')
:arg str default-value: used to set the initial selection of the
@ -470,9 +485,15 @@ def extended_choice_param(registry, xml_parent, data):
groovy script (optional, default '')
:arg str description-groovy-script: location of groovy script when value
description needs to come from a groovy script (optional, default '')
:arg str description-groovy-script-file: location of groovy script file when value
description needs to come from a groovy script (optional, default '')
:arg str description-groovy-classpath: classpath for the value description
groovy script (optional, default '')
:arg str javascript: the javascript script contents (optional, default '')
:arg str javascript-file: location of javasript script file to generate
parameters (optional, default '')
:arg bool save-json-parameter-to-file: if json parameter should be saved
to file (optional, default False)
Minimal Example:
@ -499,14 +520,20 @@ def extended_choice_param(registry, xml_parent, data):
choicedict = {
"single-select": "PT_SINGLE_SELECT",
"multi-select": "PT_MULTI_SELECT",
"multi-level-single-select": "PT_MULTI_LEVEL_SINGLE_SELECT",
"multi-level-multi-select": "PT_MULTI_LEVEL_MULTI_SELECT",
"radio": "PT_RADIO",
"checkbox": "PT_CHECKBOX",
"textbox": "PT_TEXTBOX",
"json": "PT_JSON",
"PT_SINGLE_SELECT": "PT_SINGLE_SELECT",
"PT_MULTI_SELECT": "PT_MULTI_SELECT",
"PT_MULTI_LEVEL_SINGLE_SELECT": "PT_MULTI_LEVEL_SINGLE_SELECT",
"PT_MULTI_LEVEL_MULTI_SELECT": "PT_MULTI_LEVEL_MULTI_SELECT",
"PT_RADIO": "PT_RADIO",
"PT_CHECKBOX": "PT_CHECKBOX",
"PT_TEXTBOX": "PT_TEXTBOX",
"PT_JSON": "PT_JSON",
}
mapping = [
("value", "value", ""),
@ -529,7 +556,11 @@ def extended_choice_param(registry, xml_parent, data):
("default-groovy-script", "defaultGroovyScript", ""),
("default-groovy-classpath", "defaultGroovyClasspath", ""),
("description-groovy-script", "descriptionGroovyScript", ""),
("description-groovy-script-file", "descriptionGroovyScriptFile", ""),
("description-groovy-classpath", "descriptionGroovyClasspath", ""),
("javascript", "javascript", ""),
("javascript-file", "javascriptFile", ""),
("save-json-parameter-to-file", "saveJSONParameterToFile", False),
]
helpers.convert_mapping_to_xml(pdef, data, mapping, fail_required=True)

View File

@ -19,7 +19,11 @@ import yaml
from jenkins_jobs.errors import InvalidAttributeError
from jenkins_jobs.errors import MissingAttributeError
from jenkins_jobs.modules.helpers import convert_mapping_to_xml
from jenkins_jobs.errors import JenkinsJobsException
from jenkins_jobs.modules.helpers import (
convert_mapping_to_xml,
check_mutual_exclusive_data_args,
)
from tests import base
@ -112,3 +116,19 @@ class TestCaseTestHelpers(base.BaseTestCase):
user_input_data,
user_input_mappings,
)
def test_check_mutual_exclusive_data_args_no_mutual_exclusive(self):
@check_mutual_exclusive_data_args(0, "foo", "bar")
@check_mutual_exclusive_data_args(0, "foo", "baz")
def func(data):
pass
func({"baz": "qaz", "bar": "qaz"})
def test_check_mutual_exclusive_data_args_mutual_exclusive(self):
@check_mutual_exclusive_data_args(0, "foo", "bar")
@check_mutual_exclusive_data_args(0, "foo", "baz")
def func(data):
pass
self.assertRaises(JenkinsJobsException, func, {"foo": "qaz", "bar": "qaz"})

View File

@ -4,7 +4,7 @@
<hudson.model.ParametersDefinitionProperty>
<parameterDefinitions>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<name>OPTIONS</name>
<name>OPTIONS_VALUE</name>
<description>Available options</description>
<value>foo|bar|select</value>
<visibleItemCount>2</visibleItemCount>
@ -13,6 +13,35 @@
<defaultValue>foo</defaultValue>
<descriptionPropertyValue/>
<type>PT_MULTI_SELECT</type>
<propertyFile/>
<propertyKey>key</propertyKey>
<defaultPropertyFile/>
<defaultPropertyKey>fookey</defaultPropertyKey>
<descriptionPropertyFile/>
<descriptionPropertyKey/>
<bindings/>
<groovyScript/>
<groovyScriptFile/>
<groovyClasspath/>
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<name>OPTIONS_FILE</name>
<description>Available options</description>
<value/>
<visibleItemCount>2</visibleItemCount>
<multiSelectDelimiter>|</multiSelectDelimiter>
<quoteValue>true</quoteValue>
<defaultValue/>
<descriptionPropertyValue/>
<type>PT_MULTI_SELECT</type>
<propertyFile>/home/foo/property.prop</propertyFile>
<propertyKey>key</propertyKey>
<defaultPropertyFile>/home/property.prop</defaultPropertyFile>
@ -26,7 +55,11 @@
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<name>OPTIONS_CHECKBOX</name>
@ -51,7 +84,11 @@
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<name>MULTISELECTOPTIONS</name>
@ -76,7 +113,98 @@
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<name>JSON</name>
<description/>
<value/>
<visibleItemCount>5</visibleItemCount>
<multiSelectDelimiter>,</multiSelectDelimiter>
<quoteValue>false</quoteValue>
<defaultValue/>
<descriptionPropertyValue/>
<type>PT_JSON</type>
<propertyFile/>
<propertyKey/>
<defaultPropertyFile/>
<defaultPropertyKey/>
<descriptionPropertyFile/>
<descriptionPropertyKey/>
<bindings/>
<groovyScript>import net.sf.json.JSONObject; def jsonEditorOptions = JSONObject.fromObject(/{schema: {&quot;type&quot;: &quot;object&quot;, &quot;title&quot;: &quot;Name&quot;, &quot;properties&quot;: {&quot;name&quot;: {&quot;type&quot;: &quot;string&quot;, &quot;propertyOrder&quot; : 1}}}}/);</groovyScript>
<groovyScriptFile/>
<groovyClasspath/>
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<name>MULTILEVELMULTISELECT</name>
<description/>
<value>foo,bar,baz</value>
<visibleItemCount>5</visibleItemCount>
<multiSelectDelimiter>,</multiSelectDelimiter>
<quoteValue>false</quoteValue>
<defaultValue/>
<descriptionPropertyValue/>
<type>PT_MULTI_LEVEL_MULTI_SELECT</type>
<propertyFile/>
<propertyKey/>
<defaultPropertyFile/>
<defaultPropertyKey/>
<descriptionPropertyFile/>
<descriptionPropertyKey/>
<bindings/>
<groovyScript/>
<groovyScriptFile/>
<groovyClasspath/>
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
<name>MULTILEVELSINGLESELECT</name>
<description/>
<value>foo</value>
<visibleItemCount>5</visibleItemCount>
<multiSelectDelimiter>,</multiSelectDelimiter>
<quoteValue>false</quoteValue>
<defaultValue/>
<descriptionPropertyValue/>
<type>PT_MULTI_LEVEL_SINGLE_SELECT</type>
<propertyFile/>
<propertyKey/>
<defaultPropertyFile/>
<defaultPropertyKey/>
<descriptionPropertyFile/>
<descriptionPropertyKey/>
<bindings/>
<groovyScript/>
<groovyScriptFile/>
<groovyClasspath/>
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>

View File

@ -1,8 +1,7 @@
parameters:
- extended-choice:
name: OPTIONS
name: OPTIONS_VALUE
description: "Available options"
property-file: /home/foo/property.prop
property-key: key
quote-value: true
type: multi-select
@ -10,6 +9,16 @@ parameters:
visible-items: 2
multi-select-delimiter: '|'
default-value: foo
default-property-key: fookey
- extended-choice:
name: OPTIONS_FILE
description: "Available options"
property-file: /home/foo/property.prop
property-key: key
quote-value: true
type: multi-select
visible-items: 2
multi-select-delimiter: '|'
default-property-file: /home/property.prop
default-property-key: fookey
- extended-choice:
@ -37,3 +46,24 @@ parameters:
visible-items: 2
multi-select-delimiter: '|'
default-value: foo
- extended-choice:
name: JSON
type: json
groovy-script: >-
import net.sf.json.JSONObject;
def jsonEditorOptions = JSONObject.fromObject(/{schema:
{"type": "object", "title": "Name", "properties":
{"name": {"type": "string", "propertyOrder" : 1}}}}/);
- extended-choice:
name: MULTILEVELMULTISELECT
type: multi-level-multi-select
value: !join:
- ','
-
- foo
- bar
- baz
- extended-choice:
name: MULTILEVELSINGLESELECT
type: multi-level-single-select
value: foo

View File

@ -26,7 +26,11 @@
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>

View File

@ -26,7 +26,11 @@
<defaultGroovyScript/>
<defaultGroovyClasspath/>
<descriptionGroovyScript/>
<descriptionGroovyScriptFile/>
<descriptionGroovyClasspath/>
<javascript/>
<javascriptFile/>
<saveJSONParameterToFile>false</saveJSONParameterToFile>
</com.cwctravel.hudson.plugins.extended__choice__parameter.ExtendedChoiceParameterDefinition>
</parameterDefinitions>
</hudson.model.ParametersDefinitionProperty>