This repository has been archived by the owner on Nov 12, 2021. It is now read-only.
forked from digitalocean/github-pr-resource
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check.go
190 lines (155 loc) · 4.83 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
package resource
import (
"fmt"
"log"
"sort"
"time"
"github.com/telia-oss/github-pr-resource/pullrequest"
)
func findPulls(since time.Time, gh Github) ([]pullrequest.PullRequest, error) {
if since.IsZero() {
since = time.Now().AddDate(-3, 0, 0)
}
return gh.ListOpenPullRequests(since)
}
// Check (business logic)
func Check(request CheckRequest, manager Github) (CheckResponse, error) {
var response CheckResponse
pulls, err := findPulls(request.Version.UpdatedDate, manager)
if err != nil {
return nil, fmt.Errorf("failed to get last commits: %s", err)
}
paths := request.Source.Paths
iPaths := request.Source.IgnorePaths
log.Println("total pulls found:", len(pulls))
for _, p := range pulls {
if !pullrequest.FromReleaseBranch()(p) {
log.Println("Branch of PR is not a release branch, skipping...")
continue
}
log.Printf("evaluate pull: %+v\n", p)
if !newVersion(request, p) {
log.Println("no new version found")
continue
}
if len(paths)+len(iPaths) > 0 {
log.Println("pattern/s configured")
p.Files, err = compareVersionsChangedFiles(p, request, manager)
if err != nil {
log.Println("couldn't get version: ")
return nil, err
}
log.Println("paths configured:", paths)
log.Println("ignore paths configured:", iPaths)
log.Println("changed files found:", p.Files)
switch {
// if `paths` is configured && NONE of the changed files match `paths` pattern/s
case pullrequest.Patterns(paths)(p) && !pullrequest.Files(paths, false)(p):
log.Println("paths excluded pull")
continue
// if `ignore_paths` is configured && ALL of the changed files match `ignore_paths` pattern/s
case pullrequest.Patterns(iPaths)(p) && pullrequest.Files(iPaths, true)(p):
log.Println("ignore paths excluded pull")
continue
}
}
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 != 0 {
log.Println("no new versions, use old")
response = append(response, request.Version)
}
// If there are new versions and no previous = return just the latest
if len(response) != 0 && request.Version.PR == 0 {
response = CheckResponse{response[len(response)-1]}
}
log.Println("version count in response:", len(response))
log.Println("versions:", response)
return response, nil
}
func newVersion(r CheckRequest, p pullrequest.PullRequest) bool {
switch {
// negative filters
case pullrequest.SkipCI(r.Source.DisableCISkip)(p),
pullrequest.BaseBranch(r.Source.BaseBranch)(p),
pullrequest.ApprovedReviewCount(r.Source.RequiredReviewApprovals)(p),
pullrequest.Labels(r.Source.Labels)(p),
pullrequest.Fork(r.Source.DisableForks)(p):
return false
// positive filters
case pullrequest.Created(r.Version.UpdatedDate)(p),
pullrequest.BaseRefChanged()(p),
pullrequest.BaseRefForcePushed()(p),
pullrequest.HeadRefForcePushed()(p),
pullrequest.Reopened()(p),
pullrequest.BuildCI()(p),
pullrequest.NewCommits(r.Version.UpdatedDate)(p):
return true
}
return false
}
func pullRequestFiles(n int, manager Github) ([]string, error) {
files, err := manager.GetChangedFiles(n)
if err != nil {
return nil, fmt.Errorf("failed to list modified files: %s", err)
}
return files, nil
}
func commitChangedFiles(sha string, manager Github) ([]string, error) {
files, err := manager.GetCommitChangedFiles(sha)
if err != nil {
return nil, fmt.Errorf("failed to list modified files: %s", err)
}
return files, nil
}
// If there are new versions and no previous
// Get all changed files associated with the PR
// Else get the changed files on a PRs latest commit
func compareVersionsChangedFiles(pr pullrequest.PullRequest, requestPR CheckRequest, manager Github) ([]string, error) {
files := make([]string, 0)
var err error
if (requestPR.Version.PR == 0) || (pr.Number != requestPR.Version.PR) {
files, err = pullRequestFiles(pr.Number, manager)
if err != nil {
return nil, err
}
} else {
pr, err := manager.GetPullRequest(pr.Number, pr.HeadRef.OID)
if err != nil {
log.Println("couldn't get pr...")
return nil, err
}
for commitIndex, commit := range pr.Commits {
if commit.OID == requestPR.Version.Commit {
for i := commitIndex + 1; i < len(pr.Commits); i++ {
f, err := commitChangedFiles(pr.Commits[i].OID, manager)
if err != nil {
return nil, err
}
files = append(files, f...)
}
break
}
}
}
return files, err
}
// 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].UpdatedDate.After(r[i].UpdatedDate)
}
func (r CheckResponse) Swap(i, j int) {
r[i], r[j] = r[j], r[i]
}