forked from samrocketman/jenkins-script-console-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert-ghprb-between-polling-webhooks.groovy
70 lines (62 loc) · 3.06 KB
/
convert-ghprb-between-polling-webhooks.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
/*
Copyright (c) 2015-2018 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 iterates through all Jenkins jobs and finds only jobs that have
GHPRB triggers configured. In each GHPRB configured job this script does
the following:
- Disables cron polling. (or enables it)
- Enables managing webhooks for the repository. (or disables it)
- Saves the job configuration.
- Starts the trigger so webhooks are active in Jenkins and registered with
the GitHub project via GitHub API.
*/
import hudson.model.Job
import hudson.triggers.Trigger
import java.lang.reflect.Field
import org.jenkinsci.plugins.ghprb.GhprbTrigger
//true - convert from polling to webhooks
//false - convert from webhooks to polling
pollToWebhooks = true
cron_field = 'H/15 * * * *'
dryRun = true
if(!('ghprb' in Jenkins.instance.pluginManager.plugins*.shortName)) {
throw new Exception('GitHub Pull-Request Builder Plugin is not installed. This script has aborted.')
}
Jenkins.instance.getAllItems(Job.class).findAll { Job j ->
j.getTrigger(GhprbTrigger.class)
}.each { Job j ->
if(!dryRun) {
//for this job which has GHPRB enabled do the following:
Trigger trigger = j.getTrigger(GhprbTrigger.class)
//disable cron polling
Field cron_field = trigger.getClass().getDeclaredField('cron')
cron_field.setAccessible(true)
cron_field.set(trigger, (pollToWebhooks)? '' : cron_field)
//enable managing webhooks for the repository
Field useGitHubHooks_field = trigger.getClass().getDeclaredField('useGitHubHooks')
useGitHubHooks_field.setAccessible(true)
useGitHubHooks_field.set(trigger, pollToWebhooks)
//save the job configuration
j.save()
//start the trigger so webhooks are active in Jenkins and registered with the GitHub project via GitHub API
trigger.start(j, false)
}
println "${(dryRun) ? 'DRYRUN: ' : ''}${j.name} converted."
}
//discard script console result output
null