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 Labels `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// MoveIssueOptions represents the available MoveIssue() options.
376//
377// GitLab API docs: https://docs.gitlab.com/ee/api/issues.html#move-an-issue
378type MoveIssueOptions struct {
379 ToProjectID *int `url:"to_project_id,omitempty" json:"to_project_id,omitempty"`
380}
381
382// MoveIssue updates an existing project issue. This function is also used
383// to mark an issue as closed.
384//
385// GitLab API docs: https://docs.gitlab.com/ee/api/issues.html#move-an-issue
386func (s *IssuesService) MoveIssue(pid interface{}, issue int, opt *MoveIssueOptions, options ...OptionFunc) (*Issue, *Response, error) {
387 project, err := parseID(pid)
388 if err != nil {
389 return nil, nil, err
390 }
391 u := fmt.Sprintf("projects/%s/issues/%d/move", pathEscape(project), issue)
392
393 req, err := s.client.NewRequest("POST", u, opt, options)
394 if err != nil {
395 return nil, nil, err
396 }
397
398 i := new(Issue)
399 resp, err := s.client.Do(req, i)
400 if err != nil {
401 return nil, resp, err
402 }
403
404 return i, resp, err
405}
406
407// SubscribeToIssue subscribes the authenticated user to the given issue to
408// receive notifications. If the user is already subscribed to the issue, the
409// status code 304 is returned.
410//
411// GitLab API docs:
412// https://docs.gitlab.com/ce/api/merge_requests.html#subscribe-to-a-merge-request
413func (s *IssuesService) SubscribeToIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
414 project, err := parseID(pid)
415 if err != nil {
416 return nil, nil, err
417 }
418 u := fmt.Sprintf("projects/%s/issues/%d/subscribe", pathEscape(project), issue)
419
420 req, err := s.client.NewRequest("POST", u, nil, options)
421 if err != nil {
422 return nil, nil, err
423 }
424
425 i := new(Issue)
426 resp, err := s.client.Do(req, i)
427 if err != nil {
428 return nil, resp, err
429 }
430
431 return i, resp, err
432}
433
434// UnsubscribeFromIssue unsubscribes the authenticated user from the given
435// issue to not receive notifications from that merge request. If the user
436// is not subscribed to the issue, status code 304 is returned.
437//
438// GitLab API docs:
439// https://docs.gitlab.com/ce/api/merge_requests.html#unsubscribe-from-a-merge-request
440func (s *IssuesService) UnsubscribeFromIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
441 project, err := parseID(pid)
442 if err != nil {
443 return nil, nil, err
444 }
445 u := fmt.Sprintf("projects/%s/issues/%d/unsubscribe", pathEscape(project), issue)
446
447 req, err := s.client.NewRequest("POST", u, nil, options)
448 if err != nil {
449 return nil, nil, err
450 }
451
452 i := new(Issue)
453 resp, err := s.client.Do(req, i)
454 if err != nil {
455 return nil, resp, err
456 }
457
458 return i, resp, err
459}
460
461// ListMergeRequestsClosingIssueOptions represents the available
462// ListMergeRequestsClosingIssue() options.
463//
464// GitLab API docs:
465// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-that-will-close-issue-on-merge
466type ListMergeRequestsClosingIssueOptions ListOptions
467
468// ListMergeRequestsClosingIssue gets all the merge requests that will close
469// issue when merged.
470//
471// GitLab API docs:
472// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-that-will-close-issue-on-merge
473func (s *IssuesService) ListMergeRequestsClosingIssue(pid interface{}, issue int, opt *ListMergeRequestsClosingIssueOptions, options ...OptionFunc) ([]*MergeRequest, *Response, error) {
474 project, err := parseID(pid)
475 if err != nil {
476 return nil, nil, err
477 }
478 u := fmt.Sprintf("/projects/%s/issues/%d/closed_by", pathEscape(project), issue)
479
480 req, err := s.client.NewRequest("GET", u, opt, options)
481 if err != nil {
482 return nil, nil, err
483 }
484
485 var m []*MergeRequest
486 resp, err := s.client.Do(req, &m)
487 if err != nil {
488 return nil, resp, err
489 }
490
491 return m, resp, err
492}
493
494// ListMergeRequestsRelatedToIssueOptions represents the available
495// ListMergeRequestsRelatedToIssue() options.
496//
497// GitLab API docs:
498// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-related-to-issue
499type ListMergeRequestsRelatedToIssueOptions ListOptions
500
501// ListMergeRequestsRelatedToIssue gets all the merge requests that are
502// related to the issue
503//
504// GitLab API docs:
505// https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-related-to-issue
506func (s *IssuesService) ListMergeRequestsRelatedToIssue(pid interface{}, issue int, opt *ListMergeRequestsRelatedToIssueOptions, options ...OptionFunc) ([]*MergeRequest, *Response, error) {
507 project, err := parseID(pid)
508 if err != nil {
509 return nil, nil, err
510 }
511 u := fmt.Sprintf("/projects/%s/issues/%d/related_merge_requests",
512 pathEscape(project),
513 issue,
514 )
515
516 req, err := s.client.NewRequest("GET", u, opt, options)
517 if err != nil {
518 return nil, nil, err
519 }
520
521 var m []*MergeRequest
522 resp, err := s.client.Do(req, &m)
523 if err != nil {
524 return nil, resp, err
525 }
526
527 return m, resp, err
528}
529
530// SetTimeEstimate sets the time estimate for a single project issue.
531//
532// GitLab API docs:
533// https://docs.gitlab.com/ce/api/issues.html#set-a-time-estimate-for-an-issue
534func (s *IssuesService) SetTimeEstimate(pid interface{}, issue int, opt *SetTimeEstimateOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
535 return s.timeStats.setTimeEstimate(pid, "issues", issue, opt, options...)
536}
537
538// ResetTimeEstimate resets the time estimate for a single project issue.
539//
540// GitLab API docs:
541// https://docs.gitlab.com/ce/api/issues.html#reset-the-time-estimate-for-an-issue
542func (s *IssuesService) ResetTimeEstimate(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
543 return s.timeStats.resetTimeEstimate(pid, "issues", issue, options...)
544}
545
546// AddSpentTime adds spent time for a single project issue.
547//
548// GitLab API docs:
549// https://docs.gitlab.com/ce/api/issues.html#add-spent-time-for-an-issue
550func (s *IssuesService) AddSpentTime(pid interface{}, issue int, opt *AddSpentTimeOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
551 return s.timeStats.addSpentTime(pid, "issues", issue, opt, options...)
552}
553
554// ResetSpentTime resets the spent time for a single project issue.
555//
556// GitLab API docs:
557// https://docs.gitlab.com/ce/api/issues.html#reset-spent-time-for-an-issue
558func (s *IssuesService) ResetSpentTime(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
559 return s.timeStats.resetSpentTime(pid, "issues", issue, options...)
560}
561
562// GetTimeSpent gets the spent time for a single project issue.
563//
564// GitLab API docs:
565// https://docs.gitlab.com/ce/api/issues.html#get-time-tracking-stats
566func (s *IssuesService) GetTimeSpent(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
567 return s.timeStats.getTimeSpent(pid, "issues", issue, options...)
568}