Skip to content
Permalink
Browse files

Merge pull request #3014 from daniel-beck/JENKINS-26143

[JENKINS-26143] Make choice parameter work with choices list in pipeline
  • Loading branch information
daniel-beck committed Mar 18, 2018
2 parents bb89137 + f0e5687 commit 6de571632c3873689981c27902ae8269fdf094eb
Showing with 72 additions and 3 deletions.
  1. +72 −3 core/src/main/java/hudson/model/ChoiceParameterDefinition.java
@@ -2,6 +2,9 @@

import hudson.util.FormValidation;
import org.jenkinsci.Symbol;
import org.kohsuke.accmod.Restricted;
import org.kohsuke.accmod.restrictions.NoExternalUse;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.DataBoundConstructor;
@@ -10,6 +13,8 @@
import net.sf.json.JSONObject;
import hudson.Extension;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
import java.util.Arrays;
@@ -24,18 +29,17 @@
public static final String CHOICES_DELIMETER = CHOICES_DELIMITER;


private final List<String> choices;
private /* quasi-final */ List<String> choices;
private final String defaultValue;

public static boolean areValidChoices(String choices) {
String strippedChoices = choices.trim();
return !StringUtils.isEmpty(strippedChoices) && strippedChoices.split(CHOICES_DELIMITER).length > 0;
}

@DataBoundConstructor
public ChoiceParameterDefinition(String name, String choices, String description) {
super(name, description);
this.choices = Arrays.asList(choices.split(CHOICES_DELIMITER));
setChoicesText(choices);
defaultValue = null;
}

@@ -51,6 +55,60 @@ private ChoiceParameterDefinition(String name, List<String> choices, String defa
this.defaultValue = defaultValue;
}

/**
* Databound constructor for reflective instantiation.
*
* @param name parameter name
* @param description parameter description
*
* @since TODO
*/
@DataBoundConstructor
@Restricted(NoExternalUse.class) // there are specific constructors with String and List arguments for 'choices'
public ChoiceParameterDefinition(String name, String description) {
super(name, description);
this.choices = new ArrayList<>();
this.defaultValue = null;
}

/**
* Set the list of choices. Legal arguments are String (in which case the arguments gets split into lines) and Collection
* which sets the list of legal parameters to the String representations of the argument's non-null entries.
*
* See JENKINS-26143 for background.
*
* This retains the compatibility with the legacy String 'choices' parameter, while supporting the list type as generated
* by the snippet generator.
*
* @param choices String or Collection representing this parameter definition's possible values.
*
* @since TODO
*
*/
@DataBoundSetter
@Restricted(NoExternalUse.class) // this is terrible enough without being used anywhere
public void setChoices(Object choices) {
if (choices instanceof String) {
setChoicesText((String) choices);
return;
}
if (choices instanceof List) {
ArrayList<String> newChoices = new ArrayList<>();
for (Object o : (List) choices) {
if (o != null) {
newChoices.add(o.toString());
}
}
this.choices = newChoices;
return;
}
throw new IllegalArgumentException("expected String or List, but got " + choices.getClass().getName());
}

private void setChoicesText(String choices) {
this.choices = Arrays.asList(choices.split(CHOICES_DELIMITER));
}

@Override
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
if (defaultValue instanceof StringParameterValue) {
@@ -104,6 +162,17 @@ public String getHelpFile() {
return "/help/parameter/choice.html";
}

@Override
/*
* We need this for JENKINS-26143 -- reflective creation cannot handle setChoices(Object). See that method for context.
*/
public ParameterDefinition newInstance(@Nullable StaplerRequest req, @Nonnull JSONObject formData) throws FormException {
String name = formData.getString("name");
String desc = formData.getString("description");
String choiceText = formData.getString("choices");
return new ChoiceParameterDefinition(name, choiceText, desc);
}

/**
* Checks if parameterized build choices are valid.
*/

0 comments on commit 6de5716

Please sign in to comment.
You can’t perform that action at this time.