forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
pr.go
342 lines (309 loc) · 10.5 KB
/
pr.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2017 Drone.IO Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package github
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"github.com/jenkins-x/go-scm/scm"
"github.com/jenkins-x/go-scm/scm/driver/internal/null"
errors2 "k8s.io/apimachinery/pkg/util/errors"
)
var (
teamRe = regexp.MustCompile(`^(.*)/(.*)$`)
)
type pullService struct {
*issueService
}
func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d", repo, number)
out := new(pr)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertPullRequest(out), res, err
}
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls?%s", repo, encodePullRequestListOptions(opts))
out := []*pr{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertPullRequestList(out), res, err
}
func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/files?%s", repo, number, encodeListOptions(opts))
out := []*file{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertChangeList(out), res, err
}
func (s *pullService) Merge(ctx context.Context, repo string, number int, options *scm.PullRequestMergeOptions) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/merge", repo, number)
res, err := s.client.do(ctx, "PUT", path, encodePullRequestMergeOptions(options), nil)
return res, err
}
func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d", repo, number)
data := map[string]string{"state": "closed"}
res, err := s.client.do(ctx, "PATCH", path, &data, nil)
return res, err
}
func (s *pullService) Reopen(ctx context.Context, repo string, number int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d", repo, number)
data := map[string]string{"state": "open"}
res, err := s.client.do(ctx, "PATCH", path, &data, nil)
return res, err
}
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls", repo)
in := &prInput{
Title: input.Title,
Head: input.Head,
Base: input.Base,
Body: input.Body,
}
out := new(pr)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertPullRequest(out), res, err
}
func (s *pullService) Update(ctx context.Context, repo string, number int, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d", repo, number)
in := &prInput{}
if input.Title != "" {
in.Title = input.Title
}
if input.Body != "" {
in.Body = input.Body
}
if input.Head != "" {
in.Head = input.Head
}
if input.Base != "" {
in.Base = input.Base
}
out := new(pr)
res, err := s.client.do(ctx, "PATCH", path, in, out)
return convertPullRequest(out), res, err
}
func (s *pullService) RequestReview(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
_, resp, err := s.tryRequestReview(ctx, repo, number, logins)
// At least one invalid user. Try adding them individually.
if err != nil && resp != nil && resp.Status == http.StatusUnprocessableEntity {
missing := scm.MissingUsers{
Action: "request a PR review from",
}
for _, user := range logins {
_, resp, err = s.tryRequestReview(ctx, repo, number, []string{user})
if err != nil && resp != nil && resp.Status == http.StatusUnprocessableEntity {
missing.Users = append(missing.Users, user)
} else if err != nil {
return nil, fmt.Errorf("failed to add reviewer to PR. errmsg: %v", err)
}
}
if len(missing.Users) > 0 {
return nil, missing
}
}
return resp, err
}
func (s *pullService) UnrequestReview(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
body, err := prepareReviewersBody(logins, strings.Split(repo, "/")[0])
if err != nil {
return nil, err
}
if len(body.TeamReviewers) == 0 && len(body.Reviewers) == 0 {
return nil, nil
}
path := fmt.Sprintf("repos/%s/pulls/%d/requested_reviewers", repo, number)
out := new(pr)
res, err := s.client.do(ctx, "DELETE", path, body, out)
if err != nil {
return res, err
}
extras := scm.ExtraUsers{Action: "remove the PR review request for"}
for _, user := range out.RequestedReviewers {
found := false
for _, toDelete := range logins {
if NormLogin(user.Login) == NormLogin(toDelete) {
found = true
break
}
}
if found {
extras.Users = append(extras.Users, user.Login)
}
}
if len(extras.Users) > 0 {
return res, extras
}
return res, nil
}
func prepareReviewersBody(logins []string, org string) (prReviewers, error) {
body := prReviewers{}
var errors []error
for _, login := range logins {
mat := teamRe.FindStringSubmatch(login)
if mat == nil {
body.Reviewers = append(body.Reviewers, login)
} else if mat[1] == org {
body.TeamReviewers = append(body.TeamReviewers, mat[2])
} else {
errors = append(errors, fmt.Errorf("team %s is not part of %s org", login, org))
}
}
return body, errors2.NewAggregate(errors)
}
func (s *pullService) tryRequestReview(ctx context.Context, orgAndRepo string, number int, logins []string) (*scm.PullRequest, *scm.Response, error) {
body, err := prepareReviewersBody(logins, strings.Split(orgAndRepo, "/")[0])
if err != nil {
// At least one team not in org,
// let RequestReview handle retries and alerting for each login.
return nil, nil, err
}
out := new(pr)
path := fmt.Sprintf("repos/%s/pulls/%d/requested_reviewers", orgAndRepo, number)
res, err := s.client.do(ctx, "POST", path, body, out)
return convertPullRequest(out), res, err
}
type prReviewers struct {
Reviewers []string `json:"reviewers,omitempty"`
TeamReviewers []string `json:"team_reviewers,omitempty"`
}
type pullRequestMergeRequest struct {
CommitMessage string `json:"commit_message,omitempty"`
CommitTitle string `json:"commit_title,omitempty"`
MergeMethod string `json:"merge_method,omitempty"`
SHA string `json:"sha,omitempty"`
}
type prBranch struct {
Ref string `json:"ref"`
Sha string `json:"sha"`
User user `json:"user"`
Repo repository `json:"repo"`
}
type milestone struct {
Number int `json:"number"`
ID int `json:"id"`
Title string `json:"title"`
Description string `json:"Description"`
Link string `json:"html_url"`
State string `json:"state"`
}
type pr struct {
Number int `json:"number"`
State string `json:"state"`
Title string `json:"title"`
Body string `json:"body"`
Labels []*label `json:"labels"`
DiffURL string `json:"diff_url"`
HTMLURL string `json:"html_url"`
User user `json:"user"`
RequestedReviewers []user `json:"requested_reviewers"`
Assignees []user `json:"assignees"`
Head prBranch `json:"head"`
Base prBranch `json:"base"`
Draft bool `json:"draft"`
Merged bool `json:"merged"`
Mergeable bool `json:"mergeable"`
MergeableState string `json:"mergeable_state"`
Rebaseable bool `json:"rebaseable"`
MergeSha string `json:"merge_commit_sha"`
Milestone milestone `json:"milestone"`
MergedAt null.String `json:"merged_at"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type file struct {
Sha string `json:"sha"`
Filename string `json:"filename"`
Status string `json:"status"`
Additions int `json:"additions"`
Deletions int `json:"deletions"`
Changes int `json:"changes"`
Patch string `json:"patch"`
BlobURL string `json:"blob_url"`
PreviousFilename string `json:"previous_filename"`
}
type prInput struct {
Title string `json:"title,omitempty"`
Body string `json:"body,omitempty"`
Head string `json:"head,omitempty"`
Base string `json:"base,omitempty"`
}
func convertPullRequestList(from []*pr) []*scm.PullRequest {
to := []*scm.PullRequest{}
for _, v := range from {
to = append(to, convertPullRequest(v))
}
return to
}
func convertPullRequest(from *pr) *scm.PullRequest {
return &scm.PullRequest{
Number: from.Number,
Title: from.Title,
Body: from.Body,
Labels: convertLabelObjects(from.Labels),
Sha: from.Head.Sha,
Ref: fmt.Sprintf("refs/pull/%d/head", from.Number),
State: from.State,
Source: from.Head.Ref,
Target: from.Base.Ref,
Fork: from.Head.Repo.FullName,
Base: *convertPullRequestBranch(&from.Base),
Head: *convertPullRequestBranch(&from.Head),
DiffLink: from.DiffURL,
Link: from.HTMLURL,
Closed: from.State != "open",
Draft: from.Draft,
MergeSha: from.MergeSha,
Merged: from.Merged,
Mergeable: from.Mergeable,
MergeableState: scm.ToMergeableState(from.MergeableState),
Rebaseable: from.Rebaseable,
Author: *convertUser(&from.User),
Assignees: convertUsers(from.Assignees),
Reviewers: convertUsers(from.RequestedReviewers),
Created: from.CreatedAt,
Updated: from.UpdatedAt,
}
}
func convertPullRequestBranch(src *prBranch) *scm.PullRequestBranch {
return &scm.PullRequestBranch{
Ref: src.Ref,
Sha: src.Sha,
Repo: *convertRepository(&src.Repo),
}
}
func convertUsers(users []user) []scm.User {
answer := []scm.User{}
for _, u := range users {
user := convertUser(&u)
if user.Login != "" {
answer = append(answer, *user)
}
}
if len(answer) == 0 {
return nil
}
return answer
}
func convertChangeList(from []*file) []*scm.Change {
to := []*scm.Change{}
for _, v := range from {
to = append(to, convertChange(v))
}
return to
}
func convertChange(from *file) *scm.Change {
return &scm.Change{
Path: from.Filename,
Added: from.Status == "added",
Deleted: from.Status == "deleted",
Renamed: from.Status == "moved",
Additions: from.Additions,
Deletions: from.Deletions,
Changes: from.Changes,
BlobURL: from.BlobURL,
Sha: from.Sha,
}
}