reactions.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. // Copyright 2016 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. )
  10. // ReactionsService provides access to the reactions-related functions in the
  11. // GitHub API.
  12. //
  13. // GitHub API docs: https://developer.github.com/v3/reactions/
  14. type ReactionsService service
  15. // Reaction represents a GitHub reaction.
  16. type Reaction struct {
  17. // ID is the Reaction ID.
  18. ID *int64 `json:"id,omitempty"`
  19. User *User `json:"user,omitempty"`
  20. // Content is the type of reaction.
  21. // Possible values are:
  22. // "+1", "-1", "laugh", "confused", "heart", "hooray".
  23. Content *string `json:"content,omitempty"`
  24. }
  25. // Reactions represents a summary of GitHub reactions.
  26. type Reactions struct {
  27. TotalCount *int `json:"total_count,omitempty"`
  28. PlusOne *int `json:"+1,omitempty"`
  29. MinusOne *int `json:"-1,omitempty"`
  30. Laugh *int `json:"laugh,omitempty"`
  31. Confused *int `json:"confused,omitempty"`
  32. Heart *int `json:"heart,omitempty"`
  33. Hooray *int `json:"hooray,omitempty"`
  34. URL *string `json:"url,omitempty"`
  35. }
  36. func (r Reaction) String() string {
  37. return Stringify(r)
  38. }
  39. // ListCommentReactions lists the reactions for a commit comment.
  40. //
  41. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-a-commit-comment
  42. func (s *ReactionsService) ListCommentReactions(ctx context.Context, owner, repo string, id int64, opt *ListOptions) ([]*Reaction, *Response, error) {
  43. u := fmt.Sprintf("repos/%v/%v/comments/%v/reactions", owner, repo, id)
  44. u, err := addOptions(u, opt)
  45. if err != nil {
  46. return nil, nil, err
  47. }
  48. req, err := s.client.NewRequest("GET", u, nil)
  49. if err != nil {
  50. return nil, nil, err
  51. }
  52. // TODO: remove custom Accept header when this API fully launches.
  53. req.Header.Set("Accept", mediaTypeReactionsPreview)
  54. var m []*Reaction
  55. resp, err := s.client.Do(ctx, req, &m)
  56. if err != nil {
  57. return nil, resp, err
  58. }
  59. return m, resp, nil
  60. }
  61. // CreateCommentReaction creates a reaction for a commit comment.
  62. // Note that if you have already created a reaction of type content, the
  63. // previously created reaction will be returned with Status: 200 OK.
  64. //
  65. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-a-commit-comment
  66. func (s ReactionsService) CreateCommentReaction(ctx context.Context, owner, repo string, id int64, content string) (*Reaction, *Response, error) {
  67. u := fmt.Sprintf("repos/%v/%v/comments/%v/reactions", owner, repo, id)
  68. body := &Reaction{Content: String(content)}
  69. req, err := s.client.NewRequest("POST", u, body)
  70. if err != nil {
  71. return nil, nil, err
  72. }
  73. // TODO: remove custom Accept header when this API fully launches.
  74. req.Header.Set("Accept", mediaTypeReactionsPreview)
  75. m := &Reaction{}
  76. resp, err := s.client.Do(ctx, req, m)
  77. if err != nil {
  78. return nil, resp, err
  79. }
  80. return m, resp, nil
  81. }
  82. // ListIssueReactions lists the reactions for an issue.
  83. //
  84. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue
  85. func (s *ReactionsService) ListIssueReactions(ctx context.Context, owner, repo string, number int, opt *ListOptions) ([]*Reaction, *Response, error) {
  86. u := fmt.Sprintf("repos/%v/%v/issues/%v/reactions", owner, repo, number)
  87. u, err := addOptions(u, opt)
  88. if err != nil {
  89. return nil, nil, err
  90. }
  91. req, err := s.client.NewRequest("GET", u, nil)
  92. if err != nil {
  93. return nil, nil, err
  94. }
  95. // TODO: remove custom Accept header when this API fully launches.
  96. req.Header.Set("Accept", mediaTypeReactionsPreview)
  97. var m []*Reaction
  98. resp, err := s.client.Do(ctx, req, &m)
  99. if err != nil {
  100. return nil, resp, err
  101. }
  102. return m, resp, nil
  103. }
  104. // CreateIssueReaction creates a reaction for an issue.
  105. // Note that if you have already created a reaction of type content, the
  106. // previously created reaction will be returned with Status: 200 OK.
  107. //
  108. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue
  109. func (s ReactionsService) CreateIssueReaction(ctx context.Context, owner, repo string, number int, content string) (*Reaction, *Response, error) {
  110. u := fmt.Sprintf("repos/%v/%v/issues/%v/reactions", owner, repo, number)
  111. body := &Reaction{Content: String(content)}
  112. req, err := s.client.NewRequest("POST", u, body)
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. // TODO: remove custom Accept header when this API fully launches.
  117. req.Header.Set("Accept", mediaTypeReactionsPreview)
  118. m := &Reaction{}
  119. resp, err := s.client.Do(ctx, req, m)
  120. if err != nil {
  121. return nil, resp, err
  122. }
  123. return m, resp, nil
  124. }
  125. // ListIssueCommentReactions lists the reactions for an issue comment.
  126. //
  127. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
  128. func (s *ReactionsService) ListIssueCommentReactions(ctx context.Context, owner, repo string, id int64, opt *ListOptions) ([]*Reaction, *Response, error) {
  129. u := fmt.Sprintf("repos/%v/%v/issues/comments/%v/reactions", owner, repo, id)
  130. u, err := addOptions(u, opt)
  131. if err != nil {
  132. return nil, nil, err
  133. }
  134. req, err := s.client.NewRequest("GET", u, nil)
  135. if err != nil {
  136. return nil, nil, err
  137. }
  138. // TODO: remove custom Accept header when this API fully launches.
  139. req.Header.Set("Accept", mediaTypeReactionsPreview)
  140. var m []*Reaction
  141. resp, err := s.client.Do(ctx, req, &m)
  142. if err != nil {
  143. return nil, resp, err
  144. }
  145. return m, resp, nil
  146. }
  147. // CreateIssueCommentReaction creates a reaction for an issue comment.
  148. // Note that if you have already created a reaction of type content, the
  149. // previously created reaction will be returned with Status: 200 OK.
  150. //
  151. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
  152. func (s ReactionsService) CreateIssueCommentReaction(ctx context.Context, owner, repo string, id int64, content string) (*Reaction, *Response, error) {
  153. u := fmt.Sprintf("repos/%v/%v/issues/comments/%v/reactions", owner, repo, id)
  154. body := &Reaction{Content: String(content)}
  155. req, err := s.client.NewRequest("POST", u, body)
  156. if err != nil {
  157. return nil, nil, err
  158. }
  159. // TODO: remove custom Accept header when this API fully launches.
  160. req.Header.Set("Accept", mediaTypeReactionsPreview)
  161. m := &Reaction{}
  162. resp, err := s.client.Do(ctx, req, m)
  163. if err != nil {
  164. return nil, resp, err
  165. }
  166. return m, resp, nil
  167. }
  168. // ListPullRequestCommentReactions lists the reactions for a pull request review comment.
  169. //
  170. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
  171. func (s *ReactionsService) ListPullRequestCommentReactions(ctx context.Context, owner, repo string, id int64, opt *ListOptions) ([]*Reaction, *Response, error) {
  172. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%v/reactions", owner, repo, id)
  173. u, err := addOptions(u, opt)
  174. if err != nil {
  175. return nil, nil, err
  176. }
  177. req, err := s.client.NewRequest("GET", u, nil)
  178. if err != nil {
  179. return nil, nil, err
  180. }
  181. // TODO: remove custom Accept header when this API fully launches.
  182. req.Header.Set("Accept", mediaTypeReactionsPreview)
  183. var m []*Reaction
  184. resp, err := s.client.Do(ctx, req, &m)
  185. if err != nil {
  186. return nil, resp, err
  187. }
  188. return m, resp, nil
  189. }
  190. // CreatePullRequestCommentReaction creates a reaction for a pull request review comment.
  191. // Note that if you have already created a reaction of type content, the
  192. // previously created reaction will be returned with Status: 200 OK.
  193. //
  194. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
  195. func (s ReactionsService) CreatePullRequestCommentReaction(ctx context.Context, owner, repo string, id int64, content string) (*Reaction, *Response, error) {
  196. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%v/reactions", owner, repo, id)
  197. body := &Reaction{Content: String(content)}
  198. req, err := s.client.NewRequest("POST", u, body)
  199. if err != nil {
  200. return nil, nil, err
  201. }
  202. // TODO: remove custom Accept header when this API fully launches.
  203. req.Header.Set("Accept", mediaTypeReactionsPreview)
  204. m := &Reaction{}
  205. resp, err := s.client.Do(ctx, req, m)
  206. if err != nil {
  207. return nil, resp, err
  208. }
  209. return m, resp, nil
  210. }
  211. // DeleteReaction deletes a reaction.
  212. //
  213. // GitHub API docs: https://developer.github.com/v3/reaction/reactions/#delete-a-reaction-archive
  214. func (s *ReactionsService) DeleteReaction(ctx context.Context, id int64) (*Response, error) {
  215. u := fmt.Sprintf("reactions/%v", id)
  216. req, err := s.client.NewRequest("DELETE", u, nil)
  217. if err != nil {
  218. return nil, err
  219. }
  220. // TODO: remove custom Accept header when this API fully launches.
  221. req.Header.Set("Accept", mediaTypeReactionsPreview)
  222. return s.client.Do(ctx, req, nil)
  223. }