forked from samrocketman/jervis
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgetBuildContextMap.groovy
138 lines (120 loc) · 3.97 KB
/
getBuildContextMap.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
Copyright 2014-2023 Sam Gleske - https://github.com/samrocketman/jervis
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/**
Get a build environment context map meant to be used with build trigger
filtering by context. This is used by the FilterByContext class typically.
*/
import hudson.model.Cause
import hudson.model.Job
import hudson.model.Run
import hudson.triggers.TimerTrigger
import jenkins.plugins.git.GitTagSCMHead
import jenkins.scm.api.SCMHead
import org.jenkinsci.plugins.github_branch_source.PullRequestSCMHead
import org.jenkinsci.plugins.workflow.multibranch.BranchJobProperty
import org.jenkinsci.plugins.github_branch_source.BranchSCMHead
import org.jenkinsci.plugins.pipeline.github.trigger.IssueCommentCause
@NonCPS
String getPullRequestComment(Run build) {
def cause = build.causes.find { it in IssueCommentCause }
// username of commenter is stored in cause?.userLogin
cause?.comment ?: ''
}
@NonCPS
Boolean getBranchName(Job build_parent) {
SCMHead head = build_parent?.getProperty(BranchJobProperty)?.branch?.head
if(!(head instanceof BranchSCMHead)) {
return ''
}
head?.name ?: ''
}
@NonCPS
String getTagName(Job build_parent) {
SCMHead head = build_parent?.getProperty(BranchJobProperty)?.branch?.head
if(!(head instanceof GitTagSCMHead)) {
return ''
}
head?.name ?: ''
}
@NonCPS
Boolean isPullRequest(Job build_parent) {
SCMHead head = build_parent?.getProperty(BranchJobProperty)?.branch?.head
(head instanceof PullRequestSCMHead) ?: false
}
@NonCPS
Boolean isTimerBuild(Run build) {
(build?.causes?.find {
it instanceof TimerTrigger.TimerTriggerCause
} as Boolean) ?: false
}
@NonCPS
String getUserCause(Run build) {
build.causes.find { Cause c ->
c instanceof Cause.UserIdCause
}?.userId ?: ''
}
/**
Return a HashMap which includes the full cause and context of the build
environment in Jenkins from which to act on with conditional logic.
*/
@NonCPS
Map call() {
Map context = [
trigger: '',
context: '',
metadata: [
pr: false,
branch: '',
tag: '',
push: false,
cron: false,
manually: '',
pr_comment: ''
]
]
// Determine the root cause (build trigger)
context.metadata.manually = getUserCause(currentBuild.rawBuild)
if(context.metadata.manually) {
context.trigger = 'manually'
}
context.metadata.cron = isTimerBuild(currentBuild.rawBuild)
if(context.metadata.cron) {
context.trigger = 'cron'
}
context.metadata.pr_comment = getPullRequestComment(currentBuild.rawBuild)
if(context.metadata.pr_comment) {
context.trigger = 'pr_comment'
}
// auto could mean a build was pushed or a pull request was opened. It
// just means it didn't match other known trigger types.
if(!context.trigger) {
context.trigger = 'push'
context.metadata.push = true
} else {
context.metadata.push = false
}
// Determine the context
context.metadata.pr = isPullRequest(currentBuild.rawBuild.parent)
if(context.metadata.pr) {
context.context = 'pr'
}
context.metadata.tag = getTagName(currentBuild.rawBuild.parent)
if(context.metadata.tag) {
context.context = 'tag'
}
context.metadata.branch = getBranchName(currentBuild.rawBuild.parent)
if(context.metadata.branch) {
context.context = 'branch'
}
return context
}