forked from drone/go-scm
-
Notifications
You must be signed in to change notification settings - Fork 84
/
issue.go
385 lines (344 loc) · 11.8 KB
/
issue.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
// 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 github
import (
"context"
"fmt"
"net/http"
"strings"
"time"
"github.com/jenkins-x/go-scm/scm"
)
type issueService struct {
client *wrapper
}
type searchIssue struct {
issue
Score float32 `json:"score"`
AuthorAssociation string `json:"author_association"`
URL string `json:"url"`
RepositoryURL string `json:"repository_url"`
LabelsURL string `json:"labels_url"`
CommentsURL string `json:"comments_url"`
EventsURL string `json:"events_url"`
Comments int `json:"comments"`
}
type searchResults struct {
TotalCount int `json:"total_count"`
IncompleteResults bool `json:"incomplete_results"`
Items []*searchIssue `json:"items"`
}
func (s *issueService) Search(ctx context.Context, opts scm.SearchOptions) ([]*scm.SearchIssue, *scm.Response, error) {
suffix := encodeIssueSearchOptions(opts)
query := opts.QueryArgument()
if suffix != "" {
query += "&" + suffix
}
path := fmt.Sprintf("/search/issues?q=%s", query)
out := searchResults{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertSearchIssueList(out.Items), res, err
}
// AssignIssue adds logins to org/repo#number, returning an error if any login is missing after making the call.
//
// See https://developer.github.com/v3/issues/assignees/#add-assignees-to-an-issue
func (s *issueService) AssignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/assignees", repo, number)
in := map[string][]string{"assignees": logins}
out := new(issue)
res, err := s.client.do(ctx, "POST", path, in, out)
assigned := make(map[string]bool)
if err != nil {
return res, err
}
for _, assignee := range out.Assignees {
assigned[NormLogin(assignee.Login)] = true
}
missing := scm.MissingUsers{Action: "assign"}
for _, login := range logins {
if !assigned[NormLogin(login)] {
missing.Users = append(missing.Users, login)
}
}
if len(missing.Users) > 0 {
return res, missing
}
return res, nil
}
// UnassignIssue removes logins from org/repo#number, returns an error if any login remains assigned.
//
// See https://developer.github.com/v3/issues/assignees/#remove-assignees-from-an-issue
func (s *issueService) UnassignIssue(ctx context.Context, repo string, number int, logins []string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/assignees", repo, number)
in := map[string][]string{"assignees": logins}
out := new(issue)
res, err := s.client.do(ctx, http.MethodDelete, path, in, out)
assigned := make(map[string]bool)
if err != nil {
return res, err
}
for _, assignee := range out.Assignees {
assigned[NormLogin(assignee.Login)] = true
}
extra := scm.ExtraUsers{Action: "unassign"}
for _, login := range logins {
if assigned[NormLogin(login)] {
extra.Users = append(extra.Users, login)
}
}
if len(extra.Users) > 0 {
return res, extra
}
return res, nil
}
func (s *issueService) Find(ctx context.Context, repo string, number int) (*scm.Issue, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d", repo, number)
out := new(issue)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertIssue(out), res, err
}
func (s *issueService) FindComment(ctx context.Context, repo string, index, id int) (*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/comments/%d", repo, id)
out := new(issueComment)
res, err := s.client.do(ctx, "GET", path, nil, out)
return convertIssueComment(out), res, err
}
func (s *issueService) List(ctx context.Context, repo string, opts scm.IssueListOptions) ([]*scm.Issue, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues?%s", repo, encodeIssueListOptions(opts))
out := []*issue{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertIssueList(out), res, err
}
func (s *issueService) ListComments(ctx context.Context, repo string, index int, opts scm.ListOptions) ([]*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/comments?%s", repo, index, encodeListOptions(opts))
out := []*issueComment{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertIssueCommentList(out), res, err
}
func (s *issueService) Create(ctx context.Context, repo string, input *scm.IssueInput) (*scm.Issue, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues", repo)
in := &issueInput{
Title: input.Title,
Body: input.Body,
}
out := new(issue)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertIssue(out), res, err
}
func (s *issueService) ListLabels(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.Label, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/labels?%s", repo, number, encodeListOptions(opts))
out := []*label{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertLabelObjects(out), res, err
}
func (s *issueService) ListEvents(ctx context.Context, repo string, number int, opts scm.ListOptions) ([]*scm.ListedIssueEvent, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/events?%s", repo, number, encodeListOptions(opts))
out := []*listedIssueEvent{}
res, err := s.client.do(ctx, "GET", path, nil, &out)
return convertListedIssueEvents(out), res, err
}
func (s *issueService) AddLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/labels", repo, number)
in := []string{label}
res, err := s.client.do(ctx, "POST", path, in, nil)
return res, err
}
func (s *issueService) DeleteLabel(ctx context.Context, repo string, number int, label string) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/labels/%s", repo, number, label)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
func (s *issueService) CreateComment(ctx context.Context, repo string, number int, input *scm.CommentInput) (*scm.Comment, *scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/comments", repo, number)
in := &issueCommentInput{
Body: input.Body,
}
out := new(issueComment)
res, err := s.client.do(ctx, "POST", path, in, out)
return convertIssueComment(out), res, err
}
func (s *issueService) DeleteComment(ctx context.Context, repo string, number, id int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/comments/%d", repo, id)
return s.client.do(ctx, "DELETE", path, nil, nil)
}
func (s *issueService) Close(ctx context.Context, repo string, number int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d", repo, number)
data := map[string]string{"state": "closed"}
out := new(issue)
res, err := s.client.do(ctx, "PATCH", path, &data, out)
return res, err
}
func (s *issueService) Lock(ctx context.Context, repo string, number int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/lock", repo, number)
res, err := s.client.do(ctx, "PUT", path, nil, nil)
return res, err
}
func (s *issueService) Unlock(ctx context.Context, repo string, number int) (*scm.Response, error) {
path := fmt.Sprintf("repos/%s/issues/%d/lock", repo, number)
res, err := s.client.do(ctx, "DELETE", path, nil, nil)
return res, err
}
type issue struct {
ID int `json:"id"`
HTMLURL string `json:"html_url"`
Number int `json:"number"`
State string `json:"state"`
Title string `json:"title"`
Body string `json:"body"`
User struct {
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
} `json:"user"`
Labels []struct {
Name string `json:"name"`
} `json:"labels"`
Assignees []user `json:"assignees"`
Locked bool `json:"locked"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
// This will be non-nil if it is a pull request.
PullRequest *struct{} `json:"pull_request,omitempty"`
}
type issueInput struct {
Title string `json:"title"`
Body string `json:"body"`
}
type issueComment struct {
ID int `json:"id"`
HTMLURL string `json:"html_url"`
User struct {
ID int `json:"id"`
Login string `json:"login"`
AvatarURL string `json:"avatar_url"`
} `json:"user"`
Body string `json:"body"`
CreatedAt time.Time `json:"created_at"`
UpdatedAt time.Time `json:"updated_at"`
}
type issueCommentInput struct {
Body string `json:"body"`
}
// listedIssueEvent represents an issue event from the events API (not from a webhook payload).
// https://developer.github.com/v3/issues/events/
type listedIssueEvent struct {
Event string `json:"event"` // This is the same as IssueEvent.Action.
Actor user `json:"actor"`
Label label `json:"label"`
Created time.Time `json:"created_at"`
}
// helper function to convert from the gogs issue list to
// the common issue structure.
func convertIssueList(from []*issue) []*scm.Issue {
to := []*scm.Issue{}
for _, v := range from {
to = append(to, convertIssue(v))
}
return to
}
// helper function to convert from the gogs issue list to
// the common issue structure.
func convertSearchIssueList(from []*searchIssue) []*scm.SearchIssue {
to := []*scm.SearchIssue{}
for _, v := range from {
issue := convertIssue(&v.issue)
searchIssue := &scm.SearchIssue{
Issue: *issue,
}
populateRepositoryFromURL(&searchIssue.Repository, v.RepositoryURL)
to = append(to, searchIssue)
}
return to
}
func populateRepositoryFromURL(repo *scm.Repository, u string) {
if u == "" {
return
}
paths := strings.Split(strings.TrimSuffix(u, "/"), "/")
l := len(paths)
if l > 0 {
repo.Name = paths[l-1]
}
if l > 1 {
repo.Namespace = paths[l-2]
}
}
// helper function to convert from the gogs issue structure to
// the common issue structure.
func convertIssue(from *issue) *scm.Issue {
return &scm.Issue{
Number: from.Number,
Title: from.Title,
Body: from.Body,
Link: from.HTMLURL,
Labels: convertLabels(from),
Locked: from.Locked,
State: from.State,
Closed: from.State == "closed",
Author: scm.User{
Login: from.User.Login,
Avatar: from.User.AvatarURL,
},
Assignees: convertUsers(from.Assignees),
PullRequest: from.PullRequest != nil,
Created: from.CreatedAt,
Updated: from.UpdatedAt,
}
}
// helper function to convert from the gogs issue comment list
// to the common issue structure.
func convertIssueCommentList(from []*issueComment) []*scm.Comment {
to := []*scm.Comment{}
for _, v := range from {
to = append(to, convertIssueComment(v))
}
return to
}
// helper function to convert from the gogs issue comment to
// the common issue comment structure.
func convertIssueComment(from *issueComment) *scm.Comment {
return &scm.Comment{
ID: from.ID,
Body: from.Body,
Author: scm.User{
Login: from.User.Login,
Avatar: from.User.AvatarURL,
},
Link: from.HTMLURL,
Created: from.CreatedAt,
Updated: from.UpdatedAt,
}
}
func convertLabels(from *issue) []string {
var labels []string
for _, label := range from.Labels {
labels = append(labels, label.Name)
}
return labels
}
func convertLabelObjects(from []*label) []*scm.Label {
var labels []*scm.Label
for _, label := range from {
labels = append(labels, &scm.Label{
Name: label.Name,
Description: label.Description,
URL: label.URL,
Color: label.Color,
})
}
return labels
}
func convertListedIssueEvents(src []*listedIssueEvent) []*scm.ListedIssueEvent {
var answer []*scm.ListedIssueEvent
for _, from := range src {
answer = append(answer, convertListedIssueEvent(from))
}
return answer
}
func convertListedIssueEvent(from *listedIssueEvent) *scm.ListedIssueEvent {
return &scm.ListedIssueEvent{
Event: from.Event,
Actor: *convertUser(&from.Actor),
Label: convertLabel(from.Label),
Created: from.Created,
}
}