/
pull_request.go
140 lines (120 loc) · 3.11 KB
/
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
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
package client
import (
"context"
"github.com/google/go-github/github"
)
func (cli *githubClient) CheckMergeable(ctx context.Context, owner, repo string, number int) (bool, error) {
pr, _, err := cli.client.PullRequests.Get(ctx, owner, repo, number)
if err != nil {
return false, err
}
if pr == nil {
return false, nil
}
return pr.GetMergeable(), nil
}
func (cli *githubClient) ListPullRequestBySHA(ctx context.Context, owner, repo string, sha string) (prs []PullRequest, err error) {
prs = make([]PullRequest, 0)
step := 50
page := 1
for {
githubPRs, _, err := cli.client.PullRequests.List(ctx, owner, repo, &github.PullRequestListOptions{
ListOptions: github.ListOptions{
Page: page,
PerPage: 50,
},
})
if err != nil {
return nil, err
}
// no more pull requests
if len(githubPRs) == 0 {
break
}
page++
for _, githubPR := range githubPRs {
if githubPR.GetHead().GetSHA() != sha {
continue
}
pr := PullRequest{
Number: githubPR.GetNumber(),
State: githubPR.GetState(),
Title: githubPR.GetTitle(),
Body: githubPR.GetBody(),
User: githubPR.GetUser().GetLogin(),
Labels: make([]string, 0),
HTMLURL: githubPR.GetHTMLURL(),
}
for _, l := range githubPR.Labels {
pr.Labels = append(pr.Labels, l.GetName())
}
prs = append(prs, pr)
}
// no more pull requests
if len(githubPRs) < step {
break
}
}
return
}
func (cli *githubClient) ListFilesByPullRequest(ctx context.Context, owner, repo string, number int) (files []string, err error) {
files = make([]string, 0)
step := 200
page := 1
for {
commitFiles, _, err := cli.client.PullRequests.ListFiles(ctx, owner, repo, number, &github.ListOptions{
Page: page,
PerPage: step,
})
if err != nil {
return nil, err
}
// no more files
if len(commitFiles) == 0 {
break
}
page++
for _, commitFile := range commitFiles {
files = append(files, commitFile.GetFilename())
}
// no more files
if len(commitFiles) < step {
break
}
}
return
}
// operations
type RequestReviewsOperation struct {
Owner string
Repo string
Number int
Reviewers []string
}
func (cli *githubClient) doRequestReviewsOperation(ctx context.Context, op *RequestReviewsOperation) error {
_, _, err := cli.client.PullRequests.RequestReviewers(ctx, op.Owner, op.Repo, op.Number, github.ReviewersRequest{
Reviewers: op.Reviewers,
})
return err
}
type RequestReviewsCancelOperation struct {
Owner string
Repo string
Number int
CancelReviewers []string
}
func (cli *githubClient) doRequestReviewsCancelOperation(ctx context.Context, op *RequestReviewsCancelOperation) error {
_, err := cli.client.PullRequests.RemoveReviewers(ctx, op.Owner, op.Repo, op.Number, github.ReviewersRequest{
Reviewers: op.CancelReviewers,
})
return err
}
type MergeOperation struct {
Owner string
Repo string
Number int
}
func (cli *githubClient) doMergeOperation(ctx context.Context, op *MergeOperation) error {
_, _, err := cli.client.PullRequests.Merge(ctx, op.Owner, op.Repo, op.Number, "auto merged by freebot", nil)
return err
}