From 38eb17f7f65be667df62695956b49eef9e0bcfa8 Mon Sep 17 00:00:00 2001 From: greg2001 Date: Sun, 21 Sep 2014 21:46:24 +0200 Subject: [PATCH 1/5] Support for boolean and text parameters --- pom.xml | 286 +++++----- .../BaseParameterDefinition.java | 498 +++++++++--------- .../BooleanParameterDefinition.java | 116 ++++ .../TextParameterDefinition.java | 117 ++++ .../ScriptlerBooleanParameterDefinition.java | 124 +++++ .../ScriptlerTextParameterDefinition.java | 120 +++++ .../BooleanParameterDefinition.properties | 15 + .../BooleanParameterDefinition/config.jelly | 37 ++ .../config_de.properties | 20 + .../help-defaultValueScript.html | 19 + .../help-defaultValueScript_de.html | 19 + .../BooleanParameterDefinition/index.jelly | 30 ++ .../ChoiceParameterDefinition/index.jelly | 8 +- .../TextParameterDefinition.properties | 15 + .../TextParameterDefinition/config.jelly | 37 ++ .../config_de.properties | 20 + .../help-defaultValueScript.html | 19 + .../help-defaultValueScript_de.html | 19 + .../TextParameterDefinition/index.jelly | 30 ++ ...ptlerBooleanParameterDefinition.properties | 15 + .../config.jelly | 108 ++++ .../config_de.properties | 26 + .../index.jelly | 30 ++ .../index.jelly | 8 +- ...criptlerTextParameterDefinition.properties | 15 + .../config.jelly | 108 ++++ .../config_de.properties | 26 + .../index.jelly | 30 ++ 28 files changed, 1515 insertions(+), 400 deletions(-) create mode 100644 src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition.java create mode 100644 src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition.java create mode 100644 src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition.java create mode 100644 src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition.java create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition/config.jelly create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition/config_de.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition/help-defaultValueScript.html create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition/help-defaultValueScript_de.html create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition/index.jelly create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition/config.jelly create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition/config_de.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition/help-defaultValueScript.html create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition/help-defaultValueScript_de.html create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition/index.jelly create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/config.jelly create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/config_de.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/index.jelly create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config.jelly create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config_de.properties create mode 100644 src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/index.jelly diff --git a/pom.xml b/pom.xml index 643455c..a20a9da 100644 --- a/pom.xml +++ b/pom.xml @@ -1,143 +1,143 @@ - - - 4.0.0 - - org.jenkins-ci.plugins - plugin - 1.447 - - - com.seitenbau.jenkins.plugins - dynamicparameter - hpi - 0.2.1-SNAPSHOT - - Jenkins Dynamic Parameter Plug-in - - This plugin allows build parameters with dynamically generated default values. - - - - - Apache License - All source code is under the Apache License. - - - - - scm:git:ssh://github.com/Seitenbau/sb-jenkins-dynamicparameter.git - scm:git:git@github.com:Seitenbau/sb-jenkins-dynamicparameter.git - https://github.com/Seitenbau/sb-jenkins-dynamicparameter.git - - - https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Dynamic+Parameter+Plug-in - - - - dimitarp - Dimitar Popov - - - baranowski - Christian Baranowski - - - - - - maven.jenkins-ci.org - http://repo.jenkins-ci.org/public/ - - - - - maven.jenkins-ci.org - http://repo.jenkins-ci.org/public/ - - - - - - - org.apache.maven.plugins - maven-eclipse-plugin - 2.9 - - - com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder - - - com.atlassw.tools.eclipse.checkstyle.CheckstyleNature - - - - .checkstyle - - - - - - - - - - - -]]> - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - 1.6 - 1.6 - - - - - - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - 2.9.1 - - src/test/checkstyle/sb-checks5.xml - - - - org.codehaus.mojo - findbugs-maven-plugin - 2.4.0 - - - org.codehaus.mojo - cobertura-maven-plugin - 2.5.1 - - - - - - - org.mockito - mockito-all - 1.9.0 - test - - - org.jenkins-ci.plugins - scriptler - 2.2 - - - - + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 1.532.3 + + + com.seitenbau.jenkins.plugins + dynamicparameter + hpi + 0.2.1-SNAPSHOT + + Jenkins Dynamic Parameter Plug-in + + This plugin allows build parameters with dynamically generated default values. + + + + + Apache License + All source code is under the Apache License. + + + + + scm:git:ssh://github.com/Seitenbau/sb-jenkins-dynamicparameter.git + scm:git:git@github.com:Seitenbau/sb-jenkins-dynamicparameter.git + https://github.com/Seitenbau/sb-jenkins-dynamicparameter.git + + + https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Dynamic+Parameter+Plug-in + + + + dimitarp + Dimitar Popov + + + baranowski + Christian Baranowski + + + + + + maven.jenkins-ci.org + http://repo.jenkins-ci.org/public/ + + + + + maven.jenkins-ci.org + http://repo.jenkins-ci.org/public/ + + + + + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.9 + + + com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder + + + com.atlassw.tools.eclipse.checkstyle.CheckstyleNature + + + + .checkstyle + + + + + + + + + + + +]]> + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + 1.6 + 1.6 + + + + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.9.1 + + src/test/checkstyle/sb-checks5.xml + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.4.0 + + + org.codehaus.mojo + cobertura-maven-plugin + 2.5.1 + + + + + + + org.mockito + mockito-all + 1.9.0 + test + + + org.jenkins-ci.plugins + scriptler + 2.2 + + + + diff --git a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java index b8bde36..0317647 100644 --- a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java +++ b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java @@ -1,249 +1,249 @@ -/* - * Copyright 2012 Seitenbau - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.seitenbau.jenkins.plugins.dynamicparameter; - -import hudson.model.ParameterValue; -import hudson.model.SimpleParameterDefinition; -import hudson.model.Label; -import hudson.model.StringParameterValue; -import hudson.remoting.Callable; -import hudson.remoting.VirtualChannel; - -import java.util.Collections; -import java.util.Map; -import java.util.List; -import java.util.UUID; -import java.util.logging.Level; -import java.util.logging.Logger; - -import net.sf.json.JSONArray; -import net.sf.json.JSONObject; -import net.sf.json.util.JSONUtils; - -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.StringUtils; -import org.kohsuke.stapler.StaplerRequest; - -import com.seitenbau.jenkins.plugins.dynamicparameter.scriptler.ScriptlerParameterDefinition; -import com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils; - -/** - * Base class for all script parameter definition classes. - */ -public abstract class BaseParameterDefinition extends SimpleParameterDefinition -{ - /** Serial version UID. */ - private static final long serialVersionUID = -4415132917610378545L; - - /** Logger. */ - protected static final Logger logger = Logger.getLogger(ScriptlerParameterDefinition.class - .getName()); - - /** UUID identifying the current parameter. */ - private final UUID _uuid; - - /** Flag showing if the script should be executed remotely. */ - private final Boolean _remote; - - /** - * Constructor. - * @param name parameter name - * @param description parameter description - * @param uuid UUID of the parameter definition - * @param remote flag showing if the script should be executed remotely - */ - protected BaseParameterDefinition(String name, String description, String uuid, Boolean remote) - { - super(name, description); - _remote = remote; - - if (StringUtils.length(uuid) == 0) - { - _uuid = UUID.randomUUID(); - } - else - { - _uuid = UUID.fromString(uuid); - } - } - - /** - * Return a Parameter value object for a command line parameter. - */ - @Override - public ParameterValue createValue(String value) - { - // Fix for issue https://github.com/Seitenbau/sb-jenkins-dynamicparameter/issues/3 - StringParameterValue parameterValue = createStringParameterValueFor(this.getName(), value); - return checkParameterValue(parameterValue); - } - - /** - * Should the script be executed to on a remote slave? - * @return {@code true} if the script should be executed remotely - */ - public final boolean isRemote() - { - if(_remote == null) { - return true; - } - return _remote; - } - - /** - * Get unique id for this parameter definition. - * @return the _uuid - */ - public final UUID getUUID() - { - return _uuid; - } - - /** - * Get the script result as a list. - * @return list of values if the script returns a non-null list; - * {@link Collections#EMPTY_LIST}, otherwise - */ - @SuppressWarnings("unchecked") - public final List getScriptResultAsList(Map parameters) - { - Object value = executeScript(parameters); - if (value instanceof List) - { - return (List) value; - } - String name = getName(); - String msg = String.format("Script parameter with name '%s' the value is not a instance of java.util.List the parameter value is : %s", name, value); - logger.info(msg); - return Collections.EMPTY_LIST; - } - - /** - * Get the script result as a string. - * @return the default value generated by the script or {@code null} - */ - public final String getScriptResultAsString(Map parameters) - { - Object value = executeScript(parameters); - return ObjectUtils.toString(value, null); - } - - @Override - public final ParameterValue createValue(StaplerRequest req, JSONObject jo) - { - final JSONObject parameterJsonModel = new JSONObject(false); - final Object value = jo.get("value"); - final String valueAsText; - if (JSONUtils.isArray(value)) - { - valueAsText = ((JSONArray)value).join(",", true); - } else - { - valueAsText = String.valueOf(value); - } - parameterJsonModel.put("name", jo.get("name")); - parameterJsonModel.put("value", valueAsText); - - StringParameterValue parameterValue = req.bindJSON(StringParameterValue.class, parameterJsonModel); - parameterValue.setDescription(getDescription()); - return checkParameterValue(parameterValue); - } - - /** - * Factory methods creates a String parameter value object for the given value. - * @param value of the object - * @return String parameter value object not null. - */ - private StringParameterValue createStringParameterValueFor(String name, String value) - { - String description = getDescription(); - StringParameterValue parameterValue = new StringParameterValue(name, value, description); - return parameterValue; - } - - /** - * Checks the validity of the given parameter value. The default implementation does nothing and - * should be overridden. - * @param value parameter value to check - * @return if the value is valid the same parameter value - * @throws IllegalArgumentException if the value in not valid - */ - protected StringParameterValue checkParameterValue(StringParameterValue value) - { - return value; - } - - /** - * Prepare a local call. - * @return call instance - * @throws Exception if a call instance cannot be created - */ - protected abstract Callable prepareLocalCall(Map parameters) throws Exception; - - /** - * Prepare a remote call. - * @param channel channel to the remote slave where the call will be executed - * @return call instance - * @throws Exception if a call instance cannot be created - */ - protected abstract Callable prepareRemoteCall(VirtualChannel channel, Map parameters) - throws Exception; - - /** - * Execute the current script either remotely or locally, depending on the remote flag. - * @return script result - */ - private final Object executeScript(Map parameters) - { - try - { - if (isRemote()) - { - Label label = JenkinsUtils.findProjectLabel(getUUID()); - if (label == null) - { - logger.warning(String.format( - "No label is assigned to project; script for parameter '%s' will be executed on master", - getName())); - } - else - { - VirtualChannel channel = JenkinsUtils.findActiveChannel(label); - if (channel == null) - { - logger.warning(String.format( - "Cannot find an active node of the label '%s' where to execute the script", - label.getDisplayName())); - } - else - { - Callable call = prepareRemoteCall(channel, parameters); - return channel.call(call); - } - } - } - Callable call = prepareLocalCall(parameters); - return call.call(); - } - catch (Throwable e) - { - String msg = String.format("Error during executing script for parameter '%s'", getName()); - logger.log(Level.SEVERE, msg, e); - } - return null; - } - -} +/* + * Copyright 2012 Seitenbau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.seitenbau.jenkins.plugins.dynamicparameter; + +import hudson.model.ParameterValue; +import hudson.model.SimpleParameterDefinition; +import hudson.model.Label; +import hudson.model.StringParameterValue; +import hudson.remoting.Callable; +import hudson.remoting.VirtualChannel; + +import java.util.Collections; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; +import net.sf.json.util.JSONUtils; + +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang.StringUtils; +import org.kohsuke.stapler.StaplerRequest; + +import com.seitenbau.jenkins.plugins.dynamicparameter.scriptler.ScriptlerParameterDefinition; +import com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils; + +/** + * Base class for all script parameter definition classes. + */ +public abstract class BaseParameterDefinition extends SimpleParameterDefinition +{ + /** Serial version UID. */ + private static final long serialVersionUID = -4415132917610378545L; + + /** Logger. */ + protected static final Logger logger = Logger.getLogger(ScriptlerParameterDefinition.class + .getName()); + + /** UUID identifying the current parameter. */ + private final UUID _uuid; + + /** Flag showing if the script should be executed remotely. */ + private final Boolean _remote; + + /** + * Constructor. + * @param name parameter name + * @param description parameter description + * @param uuid UUID of the parameter definition + * @param remote flag showing if the script should be executed remotely + */ + protected BaseParameterDefinition(String name, String description, String uuid, Boolean remote) + { + super(name, description); + _remote = remote; + + if (StringUtils.length(uuid) == 0) + { + _uuid = UUID.randomUUID(); + } + else + { + _uuid = UUID.fromString(uuid); + } + } + + /** + * Return a Parameter value object for a command line parameter. + */ + @Override + public ParameterValue createValue(String value) + { + // Fix for issue https://github.com/Seitenbau/sb-jenkins-dynamicparameter/issues/3 + StringParameterValue parameterValue = createStringParameterValueFor(this.getName(), value); + return checkParameterValue(parameterValue); + } + + /** + * Should the script be executed to on a remote slave? + * @return {@code true} if the script should be executed remotely + */ + public final boolean isRemote() + { + if(_remote == null) { + return true; + } + return _remote; + } + + /** + * Get unique id for this parameter definition. + * @return the _uuid + */ + public final UUID getUUID() + { + return _uuid; + } + + /** + * Get the script result as a list. + * @return list of values if the script returns a non-null list; + * {@link Collections#EMPTY_LIST}, otherwise + */ + @SuppressWarnings("unchecked") + public final List getScriptResultAsList(Map parameters) + { + Object value = executeScript(parameters); + if (value instanceof List) + { + return (List) value; + } + String name = getName(); + String msg = String.format("Script parameter with name '%s' the value is not a instance of java.util.List the parameter value is : %s", name, value); + logger.info(msg); + return Collections.EMPTY_LIST; + } + + /** + * Get the script result as a string. + * @return the default value generated by the script or {@code null} + */ + public final String getScriptResultAsString(Map parameters) + { + Object value = executeScript(parameters); + return ObjectUtils.toString(value, null); + } + + @Override + public ParameterValue createValue(StaplerRequest req, JSONObject jo) + { + final JSONObject parameterJsonModel = new JSONObject(false); + final Object value = jo.get("value"); + final String valueAsText; + if (JSONUtils.isArray(value)) + { + valueAsText = ((JSONArray)value).join(",", true); + } else + { + valueAsText = String.valueOf(value); + } + parameterJsonModel.put("name", jo.get("name")); + parameterJsonModel.put("value", valueAsText); + + StringParameterValue parameterValue = req.bindJSON(StringParameterValue.class, parameterJsonModel); + parameterValue.setDescription(getDescription()); + return checkParameterValue(parameterValue); + } + + /** + * Factory methods creates a String parameter value object for the given value. + * @param value of the object + * @return String parameter value object not null. + */ + private StringParameterValue createStringParameterValueFor(String name, String value) + { + String description = getDescription(); + StringParameterValue parameterValue = new StringParameterValue(name, value, description); + return parameterValue; + } + + /** + * Checks the validity of the given parameter value. The default implementation does nothing and + * should be overridden. + * @param value parameter value to check + * @return if the value is valid the same parameter value + * @throws IllegalArgumentException if the value in not valid + */ + protected ParameterValue checkParameterValue(ParameterValue value) + { + return value; + } + + /** + * Prepare a local call. + * @return call instance + * @throws Exception if a call instance cannot be created + */ + protected abstract Callable prepareLocalCall(Map parameters) throws Exception; + + /** + * Prepare a remote call. + * @param channel channel to the remote slave where the call will be executed + * @return call instance + * @throws Exception if a call instance cannot be created + */ + protected abstract Callable prepareRemoteCall(VirtualChannel channel, Map parameters) + throws Exception; + + /** + * Execute the current script either remotely or locally, depending on the remote flag. + * @return script result + */ + private final Object executeScript(Map parameters) + { + try + { + if (isRemote()) + { + Label label = JenkinsUtils.findProjectLabel(getUUID()); + if (label == null) + { + logger.warning(String.format( + "No label is assigned to project; script for parameter '%s' will be executed on master", + getName())); + } + else + { + VirtualChannel channel = JenkinsUtils.findActiveChannel(label); + if (channel == null) + { + logger.warning(String.format( + "Cannot find an active node of the label '%s' where to execute the script", + label.getDisplayName())); + } + else + { + Callable call = prepareRemoteCall(channel, parameters); + return channel.call(call); + } + } + } + Callable call = prepareLocalCall(parameters); + return call.call(); + } + catch (Throwable e) + { + String msg = String.format("Error during executing script for parameter '%s'", getName()); + logger.log(Level.SEVERE, msg, e); + } + return null; + } + +} diff --git a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition.java b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition.java new file mode 100644 index 0000000..be8b2f0 --- /dev/null +++ b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BooleanParameterDefinition.java @@ -0,0 +1,116 @@ +/* + * Copyright 2012 Seitenbau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.seitenbau.jenkins.plugins.dynamicparameter; + +import hudson.Extension; +import hudson.model.ParameterValue; +import hudson.model.BooleanParameterValue; + +import java.util.Collections; + +import net.sf.json.JSONObject; + +import org.jvnet.localizer.ResourceBundleHolder; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.StaplerRequest; + +/** Text parameter, with dynamically generated default value. */ +public class BooleanParameterDefinition extends ScriptParameterDefinition +{ + /** Serial version UID. */ + private static final long serialVersionUID = 3162331168133114084L; + + private final Boolean readonlyInputField; + + /** + * Constructor with the parameter which are injected by the jenkins runtime. + * + * @param name parameter name + * @param script script, which generates the parameter value + * @param description parameter description + * @param uuid identifier (optional) + * @param remote execute the script on a remote node + * @param readonlyInputField should the input field marked as read only true / false + * @param classPath the class path description + */ + @DataBoundConstructor + public BooleanParameterDefinition(String name, String script, String description, String uuid, + Boolean remote, Boolean readonlyInputField, String classPath) + { + super(name, script, description, uuid, remote, classPath); + this.readonlyInputField = readonlyInputField; + } + + + /** + * Execute the script and return the default value for this parameter. + * @return the default value generated by the script or {@code null} + */ + public final boolean getDefaultValue() + { + return Boolean.parseBoolean(getScriptResultAsString(Collections. emptyMap())); + } + + /** + * Return default parameter value - used by trigger mechanism. + */ + @Override + public ParameterValue getDefaultParameterValue() { + BooleanParameterValue booleanParameterValue = new BooleanParameterValue(getName(), getDefaultValue()); + return booleanParameterValue; + } + + /** + * Return a Parameter value object for a command line parameter. + */ + @Override + public ParameterValue createValue(String value) + { + BooleanParameterValue parameterValue = new BooleanParameterValue(getName(), Boolean.parseBoolean(value), getDescription()); + return checkParameterValue(parameterValue); + } + + @Override + public ParameterValue createValue(StaplerRequest req, JSONObject jo) + { + BooleanParameterValue value = req.bindJSON(BooleanParameterValue.class, jo); + value.setDescription(getDescription()); + return value; + } + + public final boolean isReadonlyInputField() + { + if(readonlyInputField == null) + { + return false; + } + return readonlyInputField; + } + + /** Parameter descriptor. */ + @Extension + public static final class DescriptorImpl extends BaseDescriptor + { + private static final String DISPLAY_NAME = "DisplayName"; + + @Override + public final String getDisplayName() + { + return ResourceBundleHolder.get(BooleanParameterDefinition.class).format(DISPLAY_NAME); + } + } + +} diff --git a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition.java b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition.java new file mode 100644 index 0000000..c73f1a5 --- /dev/null +++ b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/TextParameterDefinition.java @@ -0,0 +1,117 @@ +/* + * Copyright 2012 Seitenbau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.seitenbau.jenkins.plugins.dynamicparameter; + +import hudson.Extension; +import hudson.model.ParameterValue; +import hudson.model.StringParameterValue; +import hudson.model.TextParameterValue; + +import java.util.Collections; + +import net.sf.json.JSONObject; + +import org.jvnet.localizer.ResourceBundleHolder; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.StaplerRequest; + +/** Text parameter, with dynamically generated default value. */ +public class TextParameterDefinition extends ScriptParameterDefinition +{ + /** Serial version UID. */ + private static final long serialVersionUID = 3162331168133114084L; + + private final Boolean readonlyInputField; + + /** + * Constructor with the parameter which are injected by the jenkins runtime. + * + * @param name parameter name + * @param script script, which generates the parameter value + * @param description parameter description + * @param uuid identifier (optional) + * @param remote execute the script on a remote node + * @param readonlyInputField should the input field marked as read only true / false + * @param classPath the class path description + */ + @DataBoundConstructor + public TextParameterDefinition(String name, String script, String description, String uuid, + Boolean remote, Boolean readonlyInputField, String classPath) + { + super(name, script, description, uuid, remote, classPath); + this.readonlyInputField = readonlyInputField; + } + + + /** + * Execute the script and return the default value for this parameter. + * @return the default value generated by the script or {@code null} + */ + public final String getDefaultValue() + { + return getScriptResultAsString(Collections. emptyMap()); + } + + /** + * Return default parameter value - used by trigger mechanism. + */ + @Override + public ParameterValue getDefaultParameterValue() { + TextParameterValue stringParameterValue = new TextParameterValue(getName(), getDefaultValue()); + return stringParameterValue; + } + + /** + * Return a Parameter value object for a command line parameter. + */ + @Override + public ParameterValue createValue(String value) + { + StringParameterValue parameterValue = new TextParameterValue(getName(), value, getDescription()); + return checkParameterValue(parameterValue); + } + + @Override + public ParameterValue createValue(StaplerRequest req, JSONObject jo) + { + TextParameterValue value = req.bindJSON(TextParameterValue.class, jo); + value.setDescription(getDescription()); + return value; + } + + public final boolean isReadonlyInputField() + { + if(readonlyInputField == null) + { + return false; + } + return readonlyInputField; + } + + /** Parameter descriptor. */ + @Extension + public static final class DescriptorImpl extends BaseDescriptor + { + private static final String DISPLAY_NAME = "DisplayName"; + + @Override + public final String getDisplayName() + { + return ResourceBundleHolder.get(TextParameterDefinition.class).format(DISPLAY_NAME); + } + } + +} diff --git a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition.java b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition.java new file mode 100644 index 0000000..1914f9e --- /dev/null +++ b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition.java @@ -0,0 +1,124 @@ +/* + * Copyright 2012 Seitenbau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.seitenbau.jenkins.plugins.dynamicparameter.scriptler; + +import hudson.Extension; +import hudson.model.ParameterValue; +import hudson.model.BooleanParameterValue; + +import java.util.Map; +import java.util.Set; + +import net.sf.json.JSONObject; + +import org.jenkinsci.plugins.scriptler.config.Script; +import org.jvnet.localizer.ResourceBundleHolder; +import org.kohsuke.stapler.DataBoundConstructor; +import org.kohsuke.stapler.StaplerRequest; + +import com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils; + +/** Text parameter, with dynamically generated default value. */ +public class ScriptlerBooleanParameterDefinition extends ScriptlerParameterDefinition +{ + /** Serial version UID. */ + private static final long serialVersionUID = 3473431531782581400L; + + private final Boolean readonlyInputField; + + /** + * Constructor. + * @param name parameter name + * @param description parameter description + * @param uuid identifier (optional) + * @param scriptlerScriptId Scriptler script id + * @param parameters script parameters + * @param remote execute the script on a remote node + */ + @DataBoundConstructor + public ScriptlerBooleanParameterDefinition(String name, String description, String uuid, + String scriptlerScriptId, ScriptParameter[] parameters, Boolean remote, Boolean readonlyInputField) + { + super(name, description, uuid, scriptlerScriptId, parameters, remote); + this.readonlyInputField = readonlyInputField; + } + + /** + * Return default parameter value - used by trigger mechanism. + */ + @Override + public ParameterValue getDefaultParameterValue() { + BooleanParameterValue booleanParameterValue = new BooleanParameterValue(getName(), getDefaultValue()); + return booleanParameterValue; + } + + /** + * Return a Parameter value object for a command line parameter. + */ + @Override + public ParameterValue createValue(String value) + { + BooleanParameterValue parameterValue = new BooleanParameterValue(getName(), Boolean.parseBoolean(value), getDescription()); + return checkParameterValue(parameterValue); + } + + @Override + public ParameterValue createValue(StaplerRequest req, JSONObject jo) + { + BooleanParameterValue value = req.bindJSON(BooleanParameterValue.class, jo); + value.setDescription(getDescription()); + return value; + } + + /** + * Execute the script and return the default value for this parameter. + * @return the default value generated by the script or {@code null} + */ + public final boolean getDefaultValue() + { + Map parameters = getParametersAsMap(); + return Boolean.parseBoolean(getScriptResultAsString(parameters)); + } + + public boolean isReadonlyInputField() + { + if(readonlyInputField == null) + { + return false; + } + return readonlyInputField; + } + + /** Parameter descriptor. */ + @Extension + public static final class DescriptorImpl extends ParameterDescriptor + { + private static final String DISPLAY_NAME = "DisplayName"; + + @Override + public final String getDisplayName() + { + return ResourceBundleHolder.get(ScriptlerBooleanParameterDefinition.class) + .format(DISPLAY_NAME); + } + + public Set + + diff --git a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/config_de.properties b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/config_de.properties new file mode 100644 index 0000000..d89506f --- /dev/null +++ b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/config_de.properties @@ -0,0 +1,26 @@ +# Copyright 2012 Seitenbau +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +Name=Name +Remote\ Script=Remote-Skript +Description=Beschreibung +WarnNoScript=Keine Skripte +Script=Skript +Parameter\ name=Parameter Name +Parameter\ value=Parameter Wert +Delete\ Parameter=Parameter Löschen +Add\ Parameter=Parameter Zufügen +View\ Script=Skript Anzeigen +Parameters=Parameter +Readonly\ Input\ Field=Eingabefeld kann nicht editiert werden diff --git a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/index.jelly b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/index.jelly new file mode 100644 index 0000000..2697166 --- /dev/null +++ b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerBooleanParameterDefinition/index.jelly @@ -0,0 +1,30 @@ + + + +
+ + + + + + + +
+
+
diff --git a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerChoiceParameterDefinition/index.jelly b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerChoiceParameterDefinition/index.jelly index 1f563ad..a52df03 100644 --- a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerChoiceParameterDefinition/index.jelly +++ b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerChoiceParameterDefinition/index.jelly @@ -32,19 +32,19 @@ limitations under the License. - + - + diff --git a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition.properties b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition.properties new file mode 100644 index 0000000..8663686 --- /dev/null +++ b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition.properties @@ -0,0 +1,15 @@ +# Copyright 2012 Seitenbau +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +DisplayName=Dynamic Text Parameter (Scriptler) diff --git a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config.jelly b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config.jelly new file mode 100644 index 0000000..d56423d --- /dev/null +++ b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config.jelly @@ -0,0 +1,108 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config_de.properties b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config_de.properties new file mode 100644 index 0000000..d89506f --- /dev/null +++ b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/config_de.properties @@ -0,0 +1,26 @@ +# Copyright 2012 Seitenbau +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +Name=Name +Remote\ Script=Remote-Skript +Description=Beschreibung +WarnNoScript=Keine Skripte +Script=Skript +Parameter\ name=Parameter Name +Parameter\ value=Parameter Wert +Delete\ Parameter=Parameter Löschen +Add\ Parameter=Parameter Zufügen +View\ Script=Skript Anzeigen +Parameters=Parameter +Readonly\ Input\ Field=Eingabefeld kann nicht editiert werden diff --git a/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/index.jelly b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/index.jelly new file mode 100644 index 0000000..bb2abb2 --- /dev/null +++ b/src/main/resources/com/seitenbau/jenkins/plugins/dynamicparameter/scriptler/ScriptlerTextParameterDefinition/index.jelly @@ -0,0 +1,30 @@ + + + +
+ + + + + + + +
+
+
From 5bfa232bcc4226eeddd86b1db350d271928e1a5d Mon Sep 17 00:00:00 2001 From: greg2001 Date: Wed, 28 Jan 2015 16:41:17 +0100 Subject: [PATCH 2/5] For scripts executed locally, expose the current Jenkins job as "currentJob" variable --- .../plugins/dynamicparameter/BaseParameterDefinition.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java index 0317647..37a95b2 100644 --- a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java +++ b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java @@ -23,6 +23,7 @@ import hudson.remoting.VirtualChannel; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.UUID; @@ -235,7 +236,11 @@ private final Object executeScript(Map parameters) } } } - Callable call = prepareLocalCall(parameters); + + // for scripts executed locally, expose the current Jenkins job as "currentJob" variable + Map p = new HashMap(parameters); + p.put("currentJob", JenkinsUtils.findCurrentProject(getUUID())); + Callable call = prepareLocalCall((Map)p); return call.call(); } catch (Throwable e) From 85a29201689e2bd61d7d47fd6db4ff00f6bda82d Mon Sep 17 00:00:00 2001 From: greg2001 Date: Tue, 3 Mar 2015 11:08:11 +0100 Subject: [PATCH 3/5] Fix CRLF --- pom.xml | 286 +++++----- .../BaseParameterDefinition.java | 508 +++++++++--------- 2 files changed, 397 insertions(+), 397 deletions(-) diff --git a/pom.xml b/pom.xml index a20a9da..aed17be 100644 --- a/pom.xml +++ b/pom.xml @@ -1,143 +1,143 @@ - - - 4.0.0 - - org.jenkins-ci.plugins - plugin - 1.532.3 - - - com.seitenbau.jenkins.plugins - dynamicparameter - hpi - 0.2.1-SNAPSHOT - - Jenkins Dynamic Parameter Plug-in - - This plugin allows build parameters with dynamically generated default values. - - - - - Apache License - All source code is under the Apache License. - - - - - scm:git:ssh://github.com/Seitenbau/sb-jenkins-dynamicparameter.git - scm:git:git@github.com:Seitenbau/sb-jenkins-dynamicparameter.git - https://github.com/Seitenbau/sb-jenkins-dynamicparameter.git - - - https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Dynamic+Parameter+Plug-in - - - - dimitarp - Dimitar Popov - - - baranowski - Christian Baranowski - - - - - - maven.jenkins-ci.org - http://repo.jenkins-ci.org/public/ - - - - - maven.jenkins-ci.org - http://repo.jenkins-ci.org/public/ - - - - - - - org.apache.maven.plugins - maven-eclipse-plugin - 2.9 - - - com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder - - - com.atlassw.tools.eclipse.checkstyle.CheckstyleNature - - - - .checkstyle - - - - - - - - - - - -]]> - - - - - - - - org.apache.maven.plugins - maven-compiler-plugin - - - 1.6 - 1.6 - - - - - - - - - - org.apache.maven.plugins - maven-checkstyle-plugin - 2.9.1 - - src/test/checkstyle/sb-checks5.xml - - - - org.codehaus.mojo - findbugs-maven-plugin - 2.4.0 - - - org.codehaus.mojo - cobertura-maven-plugin - 2.5.1 - - - - - - - org.mockito - mockito-all - 1.9.0 - test - - - org.jenkins-ci.plugins - scriptler - 2.2 - - - - + + + 4.0.0 + + org.jenkins-ci.plugins + plugin + 1.532.3 + + + com.seitenbau.jenkins.plugins + dynamicparameter + hpi + 0.2.1-SNAPSHOT + + Jenkins Dynamic Parameter Plug-in + + This plugin allows build parameters with dynamically generated default values. + + + + + Apache License + All source code is under the Apache License. + + + + + scm:git:ssh://github.com/Seitenbau/sb-jenkins-dynamicparameter.git + scm:git:git@github.com:Seitenbau/sb-jenkins-dynamicparameter.git + https://github.com/Seitenbau/sb-jenkins-dynamicparameter.git + + + https://wiki.jenkins-ci.org/display/JENKINS/Jenkins+Dynamic+Parameter+Plug-in + + + + dimitarp + Dimitar Popov + + + baranowski + Christian Baranowski + + + + + + maven.jenkins-ci.org + http://repo.jenkins-ci.org/public/ + + + + + maven.jenkins-ci.org + http://repo.jenkins-ci.org/public/ + + + + + + + org.apache.maven.plugins + maven-eclipse-plugin + 2.9 + + + com.atlassw.tools.eclipse.checkstyle.CheckstyleBuilder + + + com.atlassw.tools.eclipse.checkstyle.CheckstyleNature + + + + .checkstyle + + + + + + + + + + + +]]> + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + + 1.6 + 1.6 + + + + + + + + + + org.apache.maven.plugins + maven-checkstyle-plugin + 2.9.1 + + src/test/checkstyle/sb-checks5.xml + + + + org.codehaus.mojo + findbugs-maven-plugin + 2.4.0 + + + org.codehaus.mojo + cobertura-maven-plugin + 2.5.1 + + + + + + + org.mockito + mockito-all + 1.9.0 + test + + + org.jenkins-ci.plugins + scriptler + 2.2 + + + + diff --git a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java index 37a95b2..a635486 100644 --- a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java +++ b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/BaseParameterDefinition.java @@ -1,254 +1,254 @@ -/* - * Copyright 2012 Seitenbau - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.seitenbau.jenkins.plugins.dynamicparameter; - -import hudson.model.ParameterValue; -import hudson.model.SimpleParameterDefinition; -import hudson.model.Label; -import hudson.model.StringParameterValue; -import hudson.remoting.Callable; -import hudson.remoting.VirtualChannel; - -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.UUID; -import java.util.logging.Level; -import java.util.logging.Logger; - -import net.sf.json.JSONArray; -import net.sf.json.JSONObject; -import net.sf.json.util.JSONUtils; - -import org.apache.commons.lang.ObjectUtils; -import org.apache.commons.lang.StringUtils; -import org.kohsuke.stapler.StaplerRequest; - -import com.seitenbau.jenkins.plugins.dynamicparameter.scriptler.ScriptlerParameterDefinition; -import com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils; - -/** - * Base class for all script parameter definition classes. - */ -public abstract class BaseParameterDefinition extends SimpleParameterDefinition -{ - /** Serial version UID. */ - private static final long serialVersionUID = -4415132917610378545L; - - /** Logger. */ - protected static final Logger logger = Logger.getLogger(ScriptlerParameterDefinition.class - .getName()); - - /** UUID identifying the current parameter. */ - private final UUID _uuid; - - /** Flag showing if the script should be executed remotely. */ - private final Boolean _remote; - - /** - * Constructor. - * @param name parameter name - * @param description parameter description - * @param uuid UUID of the parameter definition - * @param remote flag showing if the script should be executed remotely - */ - protected BaseParameterDefinition(String name, String description, String uuid, Boolean remote) - { - super(name, description); - _remote = remote; - - if (StringUtils.length(uuid) == 0) - { - _uuid = UUID.randomUUID(); - } - else - { - _uuid = UUID.fromString(uuid); - } - } - - /** - * Return a Parameter value object for a command line parameter. - */ - @Override - public ParameterValue createValue(String value) - { - // Fix for issue https://github.com/Seitenbau/sb-jenkins-dynamicparameter/issues/3 - StringParameterValue parameterValue = createStringParameterValueFor(this.getName(), value); - return checkParameterValue(parameterValue); - } - - /** - * Should the script be executed to on a remote slave? - * @return {@code true} if the script should be executed remotely - */ - public final boolean isRemote() - { - if(_remote == null) { - return true; - } - return _remote; - } - - /** - * Get unique id for this parameter definition. - * @return the _uuid - */ - public final UUID getUUID() - { - return _uuid; - } - - /** - * Get the script result as a list. - * @return list of values if the script returns a non-null list; - * {@link Collections#EMPTY_LIST}, otherwise - */ - @SuppressWarnings("unchecked") - public final List getScriptResultAsList(Map parameters) - { - Object value = executeScript(parameters); - if (value instanceof List) - { - return (List) value; - } - String name = getName(); - String msg = String.format("Script parameter with name '%s' the value is not a instance of java.util.List the parameter value is : %s", name, value); - logger.info(msg); - return Collections.EMPTY_LIST; - } - - /** - * Get the script result as a string. - * @return the default value generated by the script or {@code null} - */ - public final String getScriptResultAsString(Map parameters) - { - Object value = executeScript(parameters); - return ObjectUtils.toString(value, null); - } - - @Override - public ParameterValue createValue(StaplerRequest req, JSONObject jo) - { - final JSONObject parameterJsonModel = new JSONObject(false); - final Object value = jo.get("value"); - final String valueAsText; - if (JSONUtils.isArray(value)) - { - valueAsText = ((JSONArray)value).join(",", true); - } else - { - valueAsText = String.valueOf(value); - } - parameterJsonModel.put("name", jo.get("name")); - parameterJsonModel.put("value", valueAsText); - - StringParameterValue parameterValue = req.bindJSON(StringParameterValue.class, parameterJsonModel); - parameterValue.setDescription(getDescription()); - return checkParameterValue(parameterValue); - } - - /** - * Factory methods creates a String parameter value object for the given value. - * @param value of the object - * @return String parameter value object not null. - */ - private StringParameterValue createStringParameterValueFor(String name, String value) - { - String description = getDescription(); - StringParameterValue parameterValue = new StringParameterValue(name, value, description); - return parameterValue; - } - - /** - * Checks the validity of the given parameter value. The default implementation does nothing and - * should be overridden. - * @param value parameter value to check - * @return if the value is valid the same parameter value - * @throws IllegalArgumentException if the value in not valid - */ - protected ParameterValue checkParameterValue(ParameterValue value) - { - return value; - } - - /** - * Prepare a local call. - * @return call instance - * @throws Exception if a call instance cannot be created - */ - protected abstract Callable prepareLocalCall(Map parameters) throws Exception; - - /** - * Prepare a remote call. - * @param channel channel to the remote slave where the call will be executed - * @return call instance - * @throws Exception if a call instance cannot be created - */ - protected abstract Callable prepareRemoteCall(VirtualChannel channel, Map parameters) - throws Exception; - - /** - * Execute the current script either remotely or locally, depending on the remote flag. - * @return script result - */ - private final Object executeScript(Map parameters) - { - try - { - if (isRemote()) - { - Label label = JenkinsUtils.findProjectLabel(getUUID()); - if (label == null) - { - logger.warning(String.format( - "No label is assigned to project; script for parameter '%s' will be executed on master", - getName())); - } - else - { - VirtualChannel channel = JenkinsUtils.findActiveChannel(label); - if (channel == null) - { - logger.warning(String.format( - "Cannot find an active node of the label '%s' where to execute the script", - label.getDisplayName())); - } - else - { - Callable call = prepareRemoteCall(channel, parameters); - return channel.call(call); - } - } - } - - // for scripts executed locally, expose the current Jenkins job as "currentJob" variable - Map p = new HashMap(parameters); - p.put("currentJob", JenkinsUtils.findCurrentProject(getUUID())); - Callable call = prepareLocalCall((Map)p); - return call.call(); - } - catch (Throwable e) - { - String msg = String.format("Error during executing script for parameter '%s'", getName()); - logger.log(Level.SEVERE, msg, e); - } - return null; - } - -} +/* + * Copyright 2012 Seitenbau + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.seitenbau.jenkins.plugins.dynamicparameter; + +import hudson.model.ParameterValue; +import hudson.model.SimpleParameterDefinition; +import hudson.model.Label; +import hudson.model.StringParameterValue; +import hudson.remoting.Callable; +import hudson.remoting.VirtualChannel; + +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.UUID; +import java.util.logging.Level; +import java.util.logging.Logger; + +import net.sf.json.JSONArray; +import net.sf.json.JSONObject; +import net.sf.json.util.JSONUtils; + +import org.apache.commons.lang.ObjectUtils; +import org.apache.commons.lang.StringUtils; +import org.kohsuke.stapler.StaplerRequest; + +import com.seitenbau.jenkins.plugins.dynamicparameter.scriptler.ScriptlerParameterDefinition; +import com.seitenbau.jenkins.plugins.dynamicparameter.util.JenkinsUtils; + +/** + * Base class for all script parameter definition classes. + */ +public abstract class BaseParameterDefinition extends SimpleParameterDefinition +{ + /** Serial version UID. */ + private static final long serialVersionUID = -4415132917610378545L; + + /** Logger. */ + protected static final Logger logger = Logger.getLogger(ScriptlerParameterDefinition.class + .getName()); + + /** UUID identifying the current parameter. */ + private final UUID _uuid; + + /** Flag showing if the script should be executed remotely. */ + private final Boolean _remote; + + /** + * Constructor. + * @param name parameter name + * @param description parameter description + * @param uuid UUID of the parameter definition + * @param remote flag showing if the script should be executed remotely + */ + protected BaseParameterDefinition(String name, String description, String uuid, Boolean remote) + { + super(name, description); + _remote = remote; + + if (StringUtils.length(uuid) == 0) + { + _uuid = UUID.randomUUID(); + } + else + { + _uuid = UUID.fromString(uuid); + } + } + + /** + * Return a Parameter value object for a command line parameter. + */ + @Override + public ParameterValue createValue(String value) + { + // Fix for issue https://github.com/Seitenbau/sb-jenkins-dynamicparameter/issues/3 + StringParameterValue parameterValue = createStringParameterValueFor(this.getName(), value); + return checkParameterValue(parameterValue); + } + + /** + * Should the script be executed to on a remote slave? + * @return {@code true} if the script should be executed remotely + */ + public final boolean isRemote() + { + if(_remote == null) { + return true; + } + return _remote; + } + + /** + * Get unique id for this parameter definition. + * @return the _uuid + */ + public final UUID getUUID() + { + return _uuid; + } + + /** + * Get the script result as a list. + * @return list of values if the script returns a non-null list; + * {@link Collections#EMPTY_LIST}, otherwise + */ + @SuppressWarnings("unchecked") + public final List getScriptResultAsList(Map parameters) + { + Object value = executeScript(parameters); + if (value instanceof List) + { + return (List) value; + } + String name = getName(); + String msg = String.format("Script parameter with name '%s' the value is not a instance of java.util.List the parameter value is : %s", name, value); + logger.info(msg); + return Collections.EMPTY_LIST; + } + + /** + * Get the script result as a string. + * @return the default value generated by the script or {@code null} + */ + public final String getScriptResultAsString(Map parameters) + { + Object value = executeScript(parameters); + return ObjectUtils.toString(value, null); + } + + @Override + public ParameterValue createValue(StaplerRequest req, JSONObject jo) + { + final JSONObject parameterJsonModel = new JSONObject(false); + final Object value = jo.get("value"); + final String valueAsText; + if (JSONUtils.isArray(value)) + { + valueAsText = ((JSONArray)value).join(",", true); + } else + { + valueAsText = String.valueOf(value); + } + parameterJsonModel.put("name", jo.get("name")); + parameterJsonModel.put("value", valueAsText); + + StringParameterValue parameterValue = req.bindJSON(StringParameterValue.class, parameterJsonModel); + parameterValue.setDescription(getDescription()); + return checkParameterValue(parameterValue); + } + + /** + * Factory methods creates a String parameter value object for the given value. + * @param value of the object + * @return String parameter value object not null. + */ + private StringParameterValue createStringParameterValueFor(String name, String value) + { + String description = getDescription(); + StringParameterValue parameterValue = new StringParameterValue(name, value, description); + return parameterValue; + } + + /** + * Checks the validity of the given parameter value. The default implementation does nothing and + * should be overridden. + * @param value parameter value to check + * @return if the value is valid the same parameter value + * @throws IllegalArgumentException if the value in not valid + */ + protected ParameterValue checkParameterValue(ParameterValue value) + { + return value; + } + + /** + * Prepare a local call. + * @return call instance + * @throws Exception if a call instance cannot be created + */ + protected abstract Callable prepareLocalCall(Map parameters) throws Exception; + + /** + * Prepare a remote call. + * @param channel channel to the remote slave where the call will be executed + * @return call instance + * @throws Exception if a call instance cannot be created + */ + protected abstract Callable prepareRemoteCall(VirtualChannel channel, Map parameters) + throws Exception; + + /** + * Execute the current script either remotely or locally, depending on the remote flag. + * @return script result + */ + private final Object executeScript(Map parameters) + { + try + { + if (isRemote()) + { + Label label = JenkinsUtils.findProjectLabel(getUUID()); + if (label == null) + { + logger.warning(String.format( + "No label is assigned to project; script for parameter '%s' will be executed on master", + getName())); + } + else + { + VirtualChannel channel = JenkinsUtils.findActiveChannel(label); + if (channel == null) + { + logger.warning(String.format( + "Cannot find an active node of the label '%s' where to execute the script", + label.getDisplayName())); + } + else + { + Callable call = prepareRemoteCall(channel, parameters); + return channel.call(call); + } + } + } + + // for "local" scripts, expose the current Jenkins job as "currentJob" variable + Map p = new HashMap(parameters); + p.put("currentJob", JenkinsUtils.findCurrentProject(getUUID())); + Callable call = prepareLocalCall((Map)p); + return call.call(); + } + catch (Throwable e) + { + String msg = String.format("Error during executing script for parameter '%s'", getName()); + logger.log(Level.SEVERE, msg, e); + } + return null; + } + +} From 02230c5041cafea3fd6760300c02f8f5741b8494 Mon Sep 17 00:00:00 2001 From: greg2001 Date: Wed, 23 Sep 2020 14:13:42 +0200 Subject: [PATCH 4/5] fix serialization bug --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index aed17be..18120fe 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ com.seitenbau.jenkins.plugins dynamicparameter hpi - 0.2.1-SNAPSHOT + 0.2.1 Jenkins Dynamic Parameter Plug-in From d9f7d6b3b368f22fd14ad36ccb09d34f6bedfe7b Mon Sep 17 00:00:00 2001 From: greg2001 Date: Wed, 23 Sep 2020 14:26:21 +0200 Subject: [PATCH 5/5] fix serialization bug --- .../ScriptParameterDefinition.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/ScriptParameterDefinition.java b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/ScriptParameterDefinition.java index a878996..3b2ccdc 100644 --- a/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/ScriptParameterDefinition.java +++ b/src/main/java/com/seitenbau/jenkins/plugins/dynamicparameter/ScriptParameterDefinition.java @@ -51,11 +51,11 @@ public abstract class ScriptParameterDefinition extends BaseParameterDefinition private final String _script; /** Local class path. */ - private final FilePath _localBaseDirectory; - + private final String _localBaseDirectory; + /** Remote class path. */ private final String _remoteBaseDirectory; - + /** Class path. */ private final String _classPath; @@ -72,7 +72,7 @@ protected ScriptParameterDefinition(String name, String script, String descripti { super(name, description, uuid, remote); - _localBaseDirectory = new FilePath(DynamicParameterConfiguration.INSTANCE.getBaseDirectoryFile()); + _localBaseDirectory = DynamicParameterConfiguration.INSTANCE.getBaseDirectoryFile().toString(); _remoteBaseDirectory = DEFAULT_REMOTE_CLASSPATH; _classPath = classPath; _script = script; @@ -82,7 +82,7 @@ protected ScriptParameterDefinition(String name, String script, String descripti * Local class path directory. * @return directory on the local node */ - public final FilePath getLocalBaseDirectory() + public final String getLocalBaseDirectory() { return _localBaseDirectory; } @@ -149,7 +149,7 @@ private FilePath[] setupLocalClassPaths() for (int i = 0; i < localClassPaths.length; i++) { String path = paths[i]; - FilePath localClassPath = new FilePath(getLocalBaseDirectory(), path); + FilePath localClassPath = new FilePath(new FilePath(new File(getLocalBaseDirectory())), path); localClassPaths[i] = localClassPath; } return localClassPaths; @@ -174,7 +174,7 @@ private FilePath[] setupRemoteClassPaths(VirtualChannel channel) throws IOExcept { String path = paths[i]; - FilePath localClassPath = new FilePath(getLocalBaseDirectory(), path); + FilePath localClassPath = new FilePath(new FilePath(new File(getLocalBaseDirectory())), path); FilePath remoteClassPath = new FilePath(remoteBaseDirectory, path); localClassPath.copyRecursiveTo(remoteClassPath);