# active-choices-plugin **Repository Path**: mamh-java/active-choices-plugin ## Basic Information - **Project Name**: active-choices-plugin - **Description**: https://github.com/mamh-java/active-choices-plugin - **Primary Language**: Unknown - **License**: MIT - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2022-05-14 - **Last Updated**: 2024-07-06 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # Active Choices Plug-in [](https://plugins.jenkins.io/uno-choice) [](https://github.com/jenkinsci/active-choices-plugin/blob/master/CHANGES.md) [](https://plugins.jenkins.io/active-choices) [](https://ci.jenkins.io/job/Plugins/job/active-choices-plugin/job/master/) [](https://issues.jenkins.io/browse/JENKINS-64962?jql=component%20%3D%20%27active-choices-plugin%27%20AND%20resolution%20IS%20EMPTY%20ORDER%20BY%20updated%20DESC) ## Overview The Active Choices plugin is used in parametrized freestyle Jenkins jobs to create **scripted, dynamic and interactive job parameters**. Active Choices **parameters** can be **dynamically updated** and can be **rendered as combo-boxes, check-boxes, radio-buttons or rich HTML UI widgets**. Active Choices parameters are scripted using Groovy, or (optionally) Scriptler Groovy scripts. These custom scripts support the use of the Jenkins Java API, system environment variables, global node properties, and potentially external Java and Javascript libraries. Once the plugin is installed, three new parameter types become available: 1. Active Choices Parameter 2. Active Choices Reactive Parameter 3. Active Choices Reactive Reference Parameter > **_NOTE:_** The Reactive Reference Parameter allows for parameters to be displayed as >formatted HTML. When configuring jobs with this feature, keep in mind how the parameter will be rendered and whether it could be a security issue. > **_NOTE:_** The plug-in was developed in a way that it relies heavily on the HTML/DOM >of the Jenkins UI. We navigate the DOM using JavaScript to create the relationship and >reactivity between parameters. Follow [JENKINS-63284](https://issues.jenkins-ci.org/browse/JENKINS-63284) >for updates on a version that does not require the UI. When that issue is closed, the >plug-in should work fine with Pipelines, DSL, timers, cron, REST-API-triggered jobs, etc. Active Choices parameters allow users to select value(s) for a job parameter. Parameter values can be: - dynamically generated (using Groovy or a [Scriptler](https://wiki.jenkins-ci.org/display/JENKINS/Scriptler+Plugin) script) - dynamically updated based on other UI parameters - multi-valued (can have more than one value) - rendered with a variety of UI controls, including dynamic HTML (see NOTE above on the security risks) We will introduce the Active Choices based UI controls by briefly describing their behavior and rendering characteristics. We will then provide a guide to their configuration. ## Active Choices Parameter ### Behavior - An Active Choices parameter dynamically generates a list of value options for a build parameter using a Groovy script or a script from the Scriptler catalog. ### Rendering - Active Choices parameters can be rendered as standard selection lists, check boxes and radio buttons. - A text box filter can be optionally shown to aid in filtering the value options.  ## Active Choices Reactive and Reactive Reference Parameters Both of these parameters have additional useful behaviors and Reactive Reference has some unique rendering options. ### Behavior Similar to Active Choices Parameter above: - Active Choices Reactive and Reactive Reference parameters dynamically generate value options for a build parameter using a Groovy script or a Scriptler script Furthermore: - Active Choices Reactive and Reactive Reference parameters can be **dynamically updated**(cascade update) when the value of other job UI control(s) change(s) ### Rendering Options #### Active Choices Reactive - Active Choices Reactive parameters can be rendered as standard selection lists, check boxes and radio buttons. - A text box filter can be optionally shown to aid in filtering the value options. #### Active Choices Reactive Reference Active Choices Reactive Reference parameters are used to enhance a Jenkins job form UI with reference information. With this use case in mind, a Reactive Reference UI control can be rendered as: - An HTML list (bulleted or numbered) - An HTML input text box - Dynamically generated HTML (image, iframe, etc.); The dynamically generated HTML option, works with any well-formatted HTML returned by the Groovy script. It enables rendering of a variety of HTML elements, including **pictures, inline-frames, hyperlinks, richly formatted text** etc. In addition, Reactive Reference parameters can be **hidden** from the UI and thus provide the option of dynamically generating hidden build parameters. These options are further discussed in the Reactive Reference configuration section. ### Rendering Example  In the example above the value options for the 'Professions' parameter get updated when the 'Gender' parameter changes. In addition, the Reactive Reference parameter 'Gender_Balance' rendered as a picture is also dynamically updated when the 'Gender' parameter is updated. ### Behavior and Rendering Summary The following table summarizes the behavior and rendering characteristics of the three Active Choices parameter types.  ## Active Choices Parameter Type Configuration The plug-in includes the following parameter types: - Active Choices Parameter - Active Choices Reactive Parameter - Active Choices Reactive Reference Parameter We now describe the details of their configuration. ### Active Choices Parameter: Configuration Options (Example 01)  An Active Choices Parameter is configured by setting the following options in the parameter configuration #### The 'Name' and 'Description' These are the typical parameter Name and Description that are common to all Jenkins parameters #### The 'Script' The 'Script' is the **Groovy code or Scriptlet script** that will dynamically generate the parameter value options - By selecting either of the two radio button options you can either type a Groovy script directly or use a Scriptler script - The script must return a **java.util.List**, an **Array** or a **java.util.Map**, as in the example below: ```groovy return ['Option 1', 'Option 2', 'Option 3'] ``` #### The 'Fallback Script' The 'Fallback Script' configuration option provides alternate parameter value options in case the main Script fails either by throwing an Exception, or by not return a `java.util.List`, `Array`, or `java.util.Map`. #### The 'Choice Type' The **'Choice Type**' option provides four different rendering options for the option values: 1. A list box where a single selection is allowed 2. A list box where multiple selections are allowed 3. A list of check boxes (multiple selections allowed) 4. A list of radio buttons (a single selection is allowed) #### The 'Enable Filter' The '**Enable Filter**' option will provide a text box filter in the UI control where a text filter can be typed. Only value options that contain the text are then listed. This filer is case independent. ### Active Choices Parameter Rendering (Example 01) The 'Example 01' Active Choices parameter configuration generates the following build form UI control. The user can select a single 'State' option from a filterable drop-down list.  #### Making 'Default' selections It is possible to have some of the options displayed in an Active Choices UI control selected by default when the control is initialized. You can **define the default value selections** by adding the suffix; **:selected** to the element you want to be the default selection from those returned by the script. In the example below, we will make the State of 'Parana' the default selection when the parameter UI control is rendered.  #### Making 'Disabled' selections You also can **define disabled selections** by adding the suffix; **:disabled** to the element(s) you want to be disabled. In the example below, we will make various elements to be disabled and immutable.  As you can see, both **:selected** and **:disabled** can be specified at the same time. We credit the developers of the [Dynamic Parameter plugin](https://wiki.jenkins-ci.org/display/JENKINS/Dynamic+Parameter+Plug-in) with some of the initial concepts and code on which Active Choices was implemented. However, there are several important differences and improvements between the Active Choices plugin and the original Dynamic Parameter plugin: 1. An Active Choices parameter can be **rendered** as a multi-select control (combo-box or check-box) allowing users to **select more than one value** for the parameter 2. The parameter **options value list can be filtered**. If the "**Enable Filters**" option is checked, an extra input box will be displayed allowing users to filter the options. 3. You can define a '**fallback**' behavior if the value generator script raises an exception. 4. You can define **default value selections**in the dynamically generated value list ### Active Choices Reactive Parameter: Configuration Options (Example 02)  An Active Choices Reactive parameter is configured with a set of similar options as those shown above for the Active Choices parameter. However, a Reactive parameter provides the additional '**Referenced parameters**' configuration option. - This option, takes a list of job parameters that trigger an **auto-refresh of the Reactive Parameter when any of the 'Referenced parameters' change** #### The 'Referenced parameters' The '**Referenced parameters**' text field contains **a list of comma separated parameter Names**(from the current job) that will trigger a refresh of the Reactive Parameter when their values change. The values of these parameters are passed to the script binding context before the script is re-executed to generate a new set of option values for the Active Choices control. ### Active Choices Reactive Parameter Rendering (Example 02) Let's examine a Jenkins build form rendered with Active Choices parameters that satisfies the following requirements. The form: - Allows users to select one of several Brazilian States - Provides an additional control where a set of Cities belonging to the selected State is dynamically displayed - Allows the user to select one or more of the displayed Cities  The job UI has two parameters: #### 1) States: An Active Choices Parameter The first parameter is the **'States'** Active Choices Parameter from 'Example 01'. It allows the user to select one of several Brazilian States. We could have just as easily used a Jenkins Choice Parameter, but we use an Active Choice parameter (as shown from Example 01). The Groovy script for this is: ```groovy return [ 'Sao Paulo', 'Rio de Janeiro', 'Parana', 'Acre' ] ``` #### 2) Cities: An Active Choices Reactive Parameter The second parameter is the **'Cities'** Active Choices Reactive Parameter that **dynamically displays** a set of cities belonging to the selected State and allows users to select multiple values. The 'Cities' parameter configuration is shown above in 'Example 02'. Note that: - The 'Cities' Reactive parameter references the previously defined States parameter ('Referenced parameters'=States); - The 'Choice Type' is set to 'Check Boxes'.This will allow the user to select one or more 'Cities' by selecting multiple check boxes. - A custom 'Groovy Script' will be used to generate the 'Cities' value options as shown below (the last list value returned by the script) ```groovy if (States == "Sao Paulo") { return ["Barretos", "Sao Paulo", "Itu"] } else if (States == "Rio de Janeiro") { return ["Rio de Janeiro", "Mangaratiba"] } else if (States == "Parana") { return ["Curitiba", "Ponta Grossa"] } else if (States == "Acre") { return ["Rio Branco", "Acrelandia"] } else { return ["Unknown state"] } ``` Whenever the user changes the option of the States parameter, the 'Cities' parameter will get dynamically updated. Note how that the **'States'** referenced parameter is in the script binding and can be used directly. You can use a Reactive parameter type for things like displaying the list of Maven artifacts, given a group ID. ### Active Choices Reactive Reference Parameter: Configuration Options  A Reactive Reference parameter is configured with a set of similar options as those shown above for the Active Choices Reactive parameter. However, a **Reactive Reference parameter provides a unique set of rendering options** (see 'Choice Type'). - Input text box - Numbered list - Bullet items list - Formatted HTML - Formatted Hidden HTML Given the wide variety of rendering options the Active Choices Groovy script must return the following types of variables for each option: | Choice Type | Groovy Returns | Comment | |----------------------|----------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------| | Input text box | String | The return String appears in a simple text box | | Numbered list | List | The return List displays as a numbered list | | Bullet items list | List | The return List displays as a bulleted list | | Formatted HTML | String | The return String must be well formatted HTML to display correctly. You can include any HTML tags here, e.g.: some