issues.go 19 KB

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