/
Utils.groovy
447 lines (397 loc) · 18.4 KB
/
Utils.groovy
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
/*
* The MIT License
*
* Copyright (c) 2016, CloudBees, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.jenkinsci.plugins.pipeline.modeldefinition
import com.google.common.base.Predicate
import com.google.common.cache.CacheBuilder
import com.google.common.cache.CacheLoader
import com.google.common.cache.LoadingCache;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings
import hudson.ExtensionList
import hudson.model.Describable
import hudson.model.Descriptor
import org.apache.commons.codec.digest.DigestUtils
import org.jenkinsci.plugins.pipeline.StageStatus
import org.jenkinsci.plugins.pipeline.StageTagsMetadata
import org.jenkinsci.plugins.pipeline.SyntheticStage
import org.jenkinsci.plugins.pipeline.modeldefinition.actions.ExecutionModelAction
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTPipelineDef
import org.jenkinsci.plugins.pipeline.modeldefinition.ast.ModelASTStages
import org.jenkinsci.plugins.pipeline.modeldefinition.model.MethodsToList
import org.jenkinsci.plugins.pipeline.modeldefinition.model.StepsBlock
import org.jenkinsci.plugins.pipeline.modeldefinition.parser.Converter
import org.jenkinsci.plugins.structs.SymbolLookup
import org.jenkinsci.plugins.structs.describable.UninstantiatedDescribable
import org.jenkinsci.plugins.workflow.actions.TagsAction
import org.jenkinsci.plugins.workflow.cps.CpsFlowExecution
import org.jenkinsci.plugins.workflow.cps.CpsScript
import org.jenkinsci.plugins.workflow.cps.CpsThread
import org.jenkinsci.plugins.workflow.graphanalysis.LinearBlockHoppingScanner
import org.jenkinsci.plugins.workflow.cps.nodes.StepEndNode
import org.jenkinsci.plugins.workflow.cps.nodes.StepStartNode
import org.jenkinsci.plugins.workflow.graph.FlowNode
import org.jenkinsci.plugins.workflow.job.WorkflowRun
import org.jenkinsci.plugins.workflow.steps.StepDescriptor
import org.jenkinsci.plugins.workflow.support.steps.StageStep
import javax.annotation.Nullable
import javax.lang.model.SourceVersion
import java.lang.reflect.ParameterizedType
import java.lang.reflect.Type
import java.util.concurrent.TimeUnit
// TODO: Prune like mad once we have step-in-groovy and don't need these static whitelisted wrapper methods.
/**
* Utility methods for use primarily in CPS-transformed code to avoid excessive global whitelisting.
*
* @author Andrew Bayer
*/
@SuppressFBWarnings(value="SE_NO_SERIALVERSIONID")
public class Utils {
/**
* Workaround for not having to whitelist isAssignableFrom, metaClass etc to determine whether a field on
* a class is of a specific type.
*
* @param fieldType The type we're checking
* @param actualClass The class we're inspecting
* @param fieldName The name of the field - could be a singular when the field name is plural, in which case
* we'll get the actual name from actualFieldName(...)
* @return True if the field exists and is of the given type.
*/
public static boolean isFieldA(Class fieldType, Class actualClass, String fieldName) {
def actualFieldName = actualFieldName(actualClass, fieldName)
def realFieldType = actualClass.metaClass.getMetaProperty(actualFieldName)?.type
if (realFieldType == null) {
return false
} else {
return realFieldType == fieldType || fieldType.isAssignableFrom(realFieldType)
}
}
/**
* Gets the actual field name for a possibly-needs-to-be-pluralized name.
*
* @param actualClass The class we're inspecting
* @param fieldName The original field name, which could need to be pluralized.
* @return The real field name, pluralized if necessary, or null if not found.
*/
public static String actualFieldName(Class actualClass, String fieldName) {
if (actualClass.metaClass.getMetaProperty(fieldName) != null) {
return fieldName
} else if (actualClass.metaClass.getMetaProperty("${fieldName}s") != null) {
return "${fieldName}s"
} else {
return null
}
}
/**
* Get the actual field type or contained field type in the case of parameterized types in the inspected class.
*
* @param actualClass The class we're inspecting
* @param fieldName The field name we're looking for, which could get pluralized.
* @return The class of the field in the inspected class, or the class contained in the list or map.
*/
public static Class actualFieldType(Class actualClass, String fieldName) {
def actualFieldName = actualFieldName(actualClass, fieldName)
if (actualFieldName == null) {
return null
} else {
def field = actualClass.getDeclaredFields().find { !it.isSynthetic() && it.name == actualFieldName }
// If the field's a ParameterizedType, we need to check it to see if it's containing a Plumber class.
if (field.getGenericType() instanceof ParameterizedType) {
if (Map.class.isAssignableFrom(field.getType())) {
return (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[1]
} else {
// First class listed in the actual type arguments - we ignore anything past this because eh.
return (Class) ((ParameterizedType) field.getGenericType()).getActualTypeArguments()[0]
}
} else {
return field.getType()
}
}
}
/**
* Finds the parameterized type argument for a {@link MethodsToList} class and returns it.
*
* @param c A class.
* @return The parameterized type argument for the class, if it's a {@link MethodsToList} class. Null otherwise.
*/
public static Class<Describable> getMethodsToListType(Class c) {
Class retClass
c.genericInterfaces.each { Type t ->
if (t instanceof ParameterizedType) {
if (t.getRawType() instanceof Class
&& MethodsToList.class.isAssignableFrom((Class)t.getRawType())
&& t.getActualTypeArguments().first() instanceof Class) {
retClass = (Class)t.actualTypeArguments.first()
}
}
}
return retClass
}
/**
* Simple wrapper for isInstance to avoid whitelisting issues.
*
* @param c The class to check against
* @param o The object to check
* @return True if the object is an instance of the class, false otherwise
*/
public static boolean instanceOfWrapper(Class c, Object o) {
return c.isInstance(o)
}
/**
* Simple wrapper for isAssignableFrom to avoid whitelisting issues.
*
* @param c The class that should be assignable from
* @param o The class to check
* @return True if o can be assigned to c, false otherwise
*/
public static boolean assignableFromWrapper(Class c, Class o) {
return c.isAssignableFrom(o)
}
public static boolean hasScmContext(CpsScript script) {
try {
// Just rely on SCMVar's own context-checking (via CpsScript) rather than brewing our own.
script.getProperty("scm")
return true
} catch (_) {
// If we get an IllegalStateException, "checkout scm" isn't valid, so return false.
return false
}
}
static void attachDeclarativeActions(CpsScript script) {
WorkflowRun r = script.$build()
ModelASTPipelineDef model = Converter.parseFromCpsScript(script)
ModelASTStages stages = model.stages
stages.removeSourceLocation()
if (r.getAction(SyntheticStageGraphListener.GraphListenerAction.class) == null) {
r.addAction(new SyntheticStageGraphListener.GraphListenerAction())
}
if (r.getAction(ExecutionModelAction.class) == null) {
r.addAction(new ExecutionModelAction(stages))
}
}
static Predicate<FlowNode> endNodeForStage(final StepStartNode startNode) {
return new Predicate<FlowNode>() {
@Override
boolean apply(@Nullable FlowNode input) {
return input != null &&
input instanceof StepEndNode &&
input.getStartNode().equals(startNode)
}
}
}
static Predicate<FlowNode> isStageWithOptionalName(final String stageName = null) {
return new Predicate<FlowNode>() {
@Override
boolean apply(@Nullable FlowNode input) {
return input != null &&
input instanceof StepStartNode &&
((StepStartNode) input).descriptor instanceof StageStep.DescriptorImpl &&
(stageName == null || input.displayName?.equals(stageName))
}
}
}
public static String stringToSHA1(String s) {
return DigestUtils.sha1Hex(s)
}
/**
* Prints a log message to the Jenkins log, bypassing the echo step.
* @param s The message to log
*/
public static void logToTaskListener(String s) {
CpsThread thread = CpsThread.current()
CpsFlowExecution execution = thread.execution
execution?.getOwner()?.getListener()?.getLogger()?.println(s)
}
/**
* Returns true if we're currently nested under a stage.
*
* @return true if we're in a stage and false otherwise
*/
static boolean withinAStage() {
CpsThread thread = CpsThread.current()
CpsFlowExecution execution = thread.execution
LinearBlockHoppingScanner scanner = new LinearBlockHoppingScanner();
FlowNode stageNode = execution.currentHeads.find { h ->
scanner.findFirstMatch(h, isStageWithOptionalName())
}
return stageNode != null
}
private static FlowNode findStageFlowNode(String stageName) {
CpsThread thread = CpsThread.current()
CpsFlowExecution execution = thread.execution
LinearBlockHoppingScanner scanner = new LinearBlockHoppingScanner();
return scanner.findFirstMatch(execution.currentHeads, null, isStageWithOptionalName(stageName))
}
private static void markStageWithTag(String stageName, String tagName, String tagValue) {
FlowNode currentNode = findStageFlowNode(stageName)
if (currentNode != null) {
TagsAction tagsAction = currentNode.getAction(TagsAction.class)
if (tagsAction == null) {
tagsAction = new TagsAction()
tagsAction.addTag(tagName, tagValue)
currentNode.addAction(tagsAction)
} else if (tagsAction.getTagValue(tagName) == null) {
tagsAction.addTag(tagName, tagValue)
currentNode.save()
}
}
}
static <T extends StageTagsMetadata> T getTagMetadata(Class<T> c) {
return ExtensionList.lookup(StageTagsMetadata.class).get(c)
}
static StageStatus getStageStatusMetadata() {
return getTagMetadata(StageStatus.class)
}
static SyntheticStage getSyntheticStageMetadata() {
return getTagMetadata(SyntheticStage.class)
}
static void markStageFailedAndContinued(String stageName) {
markStageWithTag(stageName, getStageStatusMetadata().tagName, getStageStatusMetadata().failedAndContinued)
}
static void markStageSkippedForFailure(String stageName) {
markStageWithTag(stageName, getStageStatusMetadata().tagName, getStageStatusMetadata().skippedForFailure)
}
static void markStageSkippedForConditional(String stageName) {
markStageWithTag(stageName, getStageStatusMetadata().tagName, getStageStatusMetadata().skippedForConditional)
}
/**
* Creates and sets the loading for a cache of {@link Describable}s descending from the given descriptor type.
*
* @param type The {@link Descriptor} class whose extensions we want to find.
* @param includeClassNames Optionally include class names as keys. Defaults to false.
* @param excludedSymbols Optional list of symbol names to exclude from the cache.
* @return A {@link LoadingCache} for looking up types from symbols.
*/
static generateTypeCache(Class<? extends Descriptor> type, boolean includeClassNames = false,
List<String> excludedSymbols = [], Closure<Boolean> filter = null) {
return CacheBuilder.newBuilder()
.expireAfterWrite(10, TimeUnit.MINUTES)
.build(new CacheLoader<Object, Map<String, String>>() {
@Override
Map<String, String> load(Object key) throws Exception {
return populateTypeCache(type, includeClassNames, excludedSymbols, filter)
}
})
}
/**
* Actually populates the type cache.
*
* @param type The {@link Descriptor} class whose extensions we want to find.
* @param includeClassNames Optionally include class names as keys. Defaults to false.
* @param excludedSymbols Optional list of symbol names to exclude from the cache.
* @return A map of symbols or class names to class names.
*/
private static Map<String,String> populateTypeCache(Class<? extends Descriptor> type,
boolean includeClassNames = false,
List<String> excludedSymbols = [],
Closure<Boolean> filter = null) {
Map<String,String> knownTypes = [:]
ExtensionList.lookup(type).each { t ->
if (filter == null || filter.call(t)) {
// Have to special-case StepDescriptor since it doesn't actually have symbols!
if (t instanceof StepDescriptor) {
knownTypes.put(t.functionName, t.clazz.getName())
} else {
Set<String> symbolValue = SymbolLookup.getSymbolValue(t)
if (!symbolValue.isEmpty() && !symbolValue.any { excludedSymbols.contains(it) }) {
knownTypes.put(symbolValue.iterator().next(), t.clazz.getName())
}
}
if (includeClassNames) {
// Add the class name mapping even if we also found the symbol, for backwards compatibility reasons.
knownTypes.put(t.clazz.getName(), t.clazz.getName())
}
}
}
return knownTypes
}
/**
* Determines whether a given {@link UninstantiatedDescribable} is of a given type.
*
* @param ud The {@link UninstantiatedDescribable} to check
* @param base The {@link Class}
* @return True if the uninstantiated describable is of the type given
*/
public static boolean isOfType(UninstantiatedDescribable ud, Class<?> base) {
Descriptor d = SymbolLookup.get().findDescriptor(base, ud.symbol)
return d != null
}
/**
* @param c The closure to wrap.
*/
public static StepsBlock createStepsBlock(Closure c) {
// Jumping through weird hoops to get around the ejection for cases of JENKINS-26481.
StepsBlock wrapper = new StepsBlock()
wrapper.setClosure(c)
return wrapper
}
public static boolean validEnvIdentifier(String i) {
if (!SourceVersion.isIdentifier(i)) {
return false
} else if (!i.matches("[a-zA-Z_]+[a-zA-Z0-9_]*")) {
return false
}
return true
}
/**
* Find and create an {@link UninstantiatedDescribable} from a symbol name and arguments.
* The arguments are assumed to be packaged as Groovy does to {@code invokeMethod} et.al.
* And to be either a single argument or named arguments, and not taking a closure.
*
* @param symbol the {@code @Symbol} name
* @param baseClazz the base class the describable is supposed to inherit
* @param _args the arguments packaged as described above
* @return an UninstantiatedDescribable ready to be instantiated or {@code null} if the descriptor could not be found
*/
public static UninstantiatedDescribable getDescribable(String symbol, Class<? extends Describable> baseClazz, Object _args) {
def descriptor = SymbolLookup.get().findDescriptor(baseClazz, symbol)
if (descriptor != null) {
//Lots copied from org.jenkinsci.plugins.workflow.cps.DSL.invokeDescribable
Map<String, ?> args = unPackageArgs(_args)
return new UninstantiatedDescribable(symbol, descriptor.clazz.name, args)
}
return null
}
/**
* Unpacks the arguments for {@link #getDescribable(java.lang.String, java.lang.Class, java.lang.Object)}.
*
* @param _args the arguments
* @return the unpacked version suitable to give to an {@link UninstantiatedDescribable}.
* @see #getDescribable(java.lang.String, java.lang.Class, java.lang.Object)
*/
static Map<String, ?> unPackageArgs(Object _args) {
if(_args instanceof Object[]) {
List a = Arrays.asList((Object[])_args);
if (a.size()==0) {
return Collections.emptyMap()
}
if (a.size()==1 && a.get(0) instanceof Map && !((Map)a.get(0)).containsKey('$class')) {
return (Map) a.get(0)
} else if (a.size() == 1 && !(a.get(0) instanceof Map)) {
return Collections.singletonMap(UninstantiatedDescribable.ANONYMOUS_KEY, a.get(0))
}
throw new IllegalArgumentException("Expected named arguments but got "+a)
} else {
return Collections.singletonMap(UninstantiatedDescribable.ANONYMOUS_KEY, _args)
}
}
}