reactions.go 9.3 KB

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