-
Notifications
You must be signed in to change notification settings - Fork 217
/
find-all-projects-using-label.groovy
97 lines (88 loc) · 3.55 KB
/
find-all-projects-using-label.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
/*
Copyright (c) 2015-2024 Sam Gleske - https://github.com/samrocketman/jenkins-script-console-scripts
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.
*/
/*
This reveals in Jenkins which projects are using a Jenkins agent with a
particular label. Useful for finding all projects using a particular set of
agents.
*/
import hudson.model.Job
if(!binding.hasVariable('labels')) {
labels = ['language:shell', 'language:ruby']
}
if(!binding.hasVariable('evaluateOr')) {
//by default it searches for any of the labels from the list
//set evaluateAnd to true to require all labels much exist in the job
evaluateOr = true
}
if(!binding.hasVariable('formatJervis')) {
//Are Jenkins jobs organized when generated by Jervis?
//https://github.com/samrocketman/jervis
formatJervis = false
}
if(labels in String) {
labels = (labels.contains(',')) ? labels.split(',') : [labels]
}
if(evaluateOr in String) {
evaluateOr = (evaluateOr != 'false')
}
if(formatJervis in String) {
formatJervis = (formatJervis != 'false')
}
//type check user defined parameters/bindings
if(!(labels in List) || (false in labels.collect { it in String } )) {
throw new Exception('PARAMETER ERROR: labels must be a list of strings.')
}
if(!(evaluateOr in Boolean)) {
throw new Exception('PARAMETER ERROR: evaluateOr must be a boolean.')
}
if(!(formatJervis in Boolean)) {
throw new Exception('PARAMETER ERROR: formatJervis must be a boolean.')
}
projects = [] as Set
//getAllItems searches a global lookup table of items regardless of folder structure
Jenkins.instance.getAllItems(Job.class).each { Job job ->
Boolean labelFound = false
String jobLabelString
if(job.class.simpleName == 'FreeStyleProject') {
jobLabelString = job.getAssignedLabelString()
} else if(job.class.simpleName == 'WorkflowJob') {
jobLabelString = job.getDefinition().getScript()
} else {
throw new Exception("Don't know how to handle class: ${job.getClass()}")
}
List results = labels.collect { label ->
jobLabelString.contains(label)
}
if(evaluateOr) {
//evaluate if any of the labels exist in job
labelFound = true in results
} else {
//evaluate requiring all labels to exist in job
labelFound = !(false in results)
}
if(labelFound) {
if(formatJervis) {
projects << "${job.fullName.split('/')[0]}/${job.displayName.split(' ')[0]}"
} else {
projects << job.fullName
}
}
}
println(projects.join('\n'))
//null so no result shows up
null