Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ test-output/
.settings/
.project
**/*~
/.idea/
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,14 @@ public abstract class AbstractCascadableParameter extends AbstractScriptablePara
* @param name name
* @param description description
* @param script script used to generate the list of parameter values
* @param visibilityScript script used to determine the visibility of this parameter
* @param referencedParameters comma separated list of referenced parameters
* @deprecated see JENKINS-32149
*/
protected AbstractCascadableParameter(String name, String description, Script script, String referencedParameters) {
super(name, description, script);
protected AbstractCascadableParameter(String name, String description,
Script script, Script visibilityScript,
String referencedParameters) {
super(name, description, script, visibilityScript);
this.referencedParameters = referencedParameters;
}

Expand All @@ -77,11 +80,13 @@ protected AbstractCascadableParameter(String name, String description, Script sc
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script used to generate the list of parameter values
* @param visibilityScript script used to determine the visibility of this parameter
* @param referencedParameters comma separated list of referenced parameters
*/
protected AbstractCascadableParameter(String name, String description, String randomName,
Script script, String referencedParameters) {
super(name, description, randomName, script);
Script script, Script visibilityScript,
String referencedParameters) {
super(name, description, randomName, script, visibilityScript);
this.referencedParameters = referencedParameters;
}

Expand Down Expand Up @@ -152,6 +157,12 @@ public List<Object> getChoicesForUI() {
Map<Object, Object> mapResult = getChoices(getParameters());
return Arrays.<Object>asList(mapResult.values(), mapResult.keySet());
}

@Override
@JavaScriptMethod
public boolean isVisible() {
return isVisible(getParameters());
}

public String[] getReferencedParametersAsArray() {
String referencedParameters = this.getReferencedParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@

package org.biouno.unochoice;

import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import hudson.model.AbstractBuild;
import hudson.model.AbstractItem;
import hudson.model.ParameterValue;
import hudson.model.Project;
import hudson.model.StringParameterValue;
import jenkins.model.Jenkins;
import org.apache.commons.lang.ObjectUtils;
import org.apache.commons.lang.StringUtils;
import org.biouno.unochoice.model.Script;
Expand All @@ -39,12 +39,11 @@
import org.kohsuke.stapler.Stapler;
import org.kohsuke.stapler.StaplerRequest;

import hudson.model.AbstractBuild;
import hudson.model.AbstractItem;
import hudson.model.ParameterValue;
import hudson.model.Project;
import hudson.model.StringParameterValue;
import jenkins.model.Jenkins;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

/**
* Base class for parameters with scripts.
Expand Down Expand Up @@ -87,6 +86,12 @@ public abstract class AbstractScriptableParameter extends AbstractUnoChoiceParam
* Script used to render the parameter.
*/
protected final Script script;

/**
* Script used to render the visibility.
*/
protected final Script visibilityScript;

/**
* The project name.
*/
Expand All @@ -104,11 +109,13 @@ public abstract class AbstractScriptableParameter extends AbstractUnoChoiceParam
* @param name name
* @param description description
* @param script script used to generate the list of parameter values
* @param visibilityScript script used to determine the visibility of this parameter
* @deprecated see JENKINS-32149
*/
protected AbstractScriptableParameter(String name, String description, Script script) {
protected AbstractScriptableParameter(String name, String description, Script script, Script visibilityScript) {
super(name, description);
this.script = script;
this.visibilityScript = visibilityScript;
this.projectName = null;
this.projectFullName = null;
}
Expand All @@ -122,10 +129,12 @@ protected AbstractScriptableParameter(String name, String description, Script sc
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script used to generate the list of parameter values
* @param visibilityScript script used to determine the visibility of this parameter
*/
protected AbstractScriptableParameter(String name, String description, String randomName, Script script) {
protected AbstractScriptableParameter(String name, String description, String randomName, Script script, Script visibilityScript) {
super(name, description, randomName);
this.script = script;
this.visibilityScript = visibilityScript;
// Try to get the project name from the current request. In case of being called in some other non-web way,
// the name will be fetched later via Jenkins.getInstance() and iterating through all items. This is for a
// performance wise approach first.
Expand Down Expand Up @@ -156,6 +165,24 @@ public Script getScript() {
return script;
}

/**
* Gets the visibility script.
*
* @return the visibility script
*/
public Script getVisibilityScript() {
return visibilityScript;
}

public boolean isVisible() {
return evalVisibility(getParameters());
}

@Override
public boolean isVisible(Map<Object, Object> parameters) {
return evalVisibility(parameters);
}

/**
* Gets the current parameters, be it before or after other referenced parameters triggered an update. Populates
* parameters common to all evaluations, such as jenkinsProject, which is the current Jenkins project.
Expand Down Expand Up @@ -253,6 +280,9 @@ public String getChoicesAsString(Map<Object, Object> parameters) {

@SuppressWarnings({ "rawtypes", "unchecked" })
private Object eval(Map<Object, Object> parameters) {
if (script == null) {
return Collections.emptyMap();
}
try {
Map<Object, Object> scriptParameters = getHelperParameters();
scriptParameters.putAll(parameters);
Expand All @@ -264,6 +294,22 @@ private Object eval(Map<Object, Object> parameters) {
}
}

@SuppressWarnings({ "rawtypes", "unchecked" })
protected boolean evalVisibility(Map<Object, Object> parameters) {
if (visibilityScript == null) {
return true;
}
try {
Map<Object, Object> scriptParameters = getHelperParameters();
scriptParameters.putAll(parameters);
final ScriptCallback<Exception> callback = new ScriptCallback(getName(), visibilityScript, scriptParameters);
return (Boolean) callback.call();
} catch (Exception e) {
LOGGER.log(Level.SEVERE, "Error executing visibility script for dynamic parameter", e);
return true;
}
}

/*
* (non-Javadoc)
* @see hudson.model.ParameterDefinition#getDefaultParameterValue()
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/org/biouno/unochoice/CascadableParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ public interface CascadableParameter<T> extends ScriptableParameter<T> {
*/
public List<Object> getChoicesForUI();

/**
* Evaluates a script and returns the visibility of this parameter.
*
* @return script result as boolean
*/
public boolean isVisible();

/**
* Exposed to the UI. Is triggered everytime any of the referenced parameters gets updated.
* @param parameters Comma separated list of parameters
Expand Down
28 changes: 18 additions & 10 deletions src/main/java/org/biouno/unochoice/CascadeChoiceParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,14 +72,16 @@ public class CascadeChoiceParameter extends AbstractCascadableParameter {
* @param name name
* @param description description
* @param script script
* @param visibilityScript visibility script
* @param choiceType choice type
* @param referencedParameters referenced parameters
* @param filterable filter flag
* @deprecated see JENKINS-32149
*/
public CascadeChoiceParameter(String name, String description, Script script,
String choiceType, String referencedParameters, Boolean filterable) {
super(name, description, script, referencedParameters);
public CascadeChoiceParameter(String name, String description,
Script script, Script visibilityScript,
String choiceType, String referencedParameters, Boolean filterable) {
super(name, description, script, visibilityScript, referencedParameters);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = null;
Expand All @@ -92,14 +94,16 @@ public CascadeChoiceParameter(String name, String description, Script script,
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script
* @param visibilityScript visibility script
* @param choiceType choice type
* @param referencedParameters referenced parameters
* @param filterable filter flag
* @deprecated see JENKINS-31625
*/
public CascadeChoiceParameter(String name, String description, String randomName, Script script,
String choiceType, String referencedParameters, Boolean filterable) {
super(name, description, randomName, script, referencedParameters);
public CascadeChoiceParameter(String name, String description, String randomName,
Script script, Script visibilityScript,
String choiceType, String referencedParameters, Boolean filterable) {
super(name, description, randomName, script, visibilityScript, referencedParameters);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = null;
Expand All @@ -112,15 +116,18 @@ public CascadeChoiceParameter(String name, String description, String randomName
* @param description description
* @param randomName parameter random generated name (uuid)
* @param script script
* @param visibilityScript visibility script
* @param choiceType choice type
* @param referencedParameters referenced parameters
* @param filterable filter flag
* @param filterLength filter length
*/
@DataBoundConstructor
public CascadeChoiceParameter(String name, String description, String randomName, Script script,
String choiceType, String referencedParameters, Boolean filterable, Integer filterLength) {
super(name, description, randomName, script, referencedParameters);
public CascadeChoiceParameter(String name, String description, String randomName,
Script script, Script visibilityScript,
String choiceType, String referencedParameters,
Boolean filterable, Integer filterLength) {
super(name, description, randomName, script, visibilityScript, referencedParameters);
this.choiceType = StringUtils.defaultIfBlank(choiceType, PARAMETER_TYPE_SINGLE_SELECT);
this.filterable = filterable;
this.filterLength = filterLength;
Expand All @@ -136,7 +143,8 @@ public String getChoiceType() {
}

/**
* Get the filter flag.
* Gets the filter flag.
*
* @return filter flag
*/
public Boolean getFilterable() {
Expand Down
Loading