issues.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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. package gitlab
  17. import (
  18. "encoding/json"
  19. "fmt"
  20. "net/url"
  21. "strings"
  22. "time"
  23. )
  24. // IssuesService handles communication with the issue related methods
  25. // of the GitLab API.
  26. //
  27. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html
  28. type IssuesService struct {
  29. client *Client
  30. timeStats *timeStatsService
  31. }
  32. // IssueAuthor represents a author of the issue.
  33. type IssueAuthor struct {
  34. ID int `json:"id"`
  35. State string `json:"state"`
  36. WebURL string `json:"web_url"`
  37. Name string `json:"name"`
  38. AvatarURL string `json:"avatar_url"`
  39. Username string `json:"username"`
  40. }
  41. // IssueAssignee represents a assignee of the issue.
  42. type IssueAssignee struct {
  43. ID int `json:"id"`
  44. State string `json:"state"`
  45. WebURL string `json:"web_url"`
  46. Name string `json:"name"`
  47. AvatarURL string `json:"avatar_url"`
  48. Username string `json:"username"`
  49. }
  50. // IssueLinks represents links of the issue.
  51. type IssueLinks struct {
  52. Self string `json:"self"`
  53. Notes string `json:"notes"`
  54. AwardEmoji string `json:"award_emoji"`
  55. Project string `json:"project"`
  56. }
  57. // Issue represents a GitLab issue.
  58. //
  59. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html
  60. type Issue struct {
  61. ID int `json:"id"`
  62. IID int `json:"iid"`
  63. ProjectID int `json:"project_id"`
  64. Milestone *Milestone `json:"milestone"`
  65. Author *IssueAuthor `json:"author"`
  66. Description string `json:"description"`
  67. State string `json:"state"`
  68. Assignees []*IssueAssignee `json:"assignees"`
  69. Assignee *IssueAssignee `json:"assignee"`
  70. Upvotes int `json:"upvotes"`
  71. Downvotes int `json:"downvotes"`
  72. Labels []string `json:"labels"`
  73. Title string `json:"title"`
  74. UpdatedAt *time.Time `json:"updated_at"`
  75. CreatedAt *time.Time `json:"created_at"`
  76. ClosedAt *time.Time `json:"closed_at"`
  77. Subscribed bool `json:"subscribed"`
  78. UserNotesCount int `json:"user_notes_count"`
  79. DueDate *ISOTime `json:"due_date"`
  80. WebURL string `json:"web_url"`
  81. TimeStats *TimeStats `json:"time_stats"`
  82. Confidential bool `json:"confidential"`
  83. Weight int `json:"weight"`
  84. DiscussionLocked bool `json:"discussion_locked"`
  85. Links *IssueLinks `json:"_links"`
  86. IssueLinkID int `json:"issue_link_id"`
  87. }
  88. func (i Issue) String() string {
  89. return Stringify(i)
  90. }
  91. // Labels is a custom type with specific marshaling characteristics.
  92. type Labels []string
  93. // MarshalJSON implements the json.Marshaler interface.
  94. func (l *Labels) MarshalJSON() ([]byte, error) {
  95. return json.Marshal(strings.Join(*l, ","))
  96. }
  97. // ListIssuesOptions represents the available ListIssues() options.
  98. //
  99. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-issues
  100. type ListIssuesOptions struct {
  101. ListOptions
  102. State *string `url:"state,omitempty" json:"state,omitempty"`
  103. Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
  104. Milestone *string `url:"milestone,omitempty" json:"milestone,omitempty"`
  105. Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
  106. AuthorID *int `url:"author_id,omitempty" json:"author_id,omitempty"`
  107. AssigneeID *int `url:"assignee_id,omitempty" json:"assignee_id,omitempty"`
  108. MyReactionEmoji *string `url:"my_reaction_emoji,omitempty" json:"my_reaction_emoji,omitempty"`
  109. IIDs []int `url:"iids[],omitempty" json:"iids,omitempty"`
  110. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  111. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  112. Search *string `url:"search,omitempty" json:"search,omitempty"`
  113. CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
  114. CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
  115. UpdatedAfter *time.Time `url:"updated_after,omitempty" json:"updated_after,omitempty"`
  116. UpdatedBefore *time.Time `url:"updated_before,omitempty" json:"updated_before,omitempty"`
  117. }
  118. // ListIssues gets all issues created by authenticated user. This function
  119. // takes pagination parameters page and per_page to restrict the list of issues.
  120. //
  121. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-issues
  122. func (s *IssuesService) ListIssues(opt *ListIssuesOptions, options ...OptionFunc) ([]*Issue, *Response, error) {
  123. req, err := s.client.NewRequest("GET", "issues", opt, options)
  124. if err != nil {
  125. return nil, nil, err
  126. }
  127. var i []*Issue
  128. resp, err := s.client.Do(req, &i)
  129. if err != nil {
  130. return nil, resp, err
  131. }
  132. return i, resp, err
  133. }
  134. // ListGroupIssuesOptions represents the available ListGroupIssues() options.
  135. //
  136. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-group-issues
  137. type ListGroupIssuesOptions struct {
  138. ListOptions
  139. State *string `url:"state,omitempty" json:"state,omitempty"`
  140. Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
  141. IIDs []int `url:"iids[],omitempty" json:"iids,omitempty"`
  142. Milestone *string `url:"milestone,omitempty" json:"milestone,omitempty"`
  143. Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
  144. AuthorID *int `url:"author_id,omitempty" json:"author_id,omitempty"`
  145. AssigneeID *int `url:"assignee_id,omitempty" json:"assignee_id,omitempty"`
  146. MyReactionEmoji *string `url:"my_reaction_emoji,omitempty" json:"my_reaction_emoji,omitempty"`
  147. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  148. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  149. Search *string `url:"search,omitempty" json:"search,omitempty"`
  150. CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
  151. CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
  152. UpdatedAfter *time.Time `url:"updated_after,omitempty" json:"updated_after,omitempty"`
  153. UpdatedBefore *time.Time `url:"updated_before,omitempty" json:"updated_before,omitempty"`
  154. }
  155. // ListGroupIssues gets a list of group issues. This function accepts
  156. // pagination parameters page and per_page to return the list of group issues.
  157. //
  158. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-group-issues
  159. func (s *IssuesService) ListGroupIssues(pid interface{}, opt *ListGroupIssuesOptions, options ...OptionFunc) ([]*Issue, *Response, error) {
  160. group, err := parseID(pid)
  161. if err != nil {
  162. return nil, nil, err
  163. }
  164. u := fmt.Sprintf("groups/%s/issues", url.QueryEscape(group))
  165. req, err := s.client.NewRequest("GET", u, opt, options)
  166. if err != nil {
  167. return nil, nil, err
  168. }
  169. var i []*Issue
  170. resp, err := s.client.Do(req, &i)
  171. if err != nil {
  172. return nil, resp, err
  173. }
  174. return i, resp, err
  175. }
  176. // ListProjectIssuesOptions represents the available ListProjectIssues() options.
  177. //
  178. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-project-issues
  179. type ListProjectIssuesOptions struct {
  180. ListOptions
  181. IIDs []int `url:"iids[],omitempty" json:"iids,omitempty"`
  182. State *string `url:"state,omitempty" json:"state,omitempty"`
  183. Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
  184. Milestone *string `url:"milestone,omitempty" json:"milestone,omitempty"`
  185. Scope *string `url:"scope,omitempty" json:"scope,omitempty"`
  186. AuthorID *int `url:"author_id,omitempty" json:"author_id,omitempty"`
  187. AssigneeID *int `url:"assignee_id,omitempty" json:"assignee_id,omitempty"`
  188. MyReactionEmoji *string `url:"my_reaction_emoji,omitempty" json:"my_reaction_emoji,omitempty"`
  189. OrderBy *string `url:"order_by,omitempty" json:"order_by,omitempty"`
  190. Sort *string `url:"sort,omitempty" json:"sort,omitempty"`
  191. Search *string `url:"search,omitempty" json:"search,omitempty"`
  192. CreatedAfter *time.Time `url:"created_after,omitempty" json:"created_after,omitempty"`
  193. CreatedBefore *time.Time `url:"created_before,omitempty" json:"created_before,omitempty"`
  194. UpdatedAfter *time.Time `url:"updated_after,omitempty" json:"updated_after,omitempty"`
  195. UpdatedBefore *time.Time `url:"updated_before,omitempty" json:"updated_before,omitempty"`
  196. }
  197. // ListProjectIssues gets a list of project issues. This function accepts
  198. // pagination parameters page and per_page to return the list of project issues.
  199. //
  200. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#list-project-issues
  201. func (s *IssuesService) ListProjectIssues(pid interface{}, opt *ListProjectIssuesOptions, options ...OptionFunc) ([]*Issue, *Response, error) {
  202. project, err := parseID(pid)
  203. if err != nil {
  204. return nil, nil, err
  205. }
  206. u := fmt.Sprintf("projects/%s/issues", url.QueryEscape(project))
  207. req, err := s.client.NewRequest("GET", u, opt, options)
  208. if err != nil {
  209. return nil, nil, err
  210. }
  211. var i []*Issue
  212. resp, err := s.client.Do(req, &i)
  213. if err != nil {
  214. return nil, resp, err
  215. }
  216. return i, resp, err
  217. }
  218. // GetIssue gets a single project issue.
  219. //
  220. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#single-issues
  221. func (s *IssuesService) GetIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
  222. project, err := parseID(pid)
  223. if err != nil {
  224. return nil, nil, err
  225. }
  226. u := fmt.Sprintf("projects/%s/issues/%d", url.QueryEscape(project), issue)
  227. req, err := s.client.NewRequest("GET", u, nil, options)
  228. if err != nil {
  229. return nil, nil, err
  230. }
  231. i := new(Issue)
  232. resp, err := s.client.Do(req, i)
  233. if err != nil {
  234. return nil, resp, err
  235. }
  236. return i, resp, err
  237. }
  238. // CreateIssueOptions represents the available CreateIssue() options.
  239. //
  240. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#new-issues
  241. type CreateIssueOptions struct {
  242. Title *string `url:"title,omitempty" json:"title,omitempty"`
  243. Description *string `url:"description,omitempty" json:"description,omitempty"`
  244. Confidential *bool `url:"confidential,omitempty" json:"confidential,omitempty"`
  245. AssigneeIDs []int `url:"assignee_ids,omitempty" json:"assignee_ids,omitempty"`
  246. MilestoneID *int `url:"milestone_id,omitempty" json:"milestone_id,omitempty"`
  247. Labels Labels `url:"labels,comma,omitempty" json:"labels,omitempty"`
  248. CreatedAt *time.Time `url:"created_at,omitempty" json:"created_at,omitempty"`
  249. DueDate *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
  250. MergeRequestToResolveDiscussionsOf *int `url:"merge_request_to_resolve_discussions_of,omitempty" json:"merge_request_to_resolve_discussions_of,omitempty"`
  251. DiscussionToResolve *string `url:"discussion_to_resolve,omitempty" json:"discussion_to_resolve,omitempty"`
  252. Weight *int `url:"weight,omitempty" json:"weight,omitempty"`
  253. }
  254. // CreateIssue creates a new project issue.
  255. //
  256. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#new-issues
  257. func (s *IssuesService) CreateIssue(pid interface{}, opt *CreateIssueOptions, options ...OptionFunc) (*Issue, *Response, error) {
  258. project, err := parseID(pid)
  259. if err != nil {
  260. return nil, nil, err
  261. }
  262. u := fmt.Sprintf("projects/%s/issues", url.QueryEscape(project))
  263. req, err := s.client.NewRequest("POST", u, opt, options)
  264. if err != nil {
  265. return nil, nil, err
  266. }
  267. i := new(Issue)
  268. resp, err := s.client.Do(req, i)
  269. if err != nil {
  270. return nil, resp, err
  271. }
  272. return i, resp, err
  273. }
  274. // UpdateIssueOptions represents the available UpdateIssue() options.
  275. //
  276. // GitLab API docs: https://docs.gitlab.com/ee/api/issues.html#edit-issue
  277. type UpdateIssueOptions struct {
  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. StateEvent *string `url:"state_event,omitempty" json:"state_event,omitempty"`
  285. UpdatedAt *time.Time `url:"updated_at,omitempty" json:"updated_at,omitempty"`
  286. DueDate *ISOTime `url:"due_date,omitempty" json:"due_date,omitempty"`
  287. Weight *int `url:"weight,omitempty" json:"weight,omitempty"`
  288. DiscussionLocked *bool `url:"discussion_locked,omitempty" json:"discussion_locked,omitempty"`
  289. }
  290. // UpdateIssue updates an existing project issue. This function is also used
  291. // to mark an issue as closed.
  292. //
  293. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#edit-issues
  294. func (s *IssuesService) UpdateIssue(pid interface{}, issue int, opt *UpdateIssueOptions, 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/%d", url.QueryEscape(project), issue)
  300. req, err := s.client.NewRequest("PUT", u, opt, options)
  301. if err != nil {
  302. return nil, nil, err
  303. }
  304. i := new(Issue)
  305. resp, err := s.client.Do(req, i)
  306. if err != nil {
  307. return nil, resp, err
  308. }
  309. return i, resp, err
  310. }
  311. // DeleteIssue deletes a single project issue.
  312. //
  313. // GitLab API docs: https://docs.gitlab.com/ce/api/issues.html#delete-an-issue
  314. func (s *IssuesService) DeleteIssue(pid interface{}, issue int, options ...OptionFunc) (*Response, error) {
  315. project, err := parseID(pid)
  316. if err != nil {
  317. return nil, err
  318. }
  319. u := fmt.Sprintf("projects/%s/issues/%d", url.QueryEscape(project), issue)
  320. req, err := s.client.NewRequest("DELETE", u, nil, options)
  321. if err != nil {
  322. return nil, err
  323. }
  324. return s.client.Do(req, nil)
  325. }
  326. // SubscribeToIssue subscribes the authenticated user to the given issue to
  327. // receive notifications. If the user is already subscribed to the issue, the
  328. // status code 304 is returned.
  329. //
  330. // GitLab API docs:
  331. // https://docs.gitlab.com/ce/api/merge_requests.html#subscribe-to-a-merge-request
  332. func (s *IssuesService) SubscribeToIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
  333. project, err := parseID(pid)
  334. if err != nil {
  335. return nil, nil, err
  336. }
  337. u := fmt.Sprintf("projects/%s/issues/%d/subscribe", url.QueryEscape(project), issue)
  338. req, err := s.client.NewRequest("POST", u, nil, options)
  339. if err != nil {
  340. return nil, nil, err
  341. }
  342. i := new(Issue)
  343. resp, err := s.client.Do(req, i)
  344. if err != nil {
  345. return nil, resp, err
  346. }
  347. return i, resp, err
  348. }
  349. // UnsubscribeFromIssue unsubscribes the authenticated user from the given
  350. // issue to not receive notifications from that merge request. If the user
  351. // is not subscribed to the issue, status code 304 is returned.
  352. //
  353. // GitLab API docs:
  354. // https://docs.gitlab.com/ce/api/merge_requests.html#unsubscribe-from-a-merge-request
  355. func (s *IssuesService) UnsubscribeFromIssue(pid interface{}, issue int, options ...OptionFunc) (*Issue, *Response, error) {
  356. project, err := parseID(pid)
  357. if err != nil {
  358. return nil, nil, err
  359. }
  360. u := fmt.Sprintf("projects/%s/issues/%d/unsubscribe", url.QueryEscape(project), issue)
  361. req, err := s.client.NewRequest("POST", u, nil, options)
  362. if err != nil {
  363. return nil, nil, err
  364. }
  365. i := new(Issue)
  366. resp, err := s.client.Do(req, i)
  367. if err != nil {
  368. return nil, resp, err
  369. }
  370. return i, resp, err
  371. }
  372. // ListMergeRequestsClosingIssueOptions represents the available
  373. // ListMergeRequestsClosingIssue() options.
  374. //
  375. // GitLab API docs:
  376. // https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-that-will-close-issue-on-merge
  377. type ListMergeRequestsClosingIssueOptions ListOptions
  378. // ListMergeRequestsClosingIssue gets all the merge requests that will close
  379. // issue when merged.
  380. //
  381. // GitLab API docs:
  382. // https://docs.gitlab.com/ce/api/issues.html#list-merge-requests-that-will-close-issue-on-merge
  383. func (s *IssuesService) ListMergeRequestsClosingIssue(pid interface{}, issue int, opt *ListMergeRequestsClosingIssueOptions, options ...OptionFunc) ([]*MergeRequest, *Response, error) {
  384. project, err := parseID(pid)
  385. if err != nil {
  386. return nil, nil, err
  387. }
  388. u := fmt.Sprintf("/projects/%s/issues/%d/closed_by", url.QueryEscape(project), issue)
  389. req, err := s.client.NewRequest("GET", u, opt, options)
  390. if err != nil {
  391. return nil, nil, err
  392. }
  393. var m []*MergeRequest
  394. resp, err := s.client.Do(req, &m)
  395. if err != nil {
  396. return nil, resp, err
  397. }
  398. return m, resp, err
  399. }
  400. // SetTimeEstimate sets the time estimate for a single project issue.
  401. //
  402. // GitLab API docs:
  403. // https://docs.gitlab.com/ce/api/issues.html#set-a-time-estimate-for-an-issue
  404. func (s *IssuesService) SetTimeEstimate(pid interface{}, issue int, opt *SetTimeEstimateOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
  405. return s.timeStats.setTimeEstimate(pid, "issues", issue, opt, options...)
  406. }
  407. // ResetTimeEstimate resets the time estimate for a single project issue.
  408. //
  409. // GitLab API docs:
  410. // https://docs.gitlab.com/ce/api/issues.html#reset-the-time-estimate-for-an-issue
  411. func (s *IssuesService) ResetTimeEstimate(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
  412. return s.timeStats.resetTimeEstimate(pid, "issues", issue, options...)
  413. }
  414. // AddSpentTime adds spent time for a single project issue.
  415. //
  416. // GitLab API docs:
  417. // https://docs.gitlab.com/ce/api/issues.html#add-spent-time-for-an-issue
  418. func (s *IssuesService) AddSpentTime(pid interface{}, issue int, opt *AddSpentTimeOptions, options ...OptionFunc) (*TimeStats, *Response, error) {
  419. return s.timeStats.addSpentTime(pid, "issues", issue, opt, options...)
  420. }
  421. // ResetSpentTime resets the spent time for a single project issue.
  422. //
  423. // GitLab API docs:
  424. // https://docs.gitlab.com/ce/api/issues.html#reset-spent-time-for-an-issue
  425. func (s *IssuesService) ResetSpentTime(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
  426. return s.timeStats.resetSpentTime(pid, "issues", issue, options...)
  427. }
  428. // GetTimeSpent gets the spent time for a single project issue.
  429. //
  430. // GitLab API docs:
  431. // https://docs.gitlab.com/ce/api/issues.html#get-time-tracking-stats
  432. func (s *IssuesService) GetTimeSpent(pid interface{}, issue int, options ...OptionFunc) (*TimeStats, *Response, error) {
  433. return s.timeStats.getTimeSpent(pid, "issues", issue, options...)
  434. }