1

I have been trying unsuccessfully to get DynamicReferenceParameter of the Active Choice plugin to show previously selected values as list, however it either shows them on one line or it treats each symbol as a new element in the list.

hosts_script = '''
return['host1', 'host2', 'host3']
'''

properties([
    parameters([
      [$class: 'ChoiceParameter',
        choiceType: 'PT_MULTI_SELECT',
        description: 'Select one or more hosts',
        filterLength: 1,
        filterable: true,
        name: 'hosts',
        randomName: 'choice-parameter-5631314439613978',
        script: [
            $class: 'GroovyScript',
            fallbackScript: [
                classpath: [],
                sandbox: true,
                script:
                    'return[\'Could not get hosts\']'
            ],
            script: [
                classpath: [],
                sandbox: true,
                script: hosts_script,
            ]
        ]
    ],
    [$class: 'DynamicReferenceParameter', 
        choiceType: 'ET_UNORDERED_LIST', 
        description: 'These are the selected hosts', 
        name: 'Selected hosts', 
        omitValueField: false, 
        randomName: 'choice-parameter-5693388460832175', 
        referencedParameters: 'hosts',
        script: [
            $class: 'GroovyScript', 
            fallbackScript: [
                classpath: [], 
                sandbox: true, 
                script: 
                    'return[\'Could not get hosts\']'
            ], 
            script: [
                classpath: [], 
                sandbox: true,
                script: 
                    """
                    //def list = hosts.replaceAll( '"','\\'' )
                    //List list = hosts.collect{ '\\'' + it + '\\','}
                    //def list1 = new ArrayList(Arrays.asList(hosts))
                    List list = hosts.collect{ '\\'' + it + '\\','} 
                    //String list = hosts.inspect()

                    return list
                    """
              ]
          ]
       ]
    ])
 ])

I can get this • host1,host2

Or this:

 •'h',
 •'o',
 •'s',
 •'t',
 •'2',

Where I want this:

 • host1
 • host2

At the very end you will see what I have been playing with ... the biggest problem for me is that is impossible to use println to troubleshoot.

1 Answer 1

1

I figured out what was going on. The return from hosts var was a single string with comma separated hostnames host1, host2, I had to convert it to List using the comma as delimiter. The following post How to convert comma-separated String to List? helped me with that task.

Basically the last section now reads

                script: 
                """
                List<String> list = Arrays.asList(hosts.split("\\\\s*,\\\\s*"));
                return list
                """

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.