forked from samrocketman/jenkins-script-console-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
find-all-jobs-cloning-github-repository.groovy
103 lines (91 loc) · 3.87 KB
/
find-all-jobs-cloning-github-repository.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
/*
Copyright (c) 2015-2022 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 script will search a Jenkins instance for all freestyle jobs, pipeline
jobs, or multibranch pipeline jobs which have the repository configured to
be cloned. This script is useful for discovering jobs which might affect a
given repository.
*/
// enter the repository to search
String repository = 'namespace/example-repository'
// core classes
import hudson.model.Job
import hudson.model.Item
import hudson.scm.NullSCM
import jenkins.model.Jenkins
import java.util.regex.Pattern
import hudson.model.FreeStyleProject
/*
The following commented out classes are here because we use them, but they
are provided by a plugin which may not be installed in a Jenkins instance.
They're discovered without importing them.
*/
//import org.jenkinsci.plugins.multiplescms.MultiSCM
//import org.jenkinsci.plugins.workflow.job.WorkflowJob
//import org.jenkinsci.plugins.workflow.multibranch.WorkflowMultiBranchProject
Pattern pattern = Pattern.compile(".*\\Q${repository}\\E(\\.git)?\$")
List<String> getMatchingMultibranchPipelineUrls(Pattern p) {
Jenkins.instance.getAllItems(Item).findAll { Item j ->
j.class.simpleName == 'WorkflowMultiBranchProject'
}.findAll { Item j ->
j.getSCMSources().find {
it.class.simpleName == 'GitHubSCMSource'
}?.with {
p.matcher("${it.repoOwner}/${it.repository}").matches()
}
}.collect { Item j ->
"${Jenkins.instance.rootUrl}${j.url}"
}
}
List<String> getMatchingStandalonePipelineUrls(Pattern p) {
Jenkins.instance.getAllItems(Job).findAll { Job j ->
j.class.simpleName == 'WorkflowJob' &&
j.parent.class.simpleName != 'WorkflowMultiBranchProject'
}.findAll { Job j ->
j.definition?.scm?.userRemoteConfigs*.url.any { String s ->
p.matcher(s).matches()
}
}.collect { Job j ->
"${Jenkins.instance.rootUrl}${j.url}"
}
}
List<String> getMatchingFreeStyleJobUrls(Pattern p) {
Jenkins.instance.getAllItems(FreeStyleProject).findAll { Job j ->
if(j.scm in NullSCM) {
return false
}
j.scm.with { scm ->
if(scm.class.simpleName == 'MultiSCM') {
return scm.configuredSCMs*.userRemoteConfigs.flatten()
}
j.scm?.userRemoteConfigs ?: []
}*.url.any { String s ->
p.matcher(s).matches()
}
}.collect { Job j ->
"${Jenkins.instance.rootUrl}${j.url}"
}
}
List<String> findAllMatchingJobUrls(Pattern p) {
[
getMatchingFreeStyleJobUrls(p),
getMatchingMultibranchPipelineUrls(p),
getMatchingStandalonePipelineUrls(p)
].flatten().sort()
}
'\n* ' + (findAllMatchingJobUrls(pattern).join('\n* ') ?: 'No jobs found')