issues.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. // Copyright 2013 The go-github AUTHORS. All rights reserved.
  2. //
  3. // Use of this source code is governed by a BSD-style
  4. // license that can be found in the LICENSE file.
  5. package github
  6. import (
  7. "context"
  8. "fmt"
  9. "strings"
  10. "time"
  11. )
  12. // IssuesService handles communication with the issue related
  13. // methods of the GitHub API.
  14. //
  15. // GitHub API docs: https://developer.github.com/v3/issues/
  16. type IssuesService service
  17. // Issue represents a GitHub issue on a repository.
  18. //
  19. // Note: As far as the GitHub API is concerned, every pull request is an issue,
  20. // but not every issue is a pull request. Some endpoints, events, and webhooks
  21. // may also return pull requests via this struct. If PullRequestLinks is nil,
  22. // this is an issue, and if PullRequestLinks is not nil, this is a pull request.
  23. // The IsPullRequest helper method can be used to check that.
  24. type Issue struct {
  25. ID *int64 `json:"id,omitempty"`
  26. Number *int `json:"number,omitempty"`
  27. State *string `json:"state,omitempty"`
  28. Locked *bool `json:"locked,omitempty"`
  29. Title *string `json:"title,omitempty"`
  30. Body *string `json:"body,omitempty"`
  31. User *User `json:"user,omitempty"`
  32. Labels []Label `json:"labels,omitempty"`
  33. Assignee *User `json:"assignee,omitempty"`
  34. Comments *int `json:"comments,omitempty"`
  35. ClosedAt *time.Time `json:"closed_at,omitempty"`
  36. CreatedAt *time.Time `json:"created_at,omitempty"`
  37. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  38. ClosedBy *User `json:"closed_by,omitempty"`
  39. URL *string `json:"url,omitempty"`
  40. HTMLURL *string `json:"html_url,omitempty"`
  41. CommentsURL *string `json:"comments_url,omitempty"`
  42. EventsURL *string `json:"events_url,omitempty"`
  43. LabelsURL *string `json:"labels_url,omitempty"`
  44. RepositoryURL *string `json:"repository_url,omitempty"`
  45. Milestone *Milestone `json:"milestone,omitempty"`
  46. PullRequestLinks *PullRequestLinks `json:"pull_request,omitempty"`
  47. Repository *Repository `json:"repository,omitempty"`
  48. Reactions *Reactions `json:"reactions,omitempty"`
  49. Assignees []*User `json:"assignees,omitempty"`
  50. NodeID *string `json:"node_id,omitempty"`
  51. // TextMatches is only populated from search results that request text matches
  52. // See: search.go and https://developer.github.com/v3/search/#text-match-metadata
  53. TextMatches []TextMatch `json:"text_matches,omitempty"`
  54. }
  55. func (i Issue) String() string {
  56. return Stringify(i)
  57. }
  58. // IsPullRequest reports whether the issue is also a pull request. It uses the
  59. // method recommended by GitHub's API documentation, which is to check whether
  60. // PullRequestLinks is non-nil.
  61. func (i Issue) IsPullRequest() bool {
  62. return i.PullRequestLinks != nil
  63. }
  64. // IssueRequest represents a request to create/edit an issue.
  65. // It is separate from Issue above because otherwise Labels
  66. // and Assignee fail to serialize to the correct JSON.
  67. type IssueRequest struct {
  68. Title *string `json:"title,omitempty"`
  69. Body *string `json:"body,omitempty"`
  70. Labels *[]string `json:"labels,omitempty"`
  71. Assignee *string `json:"assignee,omitempty"`
  72. State *string `json:"state,omitempty"`
  73. Milestone *int `json:"milestone,omitempty"`
  74. Assignees *[]string `json:"assignees,omitempty"`
  75. }
  76. // IssueListOptions specifies the optional parameters to the IssuesService.List
  77. // and IssuesService.ListByOrg methods.
  78. type IssueListOptions struct {
  79. // Filter specifies which issues to list. Possible values are: assigned,
  80. // created, mentioned, subscribed, all. Default is "assigned".
  81. Filter string `url:"filter,omitempty"`
  82. // State filters issues based on their state. Possible values are: open,
  83. // closed, all. Default is "open".
  84. State string `url:"state,omitempty"`
  85. // Labels filters issues based on their label.
  86. Labels []string `url:"labels,comma,omitempty"`
  87. // Sort specifies how to sort issues. Possible values are: created, updated,
  88. // and comments. Default value is "created".
  89. Sort string `url:"sort,omitempty"`
  90. // Direction in which to sort issues. Possible values are: asc, desc.
  91. // Default is "desc".
  92. Direction string `url:"direction,omitempty"`
  93. // Since filters issues by time.
  94. Since time.Time `url:"since,omitempty"`
  95. ListOptions
  96. }
  97. // PullRequestLinks object is added to the Issue object when it's an issue included
  98. // in the IssueCommentEvent webhook payload, if the webhook is fired by a comment on a PR.
  99. type PullRequestLinks struct {
  100. URL *string `json:"url,omitempty"`
  101. HTMLURL *string `json:"html_url,omitempty"`
  102. DiffURL *string `json:"diff_url,omitempty"`
  103. PatchURL *string `json:"patch_url,omitempty"`
  104. }
  105. // List the issues for the authenticated user. If all is true, list issues
  106. // across all the user's visible repositories including owned, member, and
  107. // organization repositories; if false, list only owned and member
  108. // repositories.
  109. //
  110. // GitHub API docs: https://developer.github.com/v3/issues/#list-issues
  111. func (s *IssuesService) List(ctx context.Context, all bool, opt *IssueListOptions) ([]*Issue, *Response, error) {
  112. var u string
  113. if all {
  114. u = "issues"
  115. } else {
  116. u = "user/issues"
  117. }
  118. return s.listIssues(ctx, u, opt)
  119. }
  120. // ListByOrg fetches the issues in the specified organization for the
  121. // authenticated user.
  122. //
  123. // GitHub API docs: https://developer.github.com/v3/issues/#list-issues
  124. func (s *IssuesService) ListByOrg(ctx context.Context, org string, opt *IssueListOptions) ([]*Issue, *Response, error) {
  125. u := fmt.Sprintf("orgs/%v/issues", org)
  126. return s.listIssues(ctx, u, opt)
  127. }
  128. func (s *IssuesService) listIssues(ctx context.Context, u string, opt *IssueListOptions) ([]*Issue, *Response, error) {
  129. u, err := addOptions(u, opt)
  130. if err != nil {
  131. return nil, nil, err
  132. }
  133. req, err := s.client.NewRequest("GET", u, nil)
  134. if err != nil {
  135. return nil, nil, err
  136. }
  137. // TODO: remove custom Accept headers when APIs fully launch.
  138. acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  139. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  140. var issues []*Issue
  141. resp, err := s.client.Do(ctx, req, &issues)
  142. if err != nil {
  143. return nil, resp, err
  144. }
  145. return issues, resp, nil
  146. }
  147. // IssueListByRepoOptions specifies the optional parameters to the
  148. // IssuesService.ListByRepo method.
  149. type IssueListByRepoOptions struct {
  150. // Milestone limits issues for the specified milestone. Possible values are
  151. // a milestone number, "none" for issues with no milestone, "*" for issues
  152. // with any milestone.
  153. Milestone string `url:"milestone,omitempty"`
  154. // State filters issues based on their state. Possible values are: open,
  155. // closed, all. Default is "open".
  156. State string `url:"state,omitempty"`
  157. // Assignee filters issues based on their assignee. Possible values are a
  158. // user name, "none" for issues that are not assigned, "*" for issues with
  159. // any assigned user.
  160. Assignee string `url:"assignee,omitempty"`
  161. // Creator filters issues based on their creator.
  162. Creator string `url:"creator,omitempty"`
  163. // Mentioned filters issues to those mentioned a specific user.
  164. Mentioned string `url:"mentioned,omitempty"`
  165. // Labels filters issues based on their label.
  166. Labels []string `url:"labels,omitempty,comma"`
  167. // Sort specifies how to sort issues. Possible values are: created, updated,
  168. // and comments. Default value is "created".
  169. Sort string `url:"sort,omitempty"`
  170. // Direction in which to sort issues. Possible values are: asc, desc.
  171. // Default is "desc".
  172. Direction string `url:"direction,omitempty"`
  173. // Since filters issues by time.
  174. Since time.Time `url:"since,omitempty"`
  175. ListOptions
  176. }
  177. // ListByRepo lists the issues for the specified repository.
  178. //
  179. // GitHub API docs: https://developer.github.com/v3/issues/#list-issues-for-a-repository
  180. func (s *IssuesService) ListByRepo(ctx context.Context, owner string, repo string, opt *IssueListByRepoOptions) ([]*Issue, *Response, error) {
  181. u := fmt.Sprintf("repos/%v/%v/issues", owner, repo)
  182. u, err := addOptions(u, opt)
  183. if err != nil {
  184. return nil, nil, err
  185. }
  186. req, err := s.client.NewRequest("GET", u, nil)
  187. if err != nil {
  188. return nil, nil, err
  189. }
  190. // TODO: remove custom Accept headers when APIs fully launch.
  191. acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  192. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  193. var issues []*Issue
  194. resp, err := s.client.Do(ctx, req, &issues)
  195. if err != nil {
  196. return nil, resp, err
  197. }
  198. return issues, resp, nil
  199. }
  200. // Get a single issue.
  201. //
  202. // GitHub API docs: https://developer.github.com/v3/issues/#get-a-single-issue
  203. func (s *IssuesService) Get(ctx context.Context, owner string, repo string, number int) (*Issue, *Response, error) {
  204. u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number)
  205. req, err := s.client.NewRequest("GET", u, nil)
  206. if err != nil {
  207. return nil, nil, err
  208. }
  209. // TODO: remove custom Accept headers when APIs fully launch.
  210. acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  211. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  212. issue := new(Issue)
  213. resp, err := s.client.Do(ctx, req, issue)
  214. if err != nil {
  215. return nil, resp, err
  216. }
  217. return issue, resp, nil
  218. }
  219. // Create a new issue on the specified repository.
  220. //
  221. // GitHub API docs: https://developer.github.com/v3/issues/#create-an-issue
  222. func (s *IssuesService) Create(ctx context.Context, owner string, repo string, issue *IssueRequest) (*Issue, *Response, error) {
  223. u := fmt.Sprintf("repos/%v/%v/issues", owner, repo)
  224. req, err := s.client.NewRequest("POST", u, issue)
  225. if err != nil {
  226. return nil, nil, err
  227. }
  228. // TODO: remove custom Accept header when this API fully launches.
  229. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  230. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  231. i := new(Issue)
  232. resp, err := s.client.Do(ctx, req, i)
  233. if err != nil {
  234. return nil, resp, err
  235. }
  236. return i, resp, nil
  237. }
  238. // Edit an issue.
  239. //
  240. // GitHub API docs: https://developer.github.com/v3/issues/#edit-an-issue
  241. func (s *IssuesService) Edit(ctx context.Context, owner string, repo string, number int, issue *IssueRequest) (*Issue, *Response, error) {
  242. u := fmt.Sprintf("repos/%v/%v/issues/%d", owner, repo, number)
  243. req, err := s.client.NewRequest("PATCH", u, issue)
  244. if err != nil {
  245. return nil, nil, err
  246. }
  247. // TODO: remove custom Accept header when this API fully launches.
  248. acceptHeaders := []string{mediaTypeGraphQLNodeIDPreview, mediaTypeLabelDescriptionSearchPreview}
  249. req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
  250. i := new(Issue)
  251. resp, err := s.client.Do(ctx, req, i)
  252. if err != nil {
  253. return nil, resp, err
  254. }
  255. return i, resp, nil
  256. }
  257. // Lock an issue's conversation.
  258. //
  259. // GitHub API docs: https://developer.github.com/v3/issues/#lock-an-issue
  260. func (s *IssuesService) Lock(ctx context.Context, owner string, repo string, number int) (*Response, error) {
  261. u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number)
  262. req, err := s.client.NewRequest("PUT", u, nil)
  263. if err != nil {
  264. return nil, err
  265. }
  266. return s.client.Do(ctx, req, nil)
  267. }
  268. // Unlock an issue's conversation.
  269. //
  270. // GitHub API docs: https://developer.github.com/v3/issues/#unlock-an-issue
  271. func (s *IssuesService) Unlock(ctx context.Context, owner string, repo string, number int) (*Response, error) {
  272. u := fmt.Sprintf("repos/%v/%v/issues/%d/lock", owner, repo, number)
  273. req, err := s.client.NewRequest("DELETE", u, nil)
  274. if err != nil {
  275. return nil, err
  276. }
  277. return s.client.Do(ctx, req, nil)
  278. }