This repository has been archived by the owner on Oct 26, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathverify_pull_request.go
73 lines (62 loc) · 2.05 KB
/
verify_pull_request.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
package main
import (
"context"
"log"
"strings"
"github.com/google/go-github/github"
)
// StatusGetter fetches the status of a specific commit
type StatusGetter interface {
GetCombinedStatus(context.Context, string, string, string, *github.ListOptions) (*github.CombinedStatus, *github.Response, error)
}
// IssueGetter queries github for a specific issue
type IssueGetter interface {
Get(context.Context, string, string, int) (*github.Issue, *github.Response, error)
}
// verifyPullRequest filters out non-mergeable pull requests
func verifyPullRequest(issueClient IssueGetter, statusClient StatusGetter, mergeLabel string, input <-chan *github.PullRequest) <-chan *github.PullRequest {
ret := make(chan *github.PullRequest)
go func() {
for pr := range input {
if pr.GetState() != "open" {
log.Printf("%s/%s: pr %d is %s.\n", pr.Base.Repo.Owner.GetLogin(), pr.Base.Repo.GetName(), pr.GetNumber(), pr.GetState())
continue
}
issue, _, err := issueClient.Get(
context.Background(),
pr.Base.Repo.Owner.GetLogin(),
pr.Base.Repo.GetName(),
pr.GetNumber(),
)
if err != nil {
log.Printf("%s/%s: pr %d failed to lookup issue %s.\n", pr.Base.Repo.Owner.GetLogin(), pr.Base.Repo.GetName(), pr.GetNumber(), err.Error())
continue
}
mergeable := false
for _, label := range issue.Labels {
mergeable = mergeable || strings.EqualFold(*label.Name, mergeLabel)
}
if !mergeable || (pr.Mergeable != nil && !*pr.Mergeable) {
log.Printf("%s/%s: pr %d is not mergeable.\n", pr.Base.Repo.Owner.GetLogin(), pr.Base.Repo.GetName(), pr.GetNumber())
continue
}
status, _, err := statusClient.GetCombinedStatus(
context.Background(),
pr.Base.Repo.Owner.GetLogin(),
pr.Base.Repo.GetName(),
*pr.Head.SHA,
&github.ListOptions{},
)
if err != nil {
continue
}
if status.GetState() != "success" {
log.Printf("%s/%s: pr %d status is %s.\n", pr.Base.Repo.Owner.GetLogin(), pr.Base.Repo.GetName(), pr.GetNumber(), status.GetState())
continue
}
ret <- pr
}
close(ret)
}()
return ret
}