forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 83
/
pr.go
296 lines (264 loc) · 10 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
// 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 stash
import (
"context"
"fmt"
"strings"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type pullService struct {
client *wrapper
}
func (s *pullService) Find(ctx context.Context, repo string, number int) (*scm.PullRequest, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d", namespace, name, number)
out := new(pullRequest)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertPullRequest(out), res, err
}
func (s *pullService) FindComment(ctx context.Context, repo string, number int, id int) (*scm.Comment, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/comments/%d", namespace, name, number, id)
out := new(pullRequestComment)
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) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests", namespace, name)
out := new(pullRequests)
res, err := s.client.do(ctx, "GET", path, nil, out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertPullRequests(out), res, err
}
func (s *pullService) ListChanges(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Change, *scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/changes", namespace, name, number)
out := new(diffstats)
res, err := s.client.do(ctx, "GET", path, nil, out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertDiffstats(out), res, err
}
func (s *pullService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
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) {
return nil, scm.ErrNotSupported
}
func (s *pullService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *pullService) ListComments(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
// TODO(bradrydzewski) the challenge with comments is that we need to use
// the activities endpoint, which returns entries that may or may not be
// comments. This complicates how we handle counts and pagination.
// GET /rest/api/1.0/projects/PRJ/repos/my-repo/pull-requests/1/activities
projectName, repoName := scm.Split(repo)
out := new(pullRequestComments)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/activities", projectName, repoName, number)
res, err := s.client.do(ctx, "GET", path, nil, out)
if !out.pagination.LastPage.Bool {
res.Page.First = 1
res.Page.Next = opts.Page + 1
}
return convertPullRequestComments(out), res, err
}
func (s *pullService) Merge(ctx context.Context, repo string, number int) (*scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/merge", namespace, name, number)
res, err := s.client.do(ctx, "POST", path, nil, nil)
return res, err
}
func (s *pullService) Close(ctx context.Context, repo string, number int) (*scm.Response, error) {
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/decline", namespace, name, number)
res, err := s.client.do(ctx, "POST", path, nil, nil)
return res, err
}
func (s *pullService) CreateComment(ctx context.Context, repo string, number int, in *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
input := pullRequestCommentInput{Text: in.Body}
namespace, name := scm.Split(repo)
path := fmt.Sprintf("rest/api/1.0/projects/%s/repos/%s/pull-requests/%d/comments", namespace, name, number)
out := new(pullRequestComment)
res, err := s.client.do(ctx, "POST", path, &input, out)
return convertPullRequestComment(out), res, err
}
func (s *pullService) DeleteComment(context.Context, string, int, int) (*scm.Response, error) {
// TODO(bradrydzewski) the challenge with deleting comments is that we need to specify
// the comment version number. The proposal is to use 0 as the initial version number,
// and then to use expectedVersion on error and re-attempt the API call.
// DELETE /rest/api/1.0/projects/PRJ/repos/my-repo/pull-requests/1/comments/1?version=0
return nil, scm.ErrNotSupported
}
func (s *pullService) AssignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *pullService) UnassignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
return nil, scm.ErrNotSupported
}
func (s *pullService) Create(ctx context.Context, repo string, input *scm.PullRequestInput) (*scm.PullRequest, *scm.Response, error) {
return nil, nil, scm.ErrNotSupported
}
type pullRequest struct {
ID int `json:"id"`
Version int `json:"version"`
Title string `json:"title"`
Description string `json:"description"`
State string `json:"state"`
Open bool `json:"open"`
Closed bool `json:"closed"`
CreatedDate int64 `json:"createdDate"`
UpdatedDate int64 `json:"updatedDate"`
FromRef struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
LatestCommit string `json:"latestCommit"`
Repository repository `json:"repository"`
} `json:"fromRef"`
ToRef struct {
ID string `json:"id"`
DisplayID string `json:"displayId"`
LatestCommit string `json:"latestCommit"`
Repository repository `json:"repository"`
} `json:"toRef"`
Locked bool `json:"locked"`
Author struct {
User struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
ID int `json:"id"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
Slug string `json:"slug"`
Type string `json:"type"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
} `json:"user"`
Role string `json:"role"`
Approved bool `json:"approved"`
Status string `json:"status"`
} `json:"author"`
Reviewers []interface{} `json:"reviewers"`
Participants []interface{} `json:"participants"`
Links struct {
Self []link `json:"self"`
} `json:"links"`
}
type pullRequests struct {
pagination
Values []*pullRequest `json:"values"`
}
func convertPullRequests(from *pullRequests) []*scm.PullRequest {
to := []*scm.PullRequest{}
for _, v := range from.Values {
to = append(to, convertPullRequest(v))
}
return to
}
func convertPullRequest(from *pullRequest) *scm.PullRequest {
fork := scm.Join(
from.FromRef.Repository.Project.Key,
from.FromRef.Repository.Slug,
)
return &scm.PullRequest{
Number: from.ID,
Title: from.Title,
Body: from.Description,
Sha: from.FromRef.LatestCommit,
Ref: fmt.Sprintf("refs/pull-requests/%d/from", from.ID),
Source: from.FromRef.DisplayID,
Target: from.ToRef.DisplayID,
Fork: fork,
Base: scm.PullRequestBranch{
Sha: from.FromRef.LatestCommit,
},
Head: scm.PullRequestBranch{
Sha: from.FromRef.LatestCommit,
},
Link: extractSelfLink(from.Links.Self),
State: strings.ToLower(from.State),
Closed: from.Closed,
Merged: from.State == "MERGED",
Created: time.Unix(from.CreatedDate/1000, 0),
Updated: time.Unix(from.UpdatedDate/1000, 0),
Author: scm.User{
Login: from.Author.User.Slug,
Name: from.Author.User.DisplayName,
Email: from.Author.User.EmailAddress,
Avatar: avatarLink(from.Author.User.EmailAddress),
},
}
}
type pullRequestComment struct {
Properties struct {
RepositoryID int `json:"repositoryId"`
} `json:"properties"`
ID int `json:"id"`
Version int `json:"version"`
Text string `json:"text"`
Author struct {
Name string `json:"name"`
EmailAddress string `json:"emailAddress"`
ID int `json:"id"`
DisplayName string `json:"displayName"`
Active bool `json:"active"`
Slug string `json:"slug"`
Type string `json:"type"`
Links struct {
Self []struct {
Href string `json:"href"`
} `json:"self"`
} `json:"links"`
} `json:"author"`
CreatedDate int64 `json:"createdDate"`
UpdatedDate int64 `json:"updatedDate"`
Comments []pullRequestComment `json:"comments"`
Tasks []interface{} `json:"tasks"`
PermittedOperations struct {
Editable bool `json:"editable"`
Deletable bool `json:"deletable"`
} `json:"permittedOperations"`
}
type pullRequestComments struct {
pagination
Values []*pullRequestComment `json:"values"`
}
type pullRequestCommentInput struct {
Text string `json:"text"`
}
func convertPullRequestComments(from *pullRequestComments) []*scm.Comment {
to := []*scm.Comment{}
for _, v := range from.Values {
to = append(to, convertPullRequestComment(v))
}
return to
}
func convertPullRequestComment(from *pullRequestComment) *scm.Comment {
return &scm.Comment{
ID: from.ID,
Body: from.Text,
Created: time.Unix(from.CreatedDate/1000, 0),
Updated: time.Unix(from.UpdatedDate/1000, 0),
Author: scm.User{
Login: from.Author.Slug,
Name: from.Author.DisplayName,
Email: from.Author.EmailAddress,
Avatar: avatarLink(from.Author.EmailAddress),
},
}
}