forked from cloudbees/jenkins-scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pipeline-approval-scripts.groovy
59 lines (45 loc) · 1.99 KB
/
pipeline-approval-scripts.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
/**
Author: kuisathaverat
Description: list pending approvals, approve scripts/signatures pending on "In-process Script Approval" using the parameters method and signature, and add a signature tho the list.
Parameters: method and signature that you want to approve
NOTE: this is only for advanced users and for weird behaviours that does not have other workaround
**/
import org.jenkinsci.plugins.scriptsecurity.scripts.*
def method = "something"
def signature = "something"
ScriptApproval sa = ScriptApproval.get();
//list pending approvals
for (ScriptApproval.PendingScript pending : sa.getPendingScripts()) {
println "Pending Approved : " + pending.script
}
for (ScriptApproval.PendingSignature pending : sa.getPendingSignatures()) {
println "Pending Approved : " + pending.signature
}
// approve scripts
for (ScriptApproval.PendingScript pending : sa.getPendingScripts()) {
if (pending.script.equals(method)) {
sa.approveScript(pending.getHash());
println "Approved : " + pending.script
}
}
// approve signatures
for (ScriptApproval.PendingSignature pending : sa.getPendingSignatures()) {
if (pending.equals(signature)) {
sa.approveSignature(pending.signature);
println "Approved : " + pending.signature
}
}
//Add a signature to the list
signature = "staticMethod org.codehaus.groovy.runtime.DefaultGroovyMethods getText java.net.URL"
ScriptApproval.PendingSignature s = new ScriptApproval.PendingSignature(signature, false, ApprovalContext.create())
sa.getPendingSignatures().add(s)
//approbe a full script
import org.jenkinsci.plugins.scriptsecurity.scripts.*
import org.jenkinsci.plugins.scriptsecurity.scripts.languages.GroovyLanguage
final ScriptApproval sa = ScriptApproval.get();
String script = '''stage ('test'){
java.net.URL url = new java.net.URL('http://site.com')
url.getText()
}'''
ScriptApproval.PendingScript s = new ScriptApproval.PendingScript(script, GroovyLanguage.get(), ApprovalContext.create())
sa.approveScript(s.getHash())