mirror of
https://github.com/jenkinsci/jenkins.git
synced 2026-11-16 21:42:27 +00:00
Added new copyWithDefaultValue method on parameter definition. This can be used by
plugins like the rebuild plugin that want to default the parameter input fields with a previous set of parameter values. Currently the rebuild plugin has to rewrite the parameter input display for each core parameter which prevents it from supporting additional parameter types outside of core.
This commit is contained in:
1 parent
beeb7a07ef
commit
38db351d4b
7 files changed
+86
-3
No files matched your search
@@ -42,6 +42,16 @@ public class BooleanParameterDefinition extends SimpleParameterDefinition {
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
|
||||
if (defaultValue instanceof BooleanParameterValue) {
|
||||
BooleanParameterValue value = (BooleanParameterValue) defaultValue;
|
||||
return new BooleanParameterDefinition(getName(), value.value, getDescription());
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ import java.util.Arrays;
|
||||
*/
|
||||
public class ChoiceParameterDefinition extends SimpleParameterDefinition {
|
||||
private final List<String> choices;
|
||||
private final String defaultValue;
|
||||
|
||||
@DataBoundConstructor
|
||||
public ChoiceParameterDefinition(String name, String choices, String description) {
|
||||
@@ -24,6 +25,7 @@ public class ChoiceParameterDefinition extends SimpleParameterDefinition {
|
||||
if (choices.length()==0) {
|
||||
throw new IllegalArgumentException("No choices found");
|
||||
}
|
||||
defaultValue = null;
|
||||
}
|
||||
|
||||
public ChoiceParameterDefinition(String name, String[] choices, String description) {
|
||||
@@ -32,6 +34,23 @@ public class ChoiceParameterDefinition extends SimpleParameterDefinition {
|
||||
if (this.choices.isEmpty()) {
|
||||
throw new IllegalArgumentException("No choices found");
|
||||
}
|
||||
defaultValue = null;
|
||||
}
|
||||
|
||||
private ChoiceParameterDefinition(String name, List<String> choices, String defaultValue, String description) {
|
||||
super(name, description);
|
||||
this.choices = choices;
|
||||
this.defaultValue = defaultValue;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
|
||||
if (defaultValue instanceof StringParameterValue) {
|
||||
StringParameterValue value = (StringParameterValue) defaultValue;
|
||||
return new ChoiceParameterDefinition(getName(), choices, value.value, getDescription());
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@Exported
|
||||
@@ -45,10 +64,9 @@ public class ChoiceParameterDefinition extends SimpleParameterDefinition {
|
||||
|
||||
@Override
|
||||
public StringParameterValue getDefaultParameterValue() {
|
||||
return new StringParameterValue(getName(), choices.get(0), getDescription());
|
||||
return new StringParameterValue(getName(), defaultValue == null ? choices.get(0) : defaultValue, getDescription());
|
||||
}
|
||||
|
||||
|
||||
private StringParameterValue checkValue(StringParameterValue value) {
|
||||
if (!choices.contains(value.value))
|
||||
throw new IllegalArgumentException("Illegal choice: " + value.value);
|
||||
|
||||
@@ -110,6 +110,15 @@ public abstract class ParameterDefinition implements
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new instance of this parameter definition and use the passed
|
||||
* parameter value as the default value
|
||||
*/
|
||||
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
|
||||
// By default, just return this again
|
||||
return this;
|
||||
}
|
||||
|
||||
@Exported
|
||||
public String getType() {
|
||||
return this.getClass().getSimpleName();
|
||||
|
||||
@@ -45,6 +45,16 @@ public class PasswordParameterDefinition extends SimpleParameterDefinition {
|
||||
this.defaultValue = Secret.fromString(defaultValue);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
|
||||
if (defaultValue instanceof PasswordParameterValue) {
|
||||
PasswordParameterValue value = (PasswordParameterValue) defaultValue;
|
||||
return new PasswordParameterDefinition(getName(), Secret.toString(value.getValue()), getDescription());
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterValue createValue(String value) {
|
||||
return new PasswordParameterValue(getName(), value, getDescription());
|
||||
|
||||
@@ -67,5 +67,9 @@ public class PasswordParameterValue extends ParameterValue {
|
||||
@Override
|
||||
public boolean isSensitive() {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
public Secret getValue() {
|
||||
return value;
|
||||
}
|
||||
}
|
||||
@@ -33,11 +33,29 @@ import hudson.Extension;
|
||||
public class RunParameterDefinition extends SimpleParameterDefinition {
|
||||
|
||||
private final String projectName;
|
||||
private final String runId;
|
||||
|
||||
@DataBoundConstructor
|
||||
public RunParameterDefinition(String name, String projectName, String description) {
|
||||
super(name, description);
|
||||
this.projectName = projectName;
|
||||
this.runId = null;
|
||||
}
|
||||
|
||||
private RunParameterDefinition(String name, String projectName, String runId, String description) {
|
||||
super(name, description);
|
||||
this.projectName = projectName;
|
||||
this.runId = runId;
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
|
||||
if (defaultValue instanceof RunParameterValue) {
|
||||
RunParameterValue value = (RunParameterValue) defaultValue;
|
||||
return new RunParameterDefinition(getName(), value.getRunId(), getDescription());
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
@Exported
|
||||
@@ -69,6 +87,10 @@ public class RunParameterDefinition extends SimpleParameterDefinition {
|
||||
|
||||
@Override
|
||||
public ParameterValue getDefaultParameterValue() {
|
||||
if (runId != null) {
|
||||
return createValue(runId);
|
||||
}
|
||||
|
||||
Run<?,?> lastBuild = getProject().getLastBuild();
|
||||
if (lastBuild != null) {
|
||||
return createValue(lastBuild.getExternalizableId());
|
||||
|
||||
@@ -45,6 +45,16 @@ public class StringParameterDefinition extends SimpleParameterDefinition {
|
||||
this(name, defaultValue, null);
|
||||
}
|
||||
|
||||
@Override
|
||||
public ParameterDefinition copyWithDefaultValue(ParameterValue defaultValue) {
|
||||
if (defaultValue instanceof StringParameterValue) {
|
||||
StringParameterValue value = (StringParameterValue) defaultValue;
|
||||
return new StringParameterDefinition(getName(), value.value, getDescription());
|
||||
} else {
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public String getDefaultValue() {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user