forked from cloudfoundry-community/github-pr-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
249 lines (206 loc) · 5.97 KB
/
check.go
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package resource
import (
"fmt"
"path/filepath"
"regexp"
"sort"
"strings"
"github.com/shurcooL/githubv4"
)
var trustedTeamMembers map[string]bool = map[string]bool{}
// Check (business logic)
func Check(request CheckRequest, manager Github) (CheckResponse, error) {
var response CheckResponse
// Filter out pull request if it does not have a filtered state
filterStates := []githubv4.PullRequestState{githubv4.PullRequestStateOpen}
if len(request.Source.States) > 0 {
filterStates = request.Source.States
}
pulls, err := manager.ListPullRequests(filterStates)
if err != nil {
return nil, fmt.Errorf("failed to get last commits: %s", err)
}
disableSkipCI := request.Source.DisableCISkip
Loop:
for _, p := range pulls {
// [ci skip]/[skip ci] in Pull request title
if !disableSkipCI && ContainsSkipCI(p.Title) {
continue
}
// [ci skip]/[skip ci] in Commit message
if !disableSkipCI && ContainsSkipCI(p.Tip.Message) {
continue
}
// Filter pull request if the BaseBranch does not match the one specified in source
if request.Source.BaseBranch != "" && p.PullRequestObject.BaseRefName != request.Source.BaseBranch {
continue
}
// Filter out pull request if it does not contain at least one of the desired labels
if len(request.Source.Labels) > 0 {
labelFound := false
LabelLoop:
for _, wantedLabel := range request.Source.Labels {
for _, targetLabel := range p.Labels {
if targetLabel.Name == wantedLabel {
labelFound = true
break LabelLoop
}
}
}
if !labelFound {
continue Loop
}
}
// Filter out forks.
if request.Source.DisableForks && p.IsCrossRepository {
continue
}
// Filter out drafts.
if request.Source.IgnoreDrafts && p.IsDraft {
continue
}
prAuthorTrusted, err := userTrusted(p.Author.Login, request.Source.TrustedUsers, request.Source.TrustedTeams, manager)
if err != nil {
return nil, fmt.Errorf("failed to list users in team %v: %s", request.Source.TrustedTeams, err)
}
prApproved := p.ApprovedReviewCount >= request.Source.RequiredReviewApprovals
if !prAuthorTrusted && !prApproved {
continue
}
// Fetch files once if paths/ignore_paths are specified.
var files []string
if len(request.Source.Paths) > 0 || len(request.Source.IgnorePaths) > 0 {
files, err = manager.ListModifiedFiles(p.Number)
if err != nil {
return nil, fmt.Errorf("failed to list modified files: %s", err)
}
}
// Skip version if no files match the specified paths.
if len(request.Source.Paths) > 0 {
var wanted []string
for _, pattern := range request.Source.Paths {
w, err := FilterPath(files, pattern)
if err != nil {
return nil, fmt.Errorf("path match failed: %s", err)
}
wanted = append(wanted, w...)
}
if len(wanted) == 0 {
continue Loop
}
}
// Skip version if all files are ignored.
if len(request.Source.IgnorePaths) > 0 {
wanted := files
for _, pattern := range request.Source.IgnorePaths {
wanted, err = FilterIgnorePath(wanted, pattern)
if err != nil {
return nil, fmt.Errorf("ignore path match failed: %s", err)
}
}
if len(wanted) == 0 {
continue Loop
}
}
response = append(response, NewVersion(p))
}
// Sort the commits by date
sort.Sort(response)
// If there are no new but an old version = return the old
if len(response) == 0 && request.Version.PR != "" {
response = append(response, request.Version)
}
// If there are new versions and no previous = return just the latest
if len(response) != 0 && request.Version.PR == "" {
response = CheckResponse{response[len(response)-1]}
}
return response, nil
}
func userTrusted(user string, trustedUsers, trustedTeams []string, manager Github) (bool, error) {
for _, u := range trustedUsers {
if user == u {
return true, nil
}
}
if trustedTeamMembers[user] {
return true, nil
}
for _, team := range trustedTeams {
teamMembers, err := manager.ListTeamMembers(team)
if err != nil {
return false, fmt.Errorf("failed to list team members of team %q: %s", trustedTeams, err)
}
for _, u := range teamMembers {
trustedTeamMembers[u] = true
}
if trustedTeamMembers[user] {
return true, nil
}
}
return false, nil
}
// ContainsSkipCI returns true if a string contains [ci skip] or [skip ci].
func ContainsSkipCI(s string) bool {
re := regexp.MustCompile(`(?i)\[(ci skip|skip ci)\]`)
return re.MatchString(s)
}
// FilterIgnorePath ...
func FilterIgnorePath(files []string, pattern string) ([]string, error) {
var out []string
for _, file := range files {
match, err := filepath.Match(pattern, file)
if err != nil {
return nil, err
}
if !match && !IsInsidePath(pattern, file) {
out = append(out, file)
}
}
return out, nil
}
// FilterPath ...
func FilterPath(files []string, pattern string) ([]string, error) {
var out []string
for _, file := range files {
match, err := filepath.Match(pattern, file)
if err != nil {
return nil, err
}
if match || IsInsidePath(pattern, file) {
out = append(out, file)
}
}
return out, nil
}
// IsInsidePath checks whether the child path is inside the parent path.
//
// /foo/bar is inside /foo, but /foobar is not inside /foo.
// /foo is inside /foo, but /foo is not inside /foo/
func IsInsidePath(parent, child string) bool {
if parent == child {
return true
}
// we add a trailing slash so that we only get prefix matches on a
// directory separator
parentWithTrailingSlash := parent
if !strings.HasSuffix(parentWithTrailingSlash, string(filepath.Separator)) {
parentWithTrailingSlash += string(filepath.Separator)
}
return strings.HasPrefix(child, parentWithTrailingSlash)
}
// CheckRequest ...
type CheckRequest struct {
Source Source `json:"source"`
Version Version `json:"version"`
}
// CheckResponse ...
type CheckResponse []Version
func (r CheckResponse) Len() int {
return len(r)
}
func (r CheckResponse) Less(i, j int) bool {
return r[j].CommittedDate.After(r[i].CommittedDate)
}
func (r CheckResponse) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}