-
Notifications
You must be signed in to change notification settings - Fork 217
/
Copy pathFindAndAbortJobsByNameAndClass.groovy
99 lines (89 loc) · 4.39 KB
/
FindAndAbortJobsByNameAndClass.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
/**
Author: https://github.com/ranma2913
Warning: This code is unlicensed.
See https://github.com/samrocketman/jenkins-script-console-scripts/blob/master/user-contributed-examples/README.md
*/
import hudson.*
import hudson.model.*
import jenkins.*
import jenkins.model.*
import org.jenkinsci.plugins.workflow.job.*
// jobNameSearchParam should be absolute OR end in a slash. The result must be an instance of
def jobNameSearchParam = "RIPTiDE_SonarQube/SonarQube_PullRequest_Scanner/"
//def jobNameSearchParam = "RIPTiDE_SonarQube/SonarQube_PullRequest_Scanner/dashboard-launcher/Harish_Testing_branch"
def abortJobs = false
def classesToAbort = [
org.jenkinsci.plugins.workflow.job.WorkflowJob
]
def itemsNotProcessed = [:]
def jobsFoundMatchingSearch = 0
def jobsFoundInClassesToAbort = 0
def jobsAborted = 0
println("""
************************************************** START ***************************************************************" +
jobNameSearchParam: '${jobNameSearchParam}'
abortJobs: ${abortJobs}
classesToAbort: ${classesToAbort}
jobsFoundMatchingSearch: ${jobsFoundMatchingSearch}
jobsFoundInClassesToAbort: ${jobsFoundInClassesToAbort}
jobsAborted: ${jobsAborted}
""".trim())
println("""****************************************** Processing Started **********************************************************
Searching through AbstractItems...""")
//JavaDoc: https://javadoc.jenkins-ci.org/hudson/model/AbstractItem.html
Jenkins.instance.getAllItems(AbstractItem.class).each { item ->
if (item.fullName.contains(jobNameSearchParam)) {
jobsFoundMatchingSearch++
if (classesToAbort.contains(item.getClass())) {
jobsFoundInClassesToAbort++
WorkflowJob workflowJob = Jenkins.instance.getItemByFullName(item.fullName)
println("Class: ${item.getClass()}\nfullName: ${item.fullName}")
println("WorkflowJob RunMap.size(${workflowJob._getRuns().size()}):")
//JavaDoc: https://javadoc.jenkins.io/plugin/workflow-job/org/jenkinsci/plugins/workflow/job/WorkflowRun.html
workflowJob._getRuns().each { int runId, WorkflowRun build ->
println("""|\t- runId: ${runId}
|\t BuildStatusSumamry.message: ${build.getBuildStatusSummary().message}
|\t Build.hasAllowKill(): ${build.hasAllowKill()}
|\t Build.hasAllowTerm(): ${build.hasAllowTerm()}
|\t Build.hasntStartedYet(): ${build.hasntStartedYet()}
|\t Build.isBuilding(): ${build.isBuilding()}
|\t Build.isInProgress(): ${build.isInProgress()}
|\t Build.isLogUpdated(): ${build.isLogUpdated()}
|\t build: ${build}""".stripMargin() + '\n')
if (abortJobs && (build.isBuilding() || build.isInProgress())) {
build.finish(hudson.model.Result.ABORTED,
new IOException("""
|Build Aborted by Jenkins Script Console
|User: ${User.current().getId()} (${User.current()})
|Build: ${build}
|Stack Trace:""".stripMargin()
)
)
jobsAborted++
}
}
// Add Newline between each WorkflowJob group.
println("\n")
}
} else {
def itemListForType = itemsNotProcessed.get("${item.getClass()}".toString(), [])
itemListForType.add(item)
itemsNotProcessed.put("${item.getClass()}".toString(), itemListForType)
}
}
println("""
***************************************** Processing Complete **********************************************************" +
jobNameSearchParam: '${jobNameSearchParam}'
abortJobs: ${abortJobs}
classesToAbort: ${classesToAbort}
jobsFoundMatchingSearch: ${jobsFoundMatchingSearch}
jobsFoundInClassesToAbort: ${jobsFoundInClassesToAbort}
jobsAborted: ${jobsAborted}""")
println("\nitemsNotProcessed (wrong type or not matching jobNameSearchParam):")
itemsNotProcessed.each { itemType, itemListForType ->
println("itemType: ${itemType}")
itemListForType.each { item ->
println("\t - ${item}")
}
}
return "************************************************* END ******************************************************************"