forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 86
/
pr.go
369 lines (320 loc) · 10.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
package fake
import (
"context"
"fmt"
"regexp"
"sort"
"strings"
"github.com/jenkins-x/go-scm/scm"
"k8s.io/apimachinery/pkg/util/sets"
)
type pullService struct {
client *wrapper
data *Data
}
func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
f := s.data
val, exists := f.PullRequests[number]
if !exists {
return nil, nil, fmt.Errorf("pull request number %d does not exit", number)
}
return val, nil, nil
}
func (s *pullService) FindComment(context.Context, string, int, int) (*scm.Comment, *scm.Response, error) {
panic("implement me")
}
func (s *pullService) List(ctx context.Context, fullName string, opts *scm.PullRequestListOptions) ([]*scm.PullRequest, *scm.Response, error) {
var allPullRequests []*scm.PullRequest
f := s.data
keys := make([]int, 0, len(f.PullRequests))
for prKey := range f.PullRequests {
keys = append(keys, prKey)
}
sort.Ints(keys)
for _, prKey := range keys {
pr := f.PullRequests[prKey]
repo := pr.Repository()
fn := repo.FullName
if fn == "" {
fn = scm.Join(repo.Namespace, repo.Name)
}
if fn == fullName {
allPullRequests = append(allPullRequests, pr)
}
}
filteredPullRequests := filterPullRequests(allPullRequests, opts)
returnStart, returnEnd := paginated(opts.Page, opts.Size, len(filteredPullRequests))
return filteredPullRequests[returnStart:returnEnd], nil, nil
}
func filterPullRequests(requests []*scm.PullRequest, opts *scm.PullRequestListOptions) []*scm.PullRequest {
var filteredPullRequests, newFilteredPullRequests []int
for i := 0; i < len(requests); i++ {
filteredPullRequests = append(filteredPullRequests, i)
}
for _, i := range filteredPullRequests {
if opts.Closed && requests[i].Closed {
newFilteredPullRequests = append(newFilteredPullRequests, i)
}
if opts.Open && !requests[i].Closed {
newFilteredPullRequests = append(newFilteredPullRequests, i)
}
}
filteredPullRequests = newFilteredPullRequests
newFilteredPullRequests = nil
if opts.CreatedBefore != nil {
for _, i := range filteredPullRequests {
if requests[i].Created.Before(*opts.CreatedBefore) {
newFilteredPullRequests = append(newFilteredPullRequests, i)
}
}
filteredPullRequests = newFilteredPullRequests
newFilteredPullRequests = nil
}
if opts.UpdatedBefore != nil {
for _, i := range filteredPullRequests {
if requests[i].Updated.Before(*opts.UpdatedBefore) {
newFilteredPullRequests = append(newFilteredPullRequests, i)
}
}
filteredPullRequests = newFilteredPullRequests
newFilteredPullRequests = nil
}
if opts.CreatedAfter != nil {
for _, i := range filteredPullRequests {
if requests[i].Created.After(*opts.CreatedAfter) {
newFilteredPullRequests = append(newFilteredPullRequests, i)
}
}
filteredPullRequests = newFilteredPullRequests
newFilteredPullRequests = nil
}
if opts.UpdatedAfter != nil {
for _, i := range filteredPullRequests {
if requests[i].Updated.After(*opts.UpdatedAfter) {
newFilteredPullRequests = append(newFilteredPullRequests, i)
}
}
filteredPullRequests = newFilteredPullRequests
}
if len(opts.Labels) > 0 {
panic("implement me")
}
returnRequests := []*scm.PullRequest{}
for _, i := range filteredPullRequests {
returnRequests = append(returnRequests, requests[i])
}
return returnRequests
}
func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts *scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
f := s.data
returnStart, returnEnd := paginated(opts.Page, opts.Size, len(f.PullRequestChanges[number]))
return f.PullRequestChanges[number][returnStart:returnEnd], nil, nil
}
func (s *pullService) ListCommits(ctx context.Context, repo string, number int, opts *scm.ListOptions) ([]*scm.Commit, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *pullService) ListComments(ctx context.Context, repo string, number int, opts *scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
f := s.data
return append([]*scm.Comment{}, f.PullRequestComments[number]...), nil, nil
}
func (s *pullService) ListLabels(ctx context.Context, repo string, number int, opts *scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
f := s.data
re := regexp.MustCompile(fmt.Sprintf(`^%s#%d:(.*)$`, repo, number))
la := []*scm.Label{}
allLabels := sets.NewString(f.PullRequestLabelsExisting...)
allLabels.Insert(f.PullRequestLabelsAdded...)
allLabels.Delete(f.PullRequestLabelsRemoved...)
for _, l := range allLabels.List() {
groups := re.FindStringSubmatch(l)
if groups != nil {
la = append(la, &scm.Label{Name: groups[1]})
}
}
return la, nil, nil
}
func (s *pullService) ListEvents(context.Context, string, int, *scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *pullService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
f := s.data
pr := s.data.PullRequests[number]
if pr != nil {
found := false
for _, l := range pr.Labels {
if l.Name == label {
found = true
break
}
}
if !found {
pr.Labels = append(pr.Labels, &scm.Label{
ID: int64(len(pr.Labels)),
Name: label,
})
}
}
labelString := fmt.Sprintf("%s#%d:%s", repo, number, label)
if sets.NewString(f.PullRequestLabelsAdded...).Has(labelString) {
return nil, fmt.Errorf("cannot add %v to %s/#%d", label, repo, number)
}
if f.RepoLabelsExisting == nil {
f.PullRequestLabelsAdded = append(f.PullRequestLabelsAdded, labelString)
return nil, nil
}
for _, l := range f.RepoLabelsExisting {
if label == l {
f.PullRequestLabelsAdded = append(f.PullRequestLabelsAdded, labelString)
return nil, nil
}
}
return nil, fmt.Errorf("cannot add %v to %s/#%d", label, repo, number)
}
// DeleteLabel removes a label
func (s *pullService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
f := s.data
labelString := fmt.Sprintf("%s#%d:%s", repo, number, label)
if !sets.NewString(f.PullRequestLabelsRemoved...).Has(labelString) {
f.PullRequestLabelsRemoved = append(f.PullRequestLabelsRemoved, labelString)
return nil, nil
}
return nil, fmt.Errorf("cannot remove %v from %s/#%d", label, repo, number)
}
func (s *pullService) Merge(ctx context.Context, repo string, number int, mergeOpts *scm.PullRequestMergeOptions) (*scm.Response, error) {
pr, ok := s.data.PullRequests[number]
if !ok || pr == nil {
return nil, fmt.Errorf("pull request %d not found", number)
}
pr.Merged = true
pr.State = "closed"
pr.Closed = true
pr.Mergeable = false
return nil, nil
}
func (s *pullService) Update(_ context.Context, fullName string, number int, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
f := s.data
namespace := ""
name := ""
paths := strings.SplitN(fullName, "/", 2)
if len(paths) > 1 {
namespace = paths[0]
name = paths[1]
}
answer := &scm.PullRequest{
Number: number,
Title: input.Title,
Body: input.Body,
Base: scm.PullRequestBranch{
Ref: input.Base,
Repo: scm.Repository{
Namespace: namespace,
Name: name,
FullName: fullName,
},
},
Head: scm.PullRequestBranch{
Ref: input.Head,
},
}
f.PullRequestsCreated[number] = input
f.PullRequests[number] = answer
return answer, nil, nil
}
func (s *pullService) Close(context.Context, string, int) (*scm.Response, error) {
panic("implement me")
}
func (s *pullService) Reopen(context.Context, string, int) (*scm.Response, error) {
panic("implement me")
}
func (s *pullService) CreateComment(ctx context.Context, repo string, number int, comment *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
f := s.data
f.PullRequestCommentsAdded = append(f.PullRequestCommentsAdded, fmt.Sprintf("%s#%d:%s", repo, number, comment.Body))
answer := &scm.Comment{
ID: f.IssueCommentID,
Body: comment.Body,
Author: scm.User{Login: botName},
}
f.PullRequestComments[number] = append(f.PullRequestComments[number], answer)
f.IssueCommentID++
return answer, nil, nil
}
func (s *pullService) DeleteComment(ctx context.Context, repo string, number, id int) (*scm.Response, error) {
f := s.data
f.PullRequestCommentsDeleted = append(f.PullRequestCommentsDeleted, fmt.Sprintf("%s#%d", repo, id))
for num, ics := range f.PullRequestComments {
for i, ic := range ics {
if ic.ID == id {
f.PullRequestComments[num] = append(ics[:i], ics[i+1:]...)
return nil, nil
}
}
}
return nil, fmt.Errorf("could not find issue comment %d", id)
}
func (s *pullService) EditComment(ctx context.Context, repo string, number, id int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
func (s *pullService) AssignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
f := s.data
var m scm.MissingUsers
for _, a := range logins {
if a == "not-in-the-org" {
m.Users = append(m.Users, a)
continue
}
f.AssigneesAdded = append(f.AssigneesAdded, fmt.Sprintf("%s#%d:%s", repo, number, a))
}
if m.Users == nil {
return nil, nil
}
return nil, m
}
func (s *pullService) UnassignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
panic("implement me")
}
func (s *pullService) RequestReview(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *pullService) UnrequestReview(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *pullService) Create(_ context.Context, fullName string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
f := s.data
for i := range f.PullRequests {
if f.PullRequests[i].Head.Ref == input.Head && f.PullRequests[i].Base.Ref == input.Base && !f.PullRequests[i].Closed && !f.PullRequests[i].Merged {
return nil, nil, fmt.Errorf("open pull request from branch %s to branch %s exists, cannot open a duplicate", input.Head, input.Base)
}
}
f.PullRequestID++
namespace := ""
name := ""
paths := strings.SplitN(fullName, "/", 2)
if len(paths) > 1 {
namespace = paths[0]
name = paths[1]
}
answer := &scm.PullRequest{
Number: f.PullRequestID,
Title: input.Title,
Body: input.Body,
Base: scm.PullRequestBranch{
Ref: input.Base,
Repo: scm.Repository{
Namespace: namespace,
Name: name,
FullName: fullName,
},
},
Head: scm.PullRequestBranch{
Ref: input.Head,
},
}
f.PullRequestsCreated[f.PullRequestID] = input
f.PullRequests[f.PullRequestID] = answer
return answer, nil, nil
}
func (s *pullService) SetMilestone(ctx context.Context, repo string, prID, number int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *pullService) ClearMilestone(ctx context.Context, repo string, prID int) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}