forked from jenkinsci/dynamicparameter-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathScriptlerParameterDefinition.java
More file actions
191 lines (168 loc) · 5.7 KB
/
Copy pathScriptlerParameterDefinition.java
File metadata and controls
191 lines (168 loc) · 5.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
* 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 com.seitenbau.jenkins.plugins.dynamicparameter.BaseParameterDefinition;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import hudson.remoting.VirtualChannel;
import org.jenkinsci.plugins.scriptler.config.Parameter;
import org.jenkinsci.plugins.scriptler.config.Script;
import org.jenkinsci.plugins.scriptler.util.GroovyScript;
import org.jenkinsci.plugins.scriptler.util.ScriptHelper;
import org.kohsuke.stapler.DataBoundConstructor;
import java.util.HashMap;
import java.util.Map;
/** Base class for all dynamic parameters using Scriptler scripts. */
public abstract class ScriptlerParameterDefinition extends BaseParameterDefinition
{
/** Serial version UID. */
private static final long serialVersionUID = -8947340128208418404L;
/** Scriptler script id. */
private final String _scriptlerScriptId;
/** Script parameters. */
private final ScriptParameter[] _parameters;
/**
* Constructor.
* @param name parameter name
* @param description parameter description
* @param uuid UUID of the parameter definition
* @param scriptlerScriptId Scriptler script identifier
* @param parameters script parameters
* @param remote flag showing if the script should be executed remotely
*/
protected ScriptlerParameterDefinition(String name, String description, String uuid,
String scriptlerScriptId, ScriptParameter[] parameters, Boolean remote)
{
super(name, description, uuid, remote);
_scriptlerScriptId = scriptlerScriptId;
_parameters = parameters;
}
/**
* Get Scriptler script id.
* @return scriptler script id
*/
public final String getScriptlerScriptId()
{
return _scriptlerScriptId;
}
/**
* Get script parameters.
* @return array with script parameters
*/
public final ScriptParameter[] getParameters()
{
return _parameters;
}
@Override
protected ParameterizedScriptCall prepareLocalCall(Map<String, String> parameters) throws Exception
{
return prepareCall(parameters);
}
@Override
protected ParameterizedScriptCall prepareRemoteCall(VirtualChannel channel, Map<String, String> parameters) throws Exception
{
return prepareCall(parameters);
}
/**
* Prepare a call of the script.
* @return call instance
* @throws Exception if the script with the given Scriptler identifier does not exist
*/
private ParameterizedScriptCall prepareCall(Map<String, String> parmeterParams) throws Exception
{
String scriptId = getScriptlerScriptId();
Script script = ScriptHelper.getScript(scriptId, true);
if (script == null)
{
throw new Exception(String.format("No script with Scriplter ID '%s' exists", scriptId));
}
// Read all parameters from job configuration
Map<String, String> parameters = getParametersAsMap();
// Set default values, in case the value has not been set in job configuration
for (Parameter parameter : script.getParameters())
{
if (!parameters.containsKey(parameter.getName()))
{
parameters.put(parameter.getName(), parameter.getValue());
}
}
// add all parameter specific parameters to the script call
parameters.putAll(parmeterParams);
// create the script call
ParameterizedScriptCall call = new ParameterizedScriptCall(script.script, parameters);
return call;
}
/**
* Convert the list of parameters to a map.
* @return a {@link Map} with script parameters
*/
protected Map<String, String> getParametersAsMap()
{
ScriptParameter[] parameters = getParameters();
Map<String, String> map = new HashMap<String, String>(parameters.length);
for (Parameter parameter : parameters)
{
map.put(parameter.getName(), parameter.getValue());
}
return map;
}
/**
* Script parameter which has a data bound constructor.
*/
public static final class ScriptParameter extends Parameter
{
/** Serial version UID. */
private static final long serialVersionUID = -2154469334885184388L;
/**
* Data bound constructor.
* @param name parameter name
* @param value parameter value
*/
@DataBoundConstructor
public ScriptParameter(String name, String value)
{
super(name, value);
}
}
/**
* Remote call implementation.
*/
public static final class ParameterizedScriptCall implements Callable<Object, Throwable>
{
private static final long serialVersionUID = -8281488869664773282L;
private final String _remoteScript;
private final Parameter[] _parameters;
/**
* Constructor.
* @param script script to execute
* @param parameters parameters
*/
public ParameterizedScriptCall(String script, Map<String, String> parameters)
{
_remoteScript = script;
_parameters = new Parameter[parameters.size()];
int i = 0;
for (Map.Entry<String, String> e : parameters.entrySet()) {
_parameters[i++] = new Parameter(e.getKey(), e.getValue());
}
}
@Override
public Object call()
{
return new GroovyScript(_remoteScript, _parameters, true, TaskListener.NULL).call();
}
}
}