-
Notifications
You must be signed in to change notification settings - Fork 233
/
pr.go
413 lines (389 loc) · 13.2 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
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
// 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 gitee
import (
"context"
"fmt"
"time"
"github.com/drone/go-scm/scm"
)
type pullService struct {
client *wrapper
}
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) FindComment(ctx context.Context, repo string, _ int, id int) (*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/comments/%d", repo, id)
out := new(prComment)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertPullRequestComment(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, _ scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/files", repo, number)
out := []*prFile{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertPrChangeList(out), res, err
}
func (s *pullService) ListComments(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/comments/?%s", repo, number, encodeListOptions(opts))
out := []*prComment{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertPullRequestComments(out), res, err
}
func (s *pullService) ListCommits(ctx context.Context, repo string, number int, _ scm.ListOptions) ([]*scm.Commit, *scm.Response, error) {
path := fmt.Sprintf("/repos/%s/pulls/%d/commits", repo, number)
out := []*prCommit{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertPrCommitList(out), res, err
}
func (s *pullService) Merge(ctx context.Context, repo string, number int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/merge", repo, number)
res, err := s.client.do(ctx, "PUT", path, nil, 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) 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,
Body: input.Body,
Head: input.Source,
Base: input.Target,
}
out := new(pr)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertPullRequest(out), res, err
}
func (s *pullService) CreateComment(ctx context.Context, repo string, number int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/%d/comments", repo, number)
in := &prCommentInput{
Body: input.Body,
}
out := new(prComment)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertPullRequestComment(out), res, err
}
func (s *pullService) DeleteComment(ctx context.Context, repo string, _ int, id int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/pulls/comments/%d", repo, id)
res, err := s.client.do(ctx, "DELETE", path, nil, nil)
return res, err
}
type (
pr struct {
ID int `json:"id"`
URL string `json:"url"`
HtmlURL string `json:"html_url"`
DiffURL string `json:"diff_url"`
PatchURL string `json:"patch_url"`
IssueURL string `json:"issue_url"`
CommitsURL string `json:"commits_url"`
ReviewCommentsURL string `json:"review_comments_url"`
ReviewCommentURL string `json:"review_comment_url"`
CommentsURL string `json:"comments_url"`
Number int `json:"number"`
State string `json:"state"`
Title string `json:"title"`
Body string `json:"body"`
AssigneesNumber int `json:"assignees_number"`
TestersNumber int `json:"testers_number"`
Assignees []assignee `json:"assignees"`
Testers []tester `json:"testers"`
Milestone milestone `json:"milestone"`
Labels []label `json:"labels"`
Locked bool `json:"locked"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
ClosedAt time.Time `json:"closed_at"`
MergedAt time.Time `json:"merged_at"`
Mergeable bool `json:"mergeable"`
CanMergeCheck bool `json:"can_merge_check"`
Head headOrBase `json:"head"`
Base headOrBase `json:"base"`
User user `json:"user"`
}
assignee struct {
ID int `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
URL string `json:"url"`
HtmlURL string `json:"html_url"`
Remark string `json:"remark"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
}
tester struct {
ID int `json:"id"`
Login string `json:"login"`
Name string `json:"name"`
AvatarURL string `json:"avatar_url"`
URL string `json:"url"`
HtmlURL string `json:"html_url"`
Remark string `json:"remark"`
FollowersURL string `json:"followers_url"`
FollowingURL string `json:"following_url"`
GistsURL string `json:"gists_url"`
StarredURL string `json:"starred_url"`
SubscriptionsURL string `json:"subscriptions_url"`
OrganizationsURL string `json:"organizations_url"`
ReposURL string `json:"repos_url"`
EventsURL string `json:"events_url"`
ReceivedEventsURL string `json:"received_events_url"`
Type string `json:"type"`
}
milestone struct {
URL string `json:"url"`
HtmlURL string `json:"html_url"`
ID int `json:"id"`
Number int `json:"number"`
RepositoryID interface{} `json:"repository_id"`
State string `json:"state"`
Title string `json:"title"`
Description string `json:"description"`
UpdatedAt time.Time `json:"updated_at"`
CreatedAt time.Time `json:"created_at"`
OpenIssues int `json:"open_issues"`
ClosedIssues int `json:"closed_issues"`
DueOn string `json:"due_on"`
}
label struct {
ID int `json:"id"`
Name string `json:"name"`
Color string `json:"color"`
RepositoryID int `json:"repository_id"`
URL string `json:"url"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
prRepo struct {
ID int `json:"id"`
FullName string `json:"full_name"`
HumanName string `json:"human_name"`
URL string `json:"url"`
//Namespace namespace `json:"namespace"`
Path string `json:"path"`
Name string `json:"name"`
Owner user `json:"owner"`
Assigner assignee `json:"assigner"`
Description string `json:"description"`
Private bool `json:"private"`
Public bool `json:"public"`
Internal bool `json:"internal"`
Fork bool `json:"fork"`
HtmlURL string `json:"html_url"`
SshURL string `json:"ssh_url"`
}
headOrBase struct {
Label string `json:"label"`
Ref string `json:"ref"`
Sha string `json:"sha"`
User user `json:"user"`
Repo prRepo `json:"repo"`
}
prComment struct {
URL string `json:"url"`
ID int `json:"id"`
User user `json:"user"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
Body string `json:"body"`
HtmlURL string `json:"html_url"`
PullRequestURL string `json:"pull_request_url"`
}
prFile struct {
Sha string `json:"sha"`
Filename string `json:"filename"`
Status string `json:"status"`
Additions string `json:"additions"`
Deletions string `json:"deletions"`
BlobURL string `json:"blob_url"`
RawURL string `json:"raw_url"`
Patch struct {
Diff string `json:"diff"`
NewPath string `json:"new_path"`
OldPath string `json:"old_path"`
AMode string `json:"a_mode"`
BMode string `json:"b_mode"`
NewFile bool `json:"new_file"`
RenamedFile bool `json:"renamed_file"`
DeletedFile bool `json:"deleted_file"`
TooLarge bool `json:"too_large"`
} `json:"patch"`
}
prCommit struct {
URL string `json:"url"`
Sha string `json:"sha"`
HtmlURL string `json:"html_url"`
CommentsURL string `json:"comments_url"`
Commit struct {
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Date time.Time `json:"date"`
Email string `json:"email"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Date time.Time `json:"date"`
Email string `json:"email"`
} `json:"committer"`
Message string `json:"message"`
CommentCount int `json:"comment_count"`
} `json:"commit"`
Author author `json:"author"`
Committer author `json:"committer"`
Parents struct {
URL string `json:"url"`
Sha string `json:"sha"`
} `json:"parents"`
}
)
type prInput struct {
Title string `json:"title"`
Body string `json:"body"`
Head string `json:"head"`
Base string `json:"base"`
}
type prCommentInput struct {
Body string `json:"body"`
}
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 {
var labels []scm.Label
for _, label := range from.Labels {
labels = append(labels, scm.Label{
Name: label.Name,
Color: label.Color,
})
}
merged := from.State == "merged"
closed := from.State == "closed"
if merged {
closed = true
}
return &scm.PullRequest{
Number: from.Number,
Title: from.Title,
Body: from.Body,
Sha: from.Head.Sha,
Ref: fmt.Sprintf("refs/pull/%d/head", from.Number),
Source: from.Head.Ref,
Target: from.Base.Ref,
Fork: from.Head.Repo.FullName,
Link: from.HtmlURL,
Diff: from.DiffURL,
Closed: closed,
Merged: merged,
Head: scm.Reference{
Name: from.Head.Ref,
Path: scm.ExpandRef(from.Head.Ref, "refs/heads"),
Sha: from.Head.Sha,
},
Base: scm.Reference{
Name: from.Base.Ref,
Path: scm.ExpandRef(from.Base.Ref, "refs/heads"),
Sha: from.Base.Sha,
},
Author: scm.User{
Login: from.User.Login,
Avatar: from.User.AvatarURL,
},
Created: from.CreatedAt,
Updated: from.UpdatedAt,
Labels: labels,
}
}
func convertPullRequestComments(from []*prComment) []*scm.Comment {
to := []*scm.Comment{}
for _, v := range from {
to = append(to, convertPullRequestComment(v))
}
return to
}
func convertPullRequestComment(from *prComment) *scm.Comment {
return &scm.Comment{
ID: from.ID,
Body: from.Body,
Author: scm.User{
Login: from.User.Login,
Name: from.User.Name,
Avatar: from.User.AvatarURL,
},
Created: from.CreatedAt,
Updated: from.UpdatedAt,
}
}
func convertPrChangeList(from []*prFile) []*scm.Change {
to := []*scm.Change{}
for _, v := range from {
to = append(to, convertPrChange(v))
}
return to
}
func convertPrChange(from *prFile) *scm.Change {
return &scm.Change{
Path: from.Filename,
Added: from.Status == "added",
Deleted: from.Status == "deleted",
Renamed: from.Status == "renamed",
BlobID: from.Sha,
}
}
func convertPrCommitList(from []*prCommit) []*scm.Commit {
to := []*scm.Commit{}
for _, v := range from {
to = append(to, convertPrCommit(v))
}
return to
}
func convertPrCommit(from *prCommit) *scm.Commit {
return &scm.Commit{
Message: from.Commit.Message,
Sha: from.Sha,
Link: from.HtmlURL,
Author: scm.Signature{
Name: from.Commit.Author.Name,
Email: from.Commit.Author.Email,
Date: from.Commit.Author.Date,
Login: from.Author.Login,
Avatar: from.Author.AvatarURL,
},
Committer: scm.Signature{
Name: from.Commit.Committer.Name,
Email: from.Commit.Committer.Email,
Date: from.Commit.Committer.Date,
Login: from.Committer.Login,
Avatar: from.Committer.AvatarURL,
},
}
}