forked from jenkins-x/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pr.go
487 lines (433 loc) · 15.8 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
// 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 gitlab
import (
"context"
"fmt"
"net/url"
"strconv"
"strings"
"time"
"github.com/mitchellh/copystructure"
"github.com/slimm609/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("api/v4/projects/%s/merge_requests/%d", encode(repo), number)
out := new(pr)
res, err := s.client.do(ctx, "GET", path, nil, out)
if err != nil {
return nil, res, err
}
convRepo, convRes, err := s.convertPullRequest(ctx, out)
if err != nil {
return nil, convRes, err
}
return convRepo, res, nil
}
func (s *pullService) FindComment(ctx context.Context, repo string, index, id int) (*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/notes/%d", encode(repo), index, id)
out := new(issueComment)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertIssueComment(out), res, err
}
func (s *pullService) List(ctx context.Context, repo string, opts scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests?%s", encode(repo), encodePullRequestListOptions(opts))
out := []*pr{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
if err != nil {
return nil, res, err
}
convRepos, convRes, err := s.convertPullRequestList(ctx, out)
if err != nil {
return nil, convRes, err
}
return convRepos, res, nil
}
func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/changes?%s", encode(repo), number, encodeListOptions(opts))
out := new(changes)
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertChangeList(out.Changes), res, err
}
func (s *pullService) ListComments(ctx context.Context, repo string, index int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/notes?%s", encode(repo), index, encodeListOptions(opts))
out := []*issueComment{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertIssueCommentList(out), res, err
}
func (s *pullService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
mr, _, err := s.Find(ctx, repo, number)
if err != nil {
return nil, nil, err
}
return mr.Labels, nil, nil
}
func (s *pullService) ListEvents(ctx context.Context, repo string, index int, opts scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/resource_label_events?%s", encode(repo), index, encodeListOptions(opts))
out := []*labelEvent{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertLabelEvents(out), res, err
}
func (s *pullService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
existingLabels, _, err := s.ListLabels(ctx, repo, number, scm.ListOptions{})
if err != nil {
return nil, err
}
allLabels := map[string]struct{}{}
for _, l := range existingLabels {
allLabels[l.Name] = struct{}{}
}
allLabels[label] = struct{}{}
labelNames := []string{}
for l := range allLabels {
labelNames = append(labelNames, l)
}
return s.setLabels(ctx, repo, number, labelNames)
}
func (s *pullService) setLabels(ctx context.Context, repo string, number int, labels []string) (*scm.Response, error) {
in := url.Values{}
labelsStr := strings.Join(labels, ",")
in.Set("labels", labelsStr)
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d?%s", encode(repo), number, in.Encode())
return s.client.do(ctx, "PUT", path, nil, nil)
}
func (s *pullService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
existingLabels, _, err := s.ListLabels(ctx, repo, number, scm.ListOptions{})
if err != nil {
return nil, err
}
labels := []string{}
for _, l := range existingLabels {
if l.Name != label {
labels = append(labels, l.Name)
}
}
return s.setLabels(ctx, repo, number, labels)
}
func (s *pullService) CreateComment(ctx context.Context, repo string, index int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
in := url.Values{}
in.Set("body", input.Body)
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/notes?%s", encode(repo), index, in.Encode())
out := new(issueComment)
res, err := s.client.do(ctx, "POST", path, nil, out)
return convertIssueComment(out), res, err
}
func (s *pullService) DeleteComment(ctx context.Context, repo string, index, id int) (*scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/notes/%d", encode(repo), index, id)
res, err := s.client.do(ctx, "DELETE", path, nil, nil)
return res, err
}
func (s *pullService) EditComment(ctx context.Context, repo string, number int, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
in := &updateNoteOptions{Body: input.Body}
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/notes/%d", encode(repo), number, id)
out := new(issueComment)
res, err := s.client.do(ctx, "PUT", path, in, out)
return convertIssueComment(out), res, err
}
func (s *pullService) Merge(ctx context.Context, repo string, number int, options *scm.PullRequestMergeOptions) (*scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d/merge", encode(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("api/v4/projects/%s/merge_requests/%d?state_event=closed", encode(repo), number)
res, err := s.client.do(ctx, "PUT", path, nil, nil)
return res, err
}
func (s *pullService) Reopen(ctx context.Context, repo string, number int) (*scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d?state_event=reopen", encode(repo), number)
res, err := s.client.do(ctx, "PUT", path, nil, nil)
return res, err
}
func (s *pullService) AssignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
pr, _, err := s.Find(ctx, repo, number)
if err != nil {
return nil, err
}
allAssignees := map[int]struct{}{}
for _, assignee := range pr.Assignees {
allAssignees[assignee.ID] = struct{}{}
}
for _, l := range logins {
u, _, err := s.client.Users.FindLogin(ctx, l)
if err != nil {
return nil, err
}
allAssignees[u.ID] = struct{}{}
}
var assigneeIDs []int
for i := range allAssignees {
assigneeIDs = append(assigneeIDs, i)
}
return s.setAssignees(ctx, repo, number, assigneeIDs)
}
func (s *pullService) setAssignees(ctx context.Context, repo string, number int, ids []int) (*scm.Response, error) {
if len(ids) == 0 {
ids = append(ids, 0)
}
in := &updateMergeRequestOptions{
AssigneeIDs: ids,
}
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d", encode(repo), number)
return s.client.do(ctx, "PUT", path, in, nil)
}
func (s *pullService) UnassignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
pr, _, err := s.Find(ctx, repo, number)
if err != nil {
return nil, err
}
var assignees []int
for _, assignee := range pr.Assignees {
shouldKeep := true
for _, l := range logins {
if assignee.Login == l {
shouldKeep = false
}
}
if shouldKeep {
assignees = append(assignees, assignee.ID)
}
}
return s.setAssignees(ctx, repo, number, assignees)
}
func (s *pullService) RequestReview(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
return s.AssignIssue(ctx, repo, number, logins)
}
func (s *pullService) UnrequestReview(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
return s.UnassignIssue(ctx, repo, number, logins)
}
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests", encode(repo))
in := &prInput{
Title: input.Title,
SourceBranch: input.Head,
TargetBranch: input.Base,
Description: input.Body,
}
out := new(pr)
res, err := s.client.do(ctx, "POST", path, in, out)
if err != nil {
return nil, res, err
}
convRepo, convRes, err := s.convertPullRequest(ctx, out)
if err != nil {
return nil, convRes, err
}
return convRepo, res, nil
}
func (s *pullService) Update(ctx context.Context, repo string, number int, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
updateOpts := &updateMergeRequestOptions{}
if input.Title != "" {
updateOpts.Title = &input.Title
}
if input.Body != "" {
updateOpts.Description = &input.Body
}
if input.Base != "" {
updateOpts.TargetBranch = &input.Base
}
return s.updateMergeRequestField(ctx, repo, number, updateOpts)
}
func (s *pullService) SetMilestone(ctx context.Context, repo string, prID int, number int) (*scm.Response, error) {
updateOpts := &updateMergeRequestOptions{
MilestoneID: &number,
}
_, res, err := s.updateMergeRequestField(ctx, repo, prID, updateOpts)
return res, err
}
func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int) (*scm.Response, error) {
zeroVal := 0
updateOpts := &updateMergeRequestOptions{
MilestoneID: &zeroVal,
}
_, res, err := s.updateMergeRequestField(ctx, repo, prID, updateOpts)
return res, err
}
type updateMergeRequestOptions struct {
Title *string `json:"title,omitempty"`
Description *string `json:"description,omitempty"`
TargetBranch *string `json:"target_branch,omitempty"`
AssigneeID *int `json:"assignee_id,omitempty"`
AssigneeIDs []int `json:"assignee_ids,omitempty"`
Labels *string `json:"labels,omitempty"`
MilestoneID *int `json:"milestone_id,omitempty"`
StateEvent *string `json:"state_event,omitempty"`
RemoveSourceBranch *bool `json:"remove_source_branch,omitempty"`
Squash *bool `json:"squash,omitempty"`
DiscussionLocked *bool `json:"discussion_locked,omitempty"`
AllowCollaboration *bool `json:"allow_collaboration,omitempty"`
}
func (s *pullService) updateMergeRequestField(ctx context.Context, repo string, number int, input *updateMergeRequestOptions) (*scm.PullRequest, *scm.Response, error) {
path := fmt.Sprintf("api/v4/projects/%s/merge_requests/%d", encode(repo), number)
out := new(pr)
res, err := s.client.do(ctx, "PUT", path, input, out)
if err != nil {
return nil, res, err
}
convRepo, convRes, err := s.convertPullRequest(ctx, out)
if err != nil {
return nil, convRes, err
}
return convRepo, res, nil
}
type pr struct {
Number int `json:"iid"`
Sha string `json:"sha"`
Title string `json:"title"`
Desc string `json:"description"`
State string `json:"state"`
SourceProjectID int `json:"source_project_id"`
TargetProjectID int `json:"target_project_id"`
Labels []*string `json:"labels"`
Link string `json:"web_url"`
WIP bool `json:"work_in_progress"`
Author user `json:"author"`
MergeStatus string `json:"merge_status"`
SourceBranch string `json:"source_branch"`
TargetBranch string `json:"target_branch"`
Created time.Time `json:"created_at"`
Updated time.Time `json:"updated_at"`
Closed time.Time
DiffRefs struct {
BaseSHA string `json:"base_sha"`
HeadSHA string `json:"head_sha"`
} `json:"diff_refs"`
Assignee *user `json:"assignee"`
Assignees []*user `json:"assignees"`
}
type changes struct {
Changes []*change
}
type change struct {
OldPath string `json:"old_path"`
NewPath string `json:"new_path"`
Added bool `json:"new_file"`
Renamed bool `json:"renamed_file"`
Deleted bool `json:"deleted_file"`
Diff string `json:"diff"`
}
type prInput struct {
Title string `json:"title"`
Description string `json:"description"`
SourceBranch string `json:"source_branch"`
TargetBranch string `json:"target_branch"`
}
type pullRequestMergeRequest struct {
CommitMessage string `json:"merge_commit_message,omitempty"`
SquashCommitMessage string `json:"squash_commit_message,omitempty"`
Squash string `json:"squash,omitempty"`
RemoveSourceBranch string `json:"should_remove_source_branch,omitempty"`
SHA string `json:"sha,omitempty"`
MergeWhenPipelineSucceeds string `json:"merge_when_pipeline_succeeds,omitempty"`
}
func (s *pullService) convertPullRequestList(ctx context.Context, from []*pr) ([]*scm.PullRequest, *scm.Response, error) {
to := []*scm.PullRequest{}
for _, v := range from {
converted, res, err := s.convertPullRequest(ctx, v)
if err != nil {
return nil, res, err
}
to = append(to, converted)
}
return to, nil, nil
}
func (s *pullService) convertPullRequest(ctx context.Context, from *pr) (*scm.PullRequest, *scm.Response, error) {
// Diff refs only seem to be populated in more recent merge requests. Default
// to from.Sha for compatibility / consistency, but fallback to HeadSHA if
// it's not populated.
headSHA := from.Sha
if headSHA == "" && from.DiffRefs.HeadSHA != "" {
headSHA = from.DiffRefs.HeadSHA
}
var assignees []scm.User
if from.Assignee != nil {
assignees = append(assignees, *convertUser(from.Assignee))
}
for _, a := range from.Assignees {
assignees = append(assignees, *convertUser(a))
}
var res *scm.Response
baseRepo, res, err := s.client.Repositories.Find(ctx, strconv.Itoa(from.TargetProjectID))
if err != nil {
return nil, res, err
}
var headRepo *scm.Repository
if from.TargetProjectID == from.SourceProjectID {
repoCopy, err := copystructure.Copy(baseRepo)
if err != nil {
return nil, nil, err
}
headRepo = repoCopy.(*scm.Repository)
} else {
headRepo, res, err = s.client.Repositories.Find(ctx, strconv.Itoa(from.SourceProjectID))
if err != nil {
return nil, res, err
}
}
return &scm.PullRequest{
Number: from.Number,
Title: from.Title,
Body: from.Desc,
State: gitlabStateToSCMState(from.State),
Labels: convertPullRequestLabels(from.Labels),
Sha: from.Sha,
Ref: fmt.Sprintf("refs/merge-requests/%d/head", from.Number),
Source: from.SourceBranch,
Target: from.TargetBranch,
Link: from.Link,
Draft: from.WIP,
Closed: from.State != "opened",
Merged: from.State == "merged",
Mergeable: scm.ToMergeableState(from.MergeStatus) == scm.MergeableStateMergeable,
MergeableState: scm.ToMergeableState(from.MergeStatus),
Author: *convertUser(&from.Author),
Assignees: assignees,
Head: scm.PullRequestBranch{
Ref: from.SourceBranch,
Sha: headSHA,
Repo: *headRepo,
},
Base: scm.PullRequestBranch{
Ref: from.TargetBranch,
Sha: from.DiffRefs.BaseSHA,
Repo: *baseRepo,
},
Created: from.Created,
Updated: from.Updated,
}, nil, nil
}
func convertPullRequestLabels(from []*string) []*scm.Label {
var labels []*scm.Label
for _, label := range from {
l := *label
labels = append(labels, &scm.Label{
Name: l,
})
}
return labels
}
func convertChangeList(from []*change) []*scm.Change {
to := []*scm.Change{}
for _, v := range from {
to = append(to, convertChange(v))
}
return to
}
func convertChange(from *change) *scm.Change {
to := &scm.Change{
Path: from.NewPath,
PreviousPath: from.OldPath,
Added: from.Added,
Deleted: from.Deleted,
Renamed: from.Renamed,
Patch: from.Diff,
}
if to.Path == "" {
to.Path = from.OldPath
}
return to
}