3
3
import com .cloudbees .plugins .credentials .common .StandardCredentials ;
4
4
import hudson .Extension ;
5
5
import hudson .model .Item ;
6
+ import io .jenkins .plugins .restlistparam .logic .ValueResolver ;
7
+ import io .jenkins .plugins .restlistparam .model .ValueItem ;
6
8
import hudson .model .ParameterDefinition ;
7
9
import hudson .model .ParameterValue ;
8
10
import hudson .model .SimpleParameterDefinition ;
21
23
import org .kohsuke .stapler .*;
22
24
import org .kohsuke .stapler .verb .POST ;
23
25
24
- import javax .annotation .CheckForNull ;
25
26
import javax .annotation .Nonnull ;
26
27
import java .util .Collections ;
27
28
import java .util .List ;
@@ -36,23 +37,25 @@ public final class RestListParameterDefinition extends SimpleParameterDefinition
36
37
private final String credentialId ;
37
38
private final MimeType mimeType ;
38
39
private final String valueExpression ;
40
+ private String displayExpression ;
39
41
private ValueOrder valueOrder ;
40
42
private String defaultValue ;
41
43
private String filter ;
42
44
private Integer cacheTime ;
43
45
private String errorMsg ;
44
- private List <String > values ;
46
+ private List <ValueItem > values ;
45
47
46
48
@ DataBoundConstructor
47
49
public RestListParameterDefinition (final String name ,
48
50
final String description ,
49
51
final String restEndpoint ,
50
52
final String credentialId ,
51
53
final MimeType mimeType ,
52
- final String valueExpression )
54
+ final String valueExpression ,
55
+ final String displayExpression )
53
56
{
54
57
this (name , description , restEndpoint , credentialId , mimeType , valueExpression ,
55
- ValueOrder .NONE , ".*" , config .getCacheTime (), "" );
58
+ displayExpression , ValueOrder .NONE , ".*" , config .getCacheTime (), "" );
56
59
}
57
60
58
61
public RestListParameterDefinition (final String name ,
@@ -61,6 +64,7 @@ public RestListParameterDefinition(final String name,
61
64
final String credentialId ,
62
65
final MimeType mimeType ,
63
66
final String valueExpression ,
67
+ final String displayExpression ,
64
68
final ValueOrder valueOrder ,
65
69
final String filter ,
66
70
final Integer cacheTime ,
@@ -71,6 +75,9 @@ public RestListParameterDefinition(final String name,
71
75
this .mimeType = mimeType ;
72
76
this .valueExpression = valueExpression ;
73
77
this .credentialId = StringUtils .isNotBlank (credentialId ) ? credentialId : "" ;
78
+ if (mimeType == MimeType .APPLICATION_JSON ) {
79
+ this .displayExpression = StringUtils .isNotBlank (displayExpression ) ? displayExpression : "$" ;
80
+ }
74
81
this .defaultValue = StringUtils .isNotBlank (defaultValue ) ? defaultValue : "" ;
75
82
this .valueOrder = valueOrder != null ? valueOrder : ValueOrder .NONE ;
76
83
this .filter = StringUtils .isNotBlank (filter ) ? filter : ".*" ;
@@ -99,6 +106,18 @@ public String getFilter() {
99
106
return filter ;
100
107
}
101
108
109
+ public String getDisplayExpression () {
110
+ if (mimeType == MimeType .APPLICATION_JSON ) {
111
+ return StringUtils .isNotBlank (displayExpression ) ? displayExpression : "$" ;
112
+ }
113
+ return "" ;
114
+ }
115
+
116
+ @ DataBoundSetter
117
+ public void setDisplayExpression (final String displayExpression ) {
118
+ this .displayExpression = displayExpression ;
119
+ }
120
+
102
121
@ DataBoundSetter
103
122
public void setValueOrder (final ValueOrder valueOrder ) {
104
123
this .valueOrder = valueOrder ;
@@ -139,15 +158,16 @@ public String getErrorMsg() {
139
158
return errorMsg ;
140
159
}
141
160
142
- public List <String > getValues () {
161
+ public List <ValueItem > getValues () {
143
162
Optional <StandardCredentials > credentials = CredentialsUtils .findCredentials (null , credentialId );
144
163
145
- ResultContainer <List <String >> container = RestValueService .get (
164
+ ResultContainer <List <ValueItem >> container = RestValueService .get (
146
165
getRestEndpoint (),
147
166
credentials .orElse (null ),
148
167
getMimeType (),
149
168
getCacheTime (),
150
169
getValueExpression (),
170
+ getDisplayExpression (),
151
171
getFilter (),
152
172
getValueOrder ());
153
173
@@ -162,7 +182,8 @@ public ParameterDefinition copyWithDefaultValue(final ParameterValue defaultValu
162
182
RestListParameterValue value = (RestListParameterValue ) defaultValue ;
163
183
return new RestListParameterDefinition (
164
184
getName (), getDescription (), getRestEndpoint (), getCredentialId (), getMimeType (),
165
- getValueExpression (), getValueOrder (), getFilter (), getCacheTime (), value .getValue ());
185
+ getValueExpression (), getDisplayExpression (), getValueOrder (), getFilter (), getCacheTime (),
186
+ ValueResolver .parseDisplayValue (getMimeType (), value .getValue (), displayExpression ));
166
187
}
167
188
else {
168
189
return this ;
@@ -172,16 +193,17 @@ public ParameterDefinition copyWithDefaultValue(final ParameterValue defaultValu
172
193
@ Override
173
194
public ParameterValue createValue (final String value ) {
174
195
RestListParameterValue parameterValue = new RestListParameterValue (getName (), value , getDescription ());
196
+
175
197
checkValue (parameterValue );
176
198
return parameterValue ;
177
199
}
178
200
179
201
@ Override
180
- @ CheckForNull
181
202
public ParameterValue createValue (final StaplerRequest req ,
182
203
final JSONObject jo )
183
204
{
184
205
RestListParameterValue value = req .bindJSON (RestListParameterValue .class , jo );
206
+
185
207
checkValue (value );
186
208
return value ;
187
209
}
@@ -194,7 +216,14 @@ private void checkValue(final RestListParameterValue value) {
194
216
195
217
@ Override
196
218
public boolean isValid (ParameterValue value ) {
197
- return values .contains (((RestListParameterValue ) value ).getValue ());
219
+ if (value == null || value .getValue () == null ) {
220
+ return false ;
221
+ }
222
+
223
+ return values .stream ()
224
+ .map (ValueItem ::getValue )
225
+ .filter (Objects ::nonNull )
226
+ .anyMatch (val -> value .getValue ().equals (val ));
198
227
}
199
228
200
229
@ Override
@@ -333,6 +362,7 @@ public FormValidation doTestConfiguration(@AncestorInPath final Item context,
333
362
@ QueryParameter final String credentialId ,
334
363
@ QueryParameter final MimeType mimeType ,
335
364
@ QueryParameter final String valueExpression ,
365
+ @ QueryParameter final String displayExpression ,
336
366
@ QueryParameter final String filter ,
337
367
@ QueryParameter final ValueOrder valueOrder )
338
368
{
@@ -357,24 +387,25 @@ public FormValidation doTestConfiguration(@AncestorInPath final Item context,
357
387
return FormValidation .error (Messages .RLP_DescriptorImpl_ValidationErr_ExpressionEmpty ());
358
388
}
359
389
360
- ResultContainer <List <String >> container = RestValueService .get (
390
+ ResultContainer <List <ValueItem >> container = RestValueService .get (
361
391
restEndpoint ,
362
392
credentials .orElse (null ),
363
393
mimeType ,
364
394
0 ,
365
395
valueExpression ,
396
+ displayExpression ,
366
397
filter ,
367
398
valueOrder );
368
399
369
400
Optional <String > errorMsg = container .getErrorMsg ();
370
- List <String > values = container .getValue ();
401
+ List <ValueItem > values = container .getValue ();
371
402
if (errorMsg .isPresent ()) {
372
403
return FormValidation .error (errorMsg .get ());
373
404
}
374
405
375
406
// values should NEVER be empty here
376
407
// due to all the filtering and error handling done in the RestValueService
377
- return FormValidation .ok (Messages .RLP_DescriptorImpl_ValidationOk_ConfigValid (values .size (), values .get (0 )));
408
+ return FormValidation .ok (Messages .RLP_DescriptorImpl_ValidationOk_ConfigValid (values .size (), values .get (0 ). getDisplayValue () ));
378
409
}
379
410
}
380
411
}
0 commit comments