-
Notifications
You must be signed in to change notification settings - Fork 3
/
clean-pull-request-build-queue.groovy
52 lines (46 loc) · 1.43 KB
/
clean-pull-request-build-queue.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
// Look over the current build queue of pull request jobs
// and kill any duplicate builds leaving the latest intact.
// Two jobs are defined as duplicate if they are for the
// same OS and pull request number.
import hudson.model.*
import jenkins.model.Jenkins
PULL_REQUEST_JOB_NAME_PREFIX = 'pull_requests-'
def queue = Jenkins.instance.queue
def pr_queue = queue.items.findAll { it.task.name.startsWith(PULL_REQUEST_JOB_NAME_PREFIX) }
// map job names to queued items
def pr_job_map = [:]
pr_queue.each {
jobs = pr_job_map.get(it.task.name, [])
jobs << it
}
pr_job_map.each { name_jobs ->
println name_jobs.getKey()
jobs = name_jobs.getValue()
pr_task_map = [:]
jobs.each { job ->
def params_list = job.params.split()
pr_number = params_list[4]
if (!pr_number.startsWith("PR=") ) {
throw new RuntimeException('Unable to find PR= in job params list')
}
pr_tasks = pr_task_map.get(pr_number, [])
pr_tasks << job
}
def jobs_killed = false
pr_task_map.each { pr_task ->
tasks = pr_task.getValue()
if (tasks.size() > 1 ) {
tasks.sort { it.id }
tasks[0..-2].each { task ->
println " Killing old queued job for " + pr_task.getKey() + " with ID=" + task.id
queue.cancel(task)
jobs_killed = true
}
}
}
if (!jobs_killed) {
println " No duplicate PR jobs found"
}
}
// Avoids Jenkins printing all of the defined variables in the Result: section
return 0