-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpullrequest.go
More file actions
420 lines (364 loc) · 17.4 KB
/
pullrequest.go
File metadata and controls
420 lines (364 loc) · 17.4 KB
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
package backlog
import (
"context"
"time"
"github.com/nattokin/go-backlog/internal/attachment"
"github.com/nattokin/go-backlog/internal/comment"
"github.com/nattokin/go-backlog/internal/core"
"github.com/nattokin/go-backlog/internal/model"
"github.com/nattokin/go-backlog/internal/pullrequest"
)
// PullRequest represents pull request of Backlog git.
type PullRequest struct {
ID int
ProjectID int
RepositoryID int
Number int
Summary string
Description string
Base string
Branch string
Status *Status
Assignee *User
Issue *Issue
BaseCommit string
BranchCommit string
CloseAt time.Time
MergeAt time.Time
CreatedUser *User
Created time.Time
UpdatedUser *User
Updated time.Time
Attachments []*Attachment
Stars []*Star
}
// ──────────────────────────────────────────────────────────────
// PullRequestService
// ──────────────────────────────────────────────────────────────
// PullRequestService handles communication with the pull request-related methods of the Backlog API.
type PullRequestService struct {
base *pullrequest.Service
Attachment *PullRequestAttachmentService
Comment *PullRequestCommentService
Option *PullRequestOptionService
Star *PullRequestStarService
}
// All returns a list of pull requests.
//
// This method supports options returned by methods in "*Client.PullRequest.Option",
// such as:
// - WithStatusIDs
// - WithAssigneeIDs
// - WithIssueIDs
// - WithCreatedUserIDs
// - WithOffset
// - WithCount
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-pull-request-list
func (s *PullRequestService) All(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, opts ...RequestOption) ([]*PullRequest, error) {
v, err := s.base.All(ctx, projectIDOrKey, repositoryIDOrName, toCoreOptions(opts)...)
return pullRequestsFromModel(v), convertError(err)
}
// Count returns the number of pull requests.
//
// This method supports options returned by methods in "*Client.PullRequest.Option",
// such as:
// - WithStatusIDs
// - WithAssigneeIDs
// - WithIssueIDs
// - WithCreatedUserIDs
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-number-of-pull-requests
func (s *PullRequestService) Count(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, opts ...RequestOption) (int, error) {
count, err := s.base.Count(ctx, projectIDOrKey, repositoryIDOrName, toCoreOptions(opts)...)
return count, convertError(err)
}
// One returns a single pull request by its number.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-pull-request
func (s *PullRequestService) One(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int) (*PullRequest, error) {
v, err := s.base.One(ctx, projectIDOrKey, repositoryIDOrName, prNumber)
return pullRequestFromModel(v), convertError(err)
}
// Create creates a new pull request.
//
// This method supports options returned by methods in "*Client.PullRequest.Option",
// such as:
// - WithIssueID
// - WithAssigneeID
// - WithNotifiedUserIDs
// - WithAttachmentIDs
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/add-pull-request
func (s *PullRequestService) Create(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, summary string, description string, base string, branch string, opts ...RequestOption) (*PullRequest, error) {
v, err := s.base.Create(ctx, projectIDOrKey, repositoryIDOrName, summary, description, base, branch, toCoreOptions(opts)...)
return pullRequestFromModel(v), convertError(err)
}
// Update updates an existing pull request.
//
// At least one option is required. This method supports options returned by
// methods in "*Client.PullRequest.Option", such as:
// - WithSummary
// - WithDescription
// - WithIssueID
// - WithAssigneeID
// - WithNotifiedUserIDs
// - WithComment
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/update-pull-request
func (s *PullRequestService) Update(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int, option RequestOption, opts ...RequestOption) (*PullRequest, error) {
v, err := s.base.Update(ctx, projectIDOrKey, repositoryIDOrName, prNumber, option, toCoreOptions(opts)...)
return pullRequestFromModel(v), convertError(err)
}
// ──────────────────────────────────────────────────────────────
// PullRequestAttachmentService
// ──────────────────────────────────────────────────────────────
// PullRequestAttachmentService handles communication with the pull request attachment-related methods of the Backlog API.
type PullRequestAttachmentService struct {
base *attachment.PullRequestService
}
// List returns a list of all attachments in the pull request.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-list-of-pull-request-attachment
func (s *PullRequestAttachmentService) List(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int) ([]*Attachment, error) {
v, err := s.base.List(ctx, projectIDOrKey, repositoryIDOrName, prNumber)
return attachmentsFromModel(v), convertError(err)
}
// Remove removes a file attached to the pull request.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/delete-pull-request-attachments
func (s *PullRequestAttachmentService) Remove(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int, attachmentID int) (*Attachment, error) {
v, err := s.base.Remove(ctx, projectIDOrKey, repositoryIDOrName, prNumber, attachmentID)
return attachmentFromModel(v), convertError(err)
}
// ──────────────────────────────────────────────────────────────
// PullRequestCommentService
// ──────────────────────────────────────────────────────────────
// PullRequestCommentService handles communication with the pull request comment-related methods of the Backlog API.
type PullRequestCommentService struct {
base *comment.PullRequestService
Option *PullRequestCommentOptionService
}
// All returns a list of comments on a pull request.
//
// This method supports options returned by methods in "*Client.PullRequest.Comment.Option",
// such as:
// - WithMinID
// - WithMaxID
// - WithCount
// - WithOrder
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-pull-request-comment
func (s *PullRequestCommentService) All(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int, opts ...RequestOption) ([]*Comment, error) {
v, err := s.base.All(ctx, projectIDOrKey, repositoryIDOrName, prNumber, toCoreOptions(opts)...)
return commentsFromModel(v), convertError(err)
}
// Add adds a comment to a pull request.
//
// This method supports options returned by methods in "*Client.PullRequest.Comment.Option",
// such as:
// - WithNotifiedUserIDs
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/add-pull-request-comment
func (s *PullRequestCommentService) Add(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int, content string, opts ...RequestOption) (*Comment, error) {
v, err := s.base.Add(ctx, projectIDOrKey, repositoryIDOrName, prNumber, content, toCoreOptions(opts)...)
return commentFromModel(v), convertError(err)
}
// Count returns the number of comments on a pull request.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/get-number-of-pull-request-comments
func (s *PullRequestCommentService) Count(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int) (int, error) {
count, err := s.base.Count(ctx, projectIDOrKey, repositoryIDOrName, prNumber)
return count, convertError(err)
}
// Update updates a comment on a pull request.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/update-pull-request-comment-information
func (s *PullRequestCommentService) Update(ctx context.Context, projectIDOrKey string, repositoryIDOrName string, prNumber int, commentID int, content string) (*Comment, error) {
v, err := s.base.Update(ctx, projectIDOrKey, repositoryIDOrName, prNumber, commentID, content)
return commentFromModel(v), convertError(err)
}
// ──────────────────────────────────────────────────────────────
// PullRequestCommentOptionService
// ──────────────────────────────────────────────────────────────
// PullRequestCommentOptionService provides a domain-specific set of option builders
// for operations within the PullRequestCommentService.
type PullRequestCommentOptionService struct {
base *core.OptionService
}
// WithCount sets the number of comments to retrieve (1-100).
func (s *PullRequestCommentOptionService) WithCount(count int) RequestOption {
return s.base.WithCount(count)
}
// WithMaxID filters comments with ID at or below the given value.
func (s *PullRequestCommentOptionService) WithMaxID(id int) RequestOption {
return s.base.WithMaxID(id)
}
// WithMinID filters comments with ID at or above the given value.
func (s *PullRequestCommentOptionService) WithMinID(id int) RequestOption {
return s.base.WithMinID(id)
}
// WithNotifiedUserIDs returns an option to set multiple `notifiedUserId[]` parameters.
func (s *PullRequestCommentOptionService) WithNotifiedUserIDs(ids []int) RequestOption {
return s.base.WithNotifiedUserIDs(ids)
}
// WithOrder sets the sort order of results.
func (s *PullRequestCommentOptionService) WithOrder(order Order) RequestOption {
return s.base.WithOrder(model.Order(order))
}
// ──────────────────────────────────────────────────────────────
// PullRequestStarService
// ──────────────────────────────────────────────────────────────
// PullRequestStarService handles communication with the pull request star-related methods of the Backlog API.
type PullRequestStarService struct {
star *StarService
}
// Add adds a star to the pull request.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/add-star
func (s *PullRequestStarService) Add(ctx context.Context, pullRequestID int) error {
return s.star.Add(ctx, s.star.Option.WithPullRequestID(pullRequestID))
}
// Remove removes a star by its ID.
//
// Backlog API docs: https://developer.nulab.com/docs/backlog/api/2/remove-star
func (s *PullRequestStarService) Remove(ctx context.Context, starID int) error {
return s.star.Remove(ctx, starID)
}
// ──────────────────────────────────────────────────────────────
// PullRequestOptionService
// ──────────────────────────────────────────────────────────────
// PullRequestOptionService provides a domain-specific set of option builders
// for operations within the PullRequestService.
type PullRequestOptionService struct {
base *core.OptionService
}
// WithAssigneeID returns an option to set the `assigneeId` parameter.
func (s *PullRequestOptionService) WithAssigneeID(id int) RequestOption {
return s.base.WithAssigneeID(id)
}
// WithAssigneeIDs filters pull requests by assignee user IDs.
func (s *PullRequestOptionService) WithAssigneeIDs(ids []int) RequestOption {
return s.base.WithAssigneeIDs(ids)
}
// WithAttachmentIDs returns an option to set multiple `attachmentId[]` parameters.
func (s *PullRequestOptionService) WithAttachmentIDs(ids []int) RequestOption {
return s.base.WithAttachmentIDs(ids)
}
// WithComment returns an option to set the `comment` parameter.
func (s *PullRequestOptionService) WithComment(comment string) RequestOption {
return s.base.WithComment(comment)
}
// WithCount sets the number of pull requests to retrieve.
func (s *PullRequestOptionService) WithCount(count int) RequestOption {
return s.base.WithCount(count)
}
// WithCreatedUserIDs filters pull requests by created user IDs.
func (s *PullRequestOptionService) WithCreatedUserIDs(ids []int) RequestOption {
return s.base.WithCreatedUserIDs(ids)
}
// WithDescription returns an option to set the `description` parameter.
func (s *PullRequestOptionService) WithDescription(description string) RequestOption {
return s.base.WithDescription(description)
}
// WithIssueID returns an option to set the `issueId` parameter.
func (s *PullRequestOptionService) WithIssueID(id int) RequestOption {
return s.base.WithIssueID(id)
}
// WithIssueIDs filters pull requests by issue IDs.
func (s *PullRequestOptionService) WithIssueIDs(ids []int) RequestOption {
return s.base.WithIssueIDs(ids)
}
// WithNotifiedUserIDs returns an option to set multiple `notifiedUserId[]` parameters.
func (s *PullRequestOptionService) WithNotifiedUserIDs(ids []int) RequestOption {
return s.base.WithNotifiedUserIDs(ids)
}
// WithOffset sets the number of pull requests to skip.
func (s *PullRequestOptionService) WithOffset(offset int) RequestOption {
return s.base.WithOffset(offset)
}
// WithStatusIDs filters pull requests by status IDs.
func (s *PullRequestOptionService) WithStatusIDs(ids []int) RequestOption {
return s.base.WithStatusIDs(ids)
}
// WithSummary returns an option to set the `summary` parameter.
func (s *PullRequestOptionService) WithSummary(summary string) RequestOption {
return s.base.WithSummary(summary)
}
// ──────────────────────────────────────────────────────────────
// Constructors
// ──────────────────────────────────────────────────────────────
func newPullRequestService(method *core.Method, option *core.OptionService) *PullRequestService {
return &PullRequestService{
base: pullrequest.NewService(method),
Attachment: newPullRequestAttachmentService(method),
Comment: newPullRequestCommentService(method, option),
Option: newPullRequestOptionService(option),
Star: newPullRequestStarService(method, option),
}
}
func newPullRequestAttachmentService(method *core.Method) *PullRequestAttachmentService {
return &PullRequestAttachmentService{
base: attachment.NewPullRequestService(method),
}
}
func newPullRequestCommentService(method *core.Method, option *core.OptionService) *PullRequestCommentService {
return &PullRequestCommentService{
base: comment.NewPullRequestService(method),
Option: &PullRequestCommentOptionService{base: option},
}
}
func newPullRequestStarService(method *core.Method, option *core.OptionService) *PullRequestStarService {
return &PullRequestStarService{star: newStarService(method, option)}
}
func newPullRequestOptionService(option *core.OptionService) *PullRequestOptionService {
return &PullRequestOptionService{
base: option,
}
}
// ──────────────────────────────────────────────────────────────
// Helpers
// ──────────────────────────────────────────────────────────────
func pullRequestFromModel(m *model.PullRequest) *PullRequest {
if m == nil {
return nil
}
attachments := make([]*Attachment, len(m.Attachments))
for i, v := range m.Attachments {
attachments[i] = attachmentFromModel(v)
}
stars := make([]*Star, len(m.Stars))
for i, v := range m.Stars {
stars[i] = starFromModel(v)
}
return &PullRequest{
ID: m.ID,
ProjectID: m.ProjectID,
RepositoryID: m.RepositoryID,
Number: m.Number,
Summary: m.Summary,
Description: m.Description,
Base: m.Base,
Branch: m.Branch,
Status: statusFromModel(m.Status),
Assignee: userFromModel(m.Assignee),
Issue: issueFromModel(m.Issue),
BaseCommit: m.BaseCommit,
BranchCommit: m.BranchCommit,
CloseAt: m.CloseAt,
MergeAt: m.MergeAt,
CreatedUser: userFromModel(m.CreatedUser),
Created: m.Created,
UpdatedUser: userFromModel(m.UpdatedUser),
Updated: m.Updated,
Attachments: attachments,
Stars: stars,
}
}
func pullRequestsFromModel(ms []*model.PullRequest) []*PullRequest {
result := make([]*PullRequest, len(ms))
for i, v := range ms {
result[i] = pullRequestFromModel(v)
}
return result
}