1//
2// Copyright 2017, Sander van Harmelen
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17package gitlab
18
19import (
20 "encoding/json"
21 "fmt"
22 "strings"
23 "time"
24)
25
26// IssuesService handles communication with the issue related methods
27// of the GitLab API.
28//
29// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html
30type IssuesService struct {
31 client *Client
32 timeStats *timeStatsService
33}
34
35// IssueAuthor represents a author of the issue.
36type IssueAuthor struct {
37 ID int `json:"id"`
38 State string `json:"state"`
39 WebURL string `json:"web_url"`
40 Name string `json:"name"`
41 AvatarURL string `json:"avatar_url"`
42 Username string `json:"username"`
43}
44
45// IssueAssignee represents a assignee of the issue.
46type IssueAssignee struct {
47 ID int `json:"id"`
48 State string `json:"state"`
49 WebURL string `json:"web_url"`
50 Name string `json:"name"`
51 AvatarURL string `json:"avatar_url"`
52 Username string `json:"username"`
53}
54
55// IssueLinks represents links of the issue.
56type IssueLinks struct {
57 Self string `json:"self"`
58 Notes string `json:"notes"`
59 AwardEmoji string `json:"award_emoji"`
60 Project string `json:"project"`
61}
62
63// Issue represents a GitLab issue.
64//
65// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html
66type Issue struct {
67 ID int `json:"id"`
68 IID int `json:"iid"`
69 ProjectID int `json:"project_id"`
70 Milestone *Milestone `json:"milestone"`
71 Author *IssueAuthor `json:"author"`
72 Description string `json:"description"`
73 State string `json:"state"`
74 Assignees []*IssueAssignee `json:"assignees"`
75 Assignee *IssueAssignee `json:"assignee"`
76 Upvotes int `json:"upvotes"`
77 Downvotes int `json:"downvotes"`
78 Labels []string `json:"labels"`
79 Title string `json:"title"`
80 UpdatedAt *time.Time `json:"updated_at"`
81 CreatedAt *time.Time `json:"created_at"`
82 ClosedAt *time.Time `json:"closed_at"`
83 Subscribed bool `json:"subscribed"`
84 UserNotesCount int `json:"user_notes_count"`
85 DueDate *ISOTime `json:"due_date"`
86 WebURL string `json:"web_url"`
87 TimeStats *TimeStats `json:"time_stats"`
88 Confidential bool `json:"confidential"`
89 Weight int `json:"weight"`
90 DiscussionLocked bool `json:"discussion_locked"`
91 Links *IssueLinks `json:"_links"`
92 IssueLinkID int `json:"issue_link_id"`
93 MergeRequestCount int `json:"merge_requests_count"`
94}
95
96func (i Issue) String() string {
97 return Stringify(i)
98}
99
100// Labels is a custom type with specific marshaling characteristics.
101type Labels []string
102
103// MarshalJSON implements the json.Marshaler interface.
104func (l *Labels) MarshalJSON() ([]byte, error) {
105 return json.Marshal(strings.Join(*l, ","))
106}
107
108// ListIssuesOptions represents the available ListIssues() options.
109//
110// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-issues
111type ListIssuesOptions struct {
112 ListOptions
113 State *string `url:"state,omitempty" json:"state,omitempty"`
114 Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
115 WithLabelDetails *bool `url:"with_labels_details,omitempty" json:"with_labels_details,omitempty"`
116 Milestone *string `url:"milestone,omitempty" json:"milestone,omitempty"`
117 Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
118 AuthorID *int `url:"author_id,omitempty" json:"author_id,omitempty"`
119 AssigneeID *int `url:"assignee_id,omitempty" json:"assignee_id,omitempty"`
120 MyReactionEmoji *string `url:"my_reaction_emoji,omitempty" json:"my_reaction_emoji,omitempty"`
121 IIDs []int `url:"iids[],omitempty" json:"iids,omitempty"`
122 OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
123 Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
124 Search *string `url:"search,omitempty" json:"search,omitempty"`
125 CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
126 CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
127 UpdatedAfter *time.Time `url:"updated_after,omitempty" json:"updated_after,omitempty"`
128 UpdatedBefore *time.Time `url:"updated_before,omitempty" json:"updated_before,omitempty"`
129 Confidential *bool `url:"confidential,omitempty" json:"confidential,omitempty"`
130}
131
132// ListIssues gets all issues created by authenticated user. This function
133// takes pagination parameters page and per_page to restrict the list of issues.
134//
135// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-issues
136func (s *IssuesService) ListIssues(opt *ListIssuesOptions, options ...OptionFunc) ([]*Issue, *Response, error) {
137 req, err := s.client.NewRequest("GET", "issues", opt, options)
138 if err != nil {
139 return nil, nil, err
140 }
141
142 var i []*Issue
143 resp, err := s.client.Do(req, &i)
144 if err != nil {
145 return nil, resp, err
146 }
147
148 return i, resp, err
149}
150
151// ListGroupIssuesOptions represents the available ListGroupIssues() options.
152//
153// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-group-issues
154type ListGroupIssuesOptions struct {
155 ListOptions
156 State *string `url:"state,omitempty" json:"state,omitempty"`
157 Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
158 IIDs []int `url:"iids[],omitempty" json:"iids,omitempty"`
159 Milestone *string `url:"milestone,omitempty" json:"milestone,omitempty"`
160 Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
161 AuthorID *int `url:"author_id,omitempty" json:"author_id,omitempty"`
162 AssigneeID *int `url:"assignee_id,omitempty" json:"assignee_id,omitempty"`
163 MyReactionEmoji *string `url:"my_reaction_emoji,omitempty" json:"my_reaction_emoji,omitempty"`
164 OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
165 Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
166 Search *string `url:"search,omitempty" json:"search,omitempty"`
167 In *string `url:"in,omitempty" json:"in,omitempty"`
168 CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
169 CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
170 UpdatedAfter *time.Time `url:"updated_after,omitempty" json:"updated_after,omitempty"`
171 UpdatedBefore *time.Time `url:"updated_before,omitempty" json:"updated_before,omitempty"`
172}
173
174// ListGroupIssues gets a list of group issues. This function accepts
175// pagination parameters page and per_page to return the list of group issues.
176//
177// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-group-issues
178func (s *IssuesService) ListGroupIssues(pid interface{}, opt *ListGroupIssuesOptions, options ...OptionFunc) ([]*Issue, *Response, error) {
179 group, err := parseID(pid)
180 if err != nil {
181 return nil, nil, err
182 }
183 u := fmt.Sprintf("groups/%s/issues", pathEscape(group))
184
185 req, err := s.client.NewRequest("GET", u, opt, options)
186 if err != nil {
187 return nil, nil, err
188 }
189
190 var i []*Issue
191 resp, err := s.client.Do(req, &i)
192 if err != nil {
193 return nil, resp, err
194 }
195
196 return i, resp, err
197}
198
199// ListProjectIssuesOptions represents the available ListProjectIssues() options.
200//
201// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-project-issues
202type ListProjectIssuesOptions struct {
203 ListOptions
204 IIDs []int `url:"iids[],omitempty" json:"iids,omitempty"`
205 State *string `url:"state,omitempty" json:"state,omitempty"`
206 Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
207 WithLabelDetails *bool `url:"with_labels_details,omitempty" json:"with_labels_details,omitempty"`
208 Milestone *string `url:"milestone,omitempty" json:"milestone,omitempty"`
209 Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
210 AuthorID *int `url:"author_id,omitempty" json:"author_id,omitempty"`
211 AssigneeID *int `url:"assignee_id,omitempty" json:"assignee_id,omitempty"`
212 MyReactionEmoji *string `url:"my_reaction_emoji,omitempty" json:"my_reaction_emoji,omitempty"`
213 OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
214 Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
215 Search *string `url:"search,omitempty" json:"search,omitempty"`
216 In *string `url:"in,omitempty" json:"in,omitempty"`
217 CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
218 CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
219 UpdatedAfter *time.Time `url:"updated_after,omitempty" json:"updated_after,omitempty"`
220 UpdatedBefore *time.Time `url:"updated_before,omitempty" json:"updated_before,omitempty"`
221 Confidential *bool `url:"confidential,omitempty" json:"confidential,omitempty"`
222}
223
224// ListProjectIssues gets a list of project issues. This function accepts
225// pagination parameters page and per_page to return the list of project issues.
226//
227// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-project-issues
228func (s *IssuesService) ListProjectIssues(pid interface{}, opt *ListProjectIssuesOptions, options ...OptionFunc) ([]*Issue, *Response, error) {
229 project, err := parseID(pid)
230 if err != nil {
231 return nil, nil, err
232 }
233 u := fmt.Sprintf("projects/%s/issues", pathEscape(project))
234
235 req, err := s.client.NewRequest("GET", u, opt, options)
236 if err != nil {
237 return nil, nil, err
238 }
239
240 var i []*Issue
241 resp, err := s.client.Do(req, &i)
242 if err != nil {
243 return nil, resp, err
244 }
245
246 return i, resp, err
247}
248
249// GetIssue gets a single project issue.
250//
251// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#single-issues
252func (s *IssuesService) GetIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
253 project, err := parseID(pid)
254 if err != nil {
255 return nil, nil, err
256 }
257 u := fmt.Sprintf("projects/%s/issues/%d", pathEscape(project), issue)
258
259 req, err := s.client.NewRequest("GET", u, nil, options)
260 if err != nil {
261 return nil, nil, err
262 }
263
264 i := new(Issue)
265 resp, err := s.client.Do(req, i)
266 if err != nil {
267 return nil, resp, err
268 }
269
270 return i, resp, err
271}
272
273// CreateIssueOptions represents the available CreateIssue() options.
274//
275// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#new-issues
276type CreateIssueOptions struct {
277 IID *int `url:"iid,omitempty" json:"iid,omitempty"`
278 Title *string `url:"title,omitempty" json:"title,omitempty"`
279 Description *string `url:"description,omitempty" json:"description,omitempty"`
280 Confidential *bool `url:"confidential,omitempty" json:"confidential,omitempty"`
281 AssigneeIDs []int `url:"assignee_ids,omitempty" json:"assignee_ids,omitempty"`
282 MilestoneID *int `url:"milestone_id,omitempty" json:"milestone_id,omitempty"`
283 Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
284 CreatedAt *time.Time `url:"created_at,omitempty" json:"created_at,omitempty"`
285 DueDate *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
286 MergeRequestToResolveDiscussionsOf *int `url:"merge_request_to_resolve_discussions_of,omitempty" json:"merge_request_to_resolve_discussions_of,omitempty"`
287 DiscussionToResolve *string `url:"discussion_to_resolve,omitempty" json:"discussion_to_resolve,omitempty"`
288 Weight *int `url:"weight,omitempty" json:"weight,omitempty"`
289}
290
291// CreateIssue creates a new project issue.
292//
293// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#new-issues
294func (s *IssuesService) CreateIssue(pid interface{}, opt *CreateIssueOptions, options ...OptionFunc) (*Issue, *Response, error) {
295 project, err := parseID(pid)
296 if err != nil {
297 return nil, nil, err
298 }
299 u := fmt.Sprintf("projects/%s/issues", pathEscape(project))
300
301 req, err := s.client.NewRequest("POST", u, opt, options)
302 if err != nil {
303 return nil, nil, err
304 }
305
306 i := new(Issue)
307 resp, err := s.client.Do(req, i)
308 if err != nil {
309 return nil, resp, err
310 }
311
312 return i, resp, err
313}
314
315// UpdateIssueOptions represents the available UpdateIssue() options.
316//
317// GitLab API docs: https://docs.gitlab.com/ee/api/issues.html#edit-issue
318type UpdateIssueOptions struct {
319 Title *string `url:"title,omitempty" json:"title,omitempty"`
320 Description *string `url:"description,omitempty" json:"description,omitempty"`
321 Confidential *bool `url:"confidential,omitempty" json:"confidential,omitempty"`
322 AssigneeIDs []int `url:"assignee_ids,omitempty" json:"assignee_ids,omitempty"`
323 MilestoneID *int `url:"milestone_id,omitempty" json:"milestone_id,omitempty"`
324 Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
325 StateEvent *string `url:"state_event,omitempty" json:"state_event,omitempty"`
326 UpdatedAt *time.Time `url:"updated_at,omitempty" json:"updated_at,omitempty"`
327 DueDate *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
328 Weight *int `url:"weight,omitempty" json:"weight,omitempty"`
329 DiscussionLocked *bool `url:"discussion_locked,omitempty" json:"discussion_locked,omitempty"`
330}
331
332// UpdateIssue updates an existing project issue. This function is also used
333// to mark an issue as closed.
334//
335// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#edit-issues
336func (s *IssuesService) UpdateIssue(pid interface{}, issue int, opt *UpdateIssueOptions, options ...OptionFunc) (*Issue, *Response, error) {
337 project, err := parseID(pid)
338 if err != nil {
339 return nil, nil, err
340 }
341 u := fmt.Sprintf("projects/%s/issues/%d", pathEscape(project), issue)
342
343 req, err := s.client.NewRequest("PUT", u, opt, options)
344 if err != nil {
345 return nil, nil, err
346 }
347
348 i := new(Issue)
349 resp, err := s.client.Do(req, i)
350 if err != nil {
351 return nil, resp, err
352 }
353
354 return i, resp, err
355}
356
357// DeleteIssue deletes a single project issue.
358//
359// GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#delete-an-issue
360func (s *IssuesService) DeleteIssue(pid interface{}, issue int, options ...OptionFunc) (*Response, error) {
361 project, err := parseID(pid)
362 if err != nil {
363 return nil, err
364 }
365 u := fmt.Sprintf("projects/%s/issues/%d", pathEscape(project), issue)
366
367 req, err := s.client.NewRequest("DELETE", u, nil, options)
368 if err != nil {
369 return nil, err
370 }
371
372 return s.client.Do(req, nil)
373}
374
375// SubscribeToIssue subscribes the authenticated user to the given issue to
376// receive notifications. If the user is already subscribed to the issue, the
377// status code 304 is returned.
378//
379// GitLab API docs:
380// https://docs.gitlab.com/ce/api/merge_requests.html#subscribe-to-a-merge-request
381func (s *IssuesService) SubscribeToIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
382 project, err := parseID(pid)
383 if err != nil {
384 return nil, nil, err
385 }
386 u := fmt.Sprintf("projects/%s/issues/%d/subscribe", pathEscape(project), issue)
387
388 req, err := s.client.NewRequest("POST", u, nil, options)
389 if err != nil {
390 return nil, nil, err
391 }
392
393 i := new(Issue)
394 resp, err := s.client.Do(req, i)
395 if err != nil {
396 return nil, resp, err
397 }
398
399 return i, resp, err
400}
401
402// UnsubscribeFromIssue unsubscribes the authenticated user from the given
403// issue to not receive notifications from that merge request. If the user
404// is not subscribed to the issue, status code 304 is returned.
405//
406// GitLab API docs:
407// https://docs.gitlab.com/ce/api/merge_requests.html#unsubscribe-from-a-merge-request
408func (s *IssuesService) UnsubscribeFromIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
409 project, err := parseID(pid)
410 if err != nil {
411 return nil, nil, err
412 }
413 u := fmt.Sprintf("projects/%s/issues/%d/unsubscribe", pathEscape(project), issue)
414
415 req, err := s.client.NewRequest("POST", u, nil, options)
416 if err != nil {
417 return nil, nil, err
418 }
419
420 i := new(Issue)
421 resp, err := s.client.Do(req, i)
422 if err != nil {
423 return nil, resp, err
424 }
425
426 return i, resp, err
427}
428
429// ListMergeRequestsClosingIssueOptions represents the available
430// ListMergeRequestsClosingIssue() options.
431//
432// GitLab API docs:
433// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-that-will-close-issue-on-merge
434type ListMergeRequestsClosingIssueOptions ListOptions
435
436// ListMergeRequestsClosingIssue gets all the merge requests that will close
437// issue when merged.
438//
439// GitLab API docs:
440// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-that-will-close-issue-on-merge
441func (s *IssuesService) ListMergeRequestsClosingIssue(pid interface{}, issue int, opt *ListMergeRequestsClosingIssueOptions, options ...OptionFunc) ([]*MergeRequest, *Response, error) {
442 project, err := parseID(pid)
443 if err != nil {
444 return nil, nil, err
445 }
446 u := fmt.Sprintf("/projects/%s/issues/%d/closed_by", pathEscape(project), issue)
447
448 req, err := s.client.NewRequest("GET", u, opt, options)
449 if err != nil {
450 return nil, nil, err
451 }
452
453 var m []*MergeRequest
454 resp, err := s.client.Do(req, &m)
455 if err != nil {
456 return nil, resp, err
457 }
458
459 return m, resp, err
460}
461
462// ListMergeRequestsRelatedToIssueOptions represents the available
463// ListMergeRequestsRelatedToIssue() options.
464//
465// GitLab API docs:
466// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-related-to-issue
467type ListMergeRequestsRelatedToIssueOptions ListOptions
468
469// ListMergeRequestsRelatedToIssue gets all the merge requests that are
470// related to the issue
471//
472// GitLab API docs:
473// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-related-to-issue
474func (s *IssuesService) ListMergeRequestsRelatedToIssue(pid interface{}, issue int, opt *ListMergeRequestsRelatedToIssueOptions, options ...OptionFunc) ([]*MergeRequest, *Response, error) {
475 project, err := parseID(pid)
476 if err != nil {
477 return nil, nil, err
478 }
479 u := fmt.Sprintf("/projects/%s/issues/%d/related_merge_requests",
480 pathEscape(project),
481 issue,
482 )
483
484 req, err := s.client.NewRequest("GET", u, opt, options)
485 if err != nil {
486 return nil, nil, err
487 }
488
489 var m []*MergeRequest
490 resp, err := s.client.Do(req, &m)
491 if err != nil {
492 return nil, resp, err
493 }
494
495 return m, resp, err
496}
497
498// SetTimeEstimate sets the time estimate for a single project issue.
499//
500// GitLab API docs:
501// https://docs.gitlab.com/ce/api/issues.html#set-a-time-estimate-for-an-issue
502func (s *IssuesService) SetTimeEstimate(pid interface{}, issue int, opt *SetTimeEstimateOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
503 return s.timeStats.setTimeEstimate(pid, "issues", issue, opt, options...)
504}
505
506// ResetTimeEstimate resets the time estimate for a single project issue.
507//
508// GitLab API docs:
509// https://docs.gitlab.com/ce/api/issues.html#reset-the-time-estimate-for-an-issue
510func (s *IssuesService) ResetTimeEstimate(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
511 return s.timeStats.resetTimeEstimate(pid, "issues", issue, options...)
512}
513
514// AddSpentTime adds spent time for a single project issue.
515//
516// GitLab API docs:
517// https://docs.gitlab.com/ce/api/issues.html#add-spent-time-for-an-issue
518func (s *IssuesService) AddSpentTime(pid interface{}, issue int, opt *AddSpentTimeOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
519 return s.timeStats.addSpentTime(pid, "issues", issue, opt, options...)
520}
521
522// ResetSpentTime resets the spent time for a single project issue.
523//
524// GitLab API docs:
525// https://docs.gitlab.com/ce/api/issues.html#reset-spent-time-for-an-issue
526func (s *IssuesService) ResetSpentTime(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
527 return s.timeStats.resetSpentTime(pid, "issues", issue, options...)
528}
529
530// GetTimeSpent gets the spent time for a single project issue.
531//
532// GitLab API docs:
533// https://docs.gitlab.com/ce/api/issues.html#get-time-tracking-stats
534func (s *IssuesService) GetTimeSpent(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
535 return s.timeStats.getTimeSpent(pid, "issues", issue, options...)
536}