You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

377 lines
12 KiB

  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. NodeID *string `json:"node_id,omitempty"`
  21. // Content is the type of reaction.
  22. // Possible values are:
  23. // "+1", "-1", "laugh", "confused", "heart", "hooray".
  24. Content *string `json:"content,omitempty"`
  25. }
  26. // Reactions represents a summary of GitHub reactions.
  27. type Reactions struct {
  28. TotalCount *int `json:"total_count,omitempty"`
  29. PlusOne *int `json:"+1,omitempty"`
  30. MinusOne *int `json:"-1,omitempty"`
  31. Laugh *int `json:"laugh,omitempty"`
  32. Confused *int `json:"confused,omitempty"`
  33. Heart *int `json:"heart,omitempty"`
  34. Hooray *int `json:"hooray,omitempty"`
  35. URL *string `json:"url,omitempty"`
  36. }
  37. func (r Reaction) String() string {
  38. return Stringify(r)
  39. }
  40. // ListCommentReactions lists the reactions for a commit comment.
  41. //
  42. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-a-commit-comment
  43. func (s *ReactionsService) ListCommentReactions(ctx context.Context, owner, repo string, id int64, opt *ListOptions) ([]*Reaction, *Response, error) {
  44. u := fmt.Sprintf("repos/%v/%v/comments/%v/reactions", owner, repo, id)
  45. u, err := addOptions(u, opt)
  46. if err != nil {
  47. return nil, nil, err
  48. }
  49. req, err := s.client.NewRequest("GET", u, nil)
  50. if err != nil {
  51. return nil, nil, err
  52. }
  53. // TODO: remove custom Accept headers when APIs fully launch.
  54. req.Header.Set("Accept", mediaTypeReactionsPreview)
  55. var m []*Reaction
  56. resp, err := s.client.Do(ctx, req, &m)
  57. if err != nil {
  58. return nil, resp, err
  59. }
  60. return m, resp, nil
  61. }
  62. // CreateCommentReaction creates a reaction for a commit comment.
  63. // Note that if you have already created a reaction of type content, the
  64. // previously created reaction will be returned with Status: 200 OK.
  65. // The content should have one of the following values: "+1", "-1", "laugh", "confused", "heart", "hooray".
  66. //
  67. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-a-commit-comment
  68. func (s ReactionsService) CreateCommentReaction(ctx context.Context, owner, repo string, id int64, content string) (*Reaction, *Response, error) {
  69. u := fmt.Sprintf("repos/%v/%v/comments/%v/reactions", owner, repo, id)
  70. body := &Reaction{Content: String(content)}
  71. req, err := s.client.NewRequest("POST", u, body)
  72. if err != nil {
  73. return nil, nil, err
  74. }
  75. // TODO: remove custom Accept headers when APIs fully launch.
  76. req.Header.Set("Accept", mediaTypeReactionsPreview)
  77. m := &Reaction{}
  78. resp, err := s.client.Do(ctx, req, m)
  79. if err != nil {
  80. return nil, resp, err
  81. }
  82. return m, resp, nil
  83. }
  84. // ListIssueReactions lists the reactions for an issue.
  85. //
  86. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue
  87. func (s *ReactionsService) ListIssueReactions(ctx context.Context, owner, repo string, number int, opt *ListOptions) ([]*Reaction, *Response, error) {
  88. u := fmt.Sprintf("repos/%v/%v/issues/%v/reactions", owner, repo, number)
  89. u, err := addOptions(u, opt)
  90. if err != nil {
  91. return nil, nil, err
  92. }
  93. req, err := s.client.NewRequest("GET", u, nil)
  94. if err != nil {
  95. return nil, nil, err
  96. }
  97. // TODO: remove custom Accept headers when APIs fully launch.
  98. req.Header.Set("Accept", mediaTypeReactionsPreview)
  99. var m []*Reaction
  100. resp, err := s.client.Do(ctx, req, &m)
  101. if err != nil {
  102. return nil, resp, err
  103. }
  104. return m, resp, nil
  105. }
  106. // CreateIssueReaction creates a reaction for an issue.
  107. // Note that if you have already created a reaction of type content, the
  108. // previously created reaction will be returned with Status: 200 OK.
  109. // The content should have one of the following values: "+1", "-1", "laugh", "confused", "heart", "hooray".
  110. //
  111. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue
  112. func (s ReactionsService) CreateIssueReaction(ctx context.Context, owner, repo string, number int, content string) (*Reaction, *Response, error) {
  113. u := fmt.Sprintf("repos/%v/%v/issues/%v/reactions", owner, repo, number)
  114. body := &Reaction{Content: String(content)}
  115. req, err := s.client.NewRequest("POST", u, body)
  116. if err != nil {
  117. return nil, nil, err
  118. }
  119. // TODO: remove custom Accept headers when APIs fully launch.
  120. req.Header.Set("Accept", mediaTypeReactionsPreview)
  121. m := &Reaction{}
  122. resp, err := s.client.Do(ctx, req, m)
  123. if err != nil {
  124. return nil, resp, err
  125. }
  126. return m, resp, nil
  127. }
  128. // ListIssueCommentReactions lists the reactions for an issue comment.
  129. //
  130. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
  131. func (s *ReactionsService) ListIssueCommentReactions(ctx context.Context, owner, repo string, id int64, opt *ListOptions) ([]*Reaction, *Response, error) {
  132. u := fmt.Sprintf("repos/%v/%v/issues/comments/%v/reactions", owner, repo, id)
  133. u, err := addOptions(u, opt)
  134. if err != nil {
  135. return nil, nil, err
  136. }
  137. req, err := s.client.NewRequest("GET", u, nil)
  138. if err != nil {
  139. return nil, nil, err
  140. }
  141. // TODO: remove custom Accept headers when APIs fully launch.
  142. req.Header.Set("Accept", mediaTypeReactionsPreview)
  143. var m []*Reaction
  144. resp, err := s.client.Do(ctx, req, &m)
  145. if err != nil {
  146. return nil, resp, err
  147. }
  148. return m, resp, nil
  149. }
  150. // CreateIssueCommentReaction creates a reaction for an issue comment.
  151. // Note that if you have already created a reaction of type content, the
  152. // previously created reaction will be returned with Status: 200 OK.
  153. // The content should have one of the following values: "+1", "-1", "laugh", "confused", "heart", "hooray".
  154. //
  155. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
  156. func (s ReactionsService) CreateIssueCommentReaction(ctx context.Context, owner, repo string, id int64, content string) (*Reaction, *Response, error) {
  157. u := fmt.Sprintf("repos/%v/%v/issues/comments/%v/reactions", owner, repo, id)
  158. body := &Reaction{Content: String(content)}
  159. req, err := s.client.NewRequest("POST", u, body)
  160. if err != nil {
  161. return nil, nil, err
  162. }
  163. // TODO: remove custom Accept headers when APIs fully launch.
  164. req.Header.Set("Accept", mediaTypeReactionsPreview)
  165. m := &Reaction{}
  166. resp, err := s.client.Do(ctx, req, m)
  167. if err != nil {
  168. return nil, resp, err
  169. }
  170. return m, resp, nil
  171. }
  172. // ListPullRequestCommentReactions lists the reactions for a pull request review comment.
  173. //
  174. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-an-issue-comment
  175. func (s *ReactionsService) ListPullRequestCommentReactions(ctx context.Context, owner, repo string, id int64, opt *ListOptions) ([]*Reaction, *Response, error) {
  176. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%v/reactions", owner, repo, id)
  177. u, err := addOptions(u, opt)
  178. if err != nil {
  179. return nil, nil, err
  180. }
  181. req, err := s.client.NewRequest("GET", u, nil)
  182. if err != nil {
  183. return nil, nil, err
  184. }
  185. // TODO: remove custom Accept headers when APIs fully launch.
  186. req.Header.Set("Accept", mediaTypeReactionsPreview)
  187. var m []*Reaction
  188. resp, err := s.client.Do(ctx, req, &m)
  189. if err != nil {
  190. return nil, resp, err
  191. }
  192. return m, resp, nil
  193. }
  194. // CreatePullRequestCommentReaction creates a reaction for a pull request review comment.
  195. // Note that if you have already created a reaction of type content, the
  196. // previously created reaction will be returned with Status: 200 OK.
  197. // The content should have one of the following values: "+1", "-1", "laugh", "confused", "heart", "hooray".
  198. //
  199. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-an-issue-comment
  200. func (s ReactionsService) CreatePullRequestCommentReaction(ctx context.Context, owner, repo string, id int64, content string) (*Reaction, *Response, error) {
  201. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%v/reactions", owner, repo, id)
  202. body := &Reaction{Content: String(content)}
  203. req, err := s.client.NewRequest("POST", u, body)
  204. if err != nil {
  205. return nil, nil, err
  206. }
  207. // TODO: remove custom Accept headers when APIs fully launch.
  208. req.Header.Set("Accept", mediaTypeReactionsPreview)
  209. m := &Reaction{}
  210. resp, err := s.client.Do(ctx, req, m)
  211. if err != nil {
  212. return nil, resp, err
  213. }
  214. return m, resp, nil
  215. }
  216. // ListTeamDiscussionReactions lists the reactions for a team discussion.
  217. //
  218. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-a-team-discussion
  219. func (s *ReactionsService) ListTeamDiscussionReactions(ctx context.Context, teamID int64, discussionNumber int, opt *ListOptions) ([]*Reaction, *Response, error) {
  220. u := fmt.Sprintf("teams/%v/discussions/%v/reactions", teamID, discussionNumber)
  221. u, err := addOptions(u, opt)
  222. if err != nil {
  223. return nil, nil, err
  224. }
  225. req, err := s.client.NewRequest("GET", u, nil)
  226. if err != nil {
  227. return nil, nil, err
  228. }
  229. req.Header.Set("Accept", mediaTypeReactionsPreview)
  230. var m []*Reaction
  231. resp, err := s.client.Do(ctx, req, &m)
  232. if err != nil {
  233. return nil, resp, err
  234. }
  235. return m, resp, nil
  236. }
  237. // CreateTeamDiscussionReaction creates a reaction for a team discussion.
  238. // The content should have one of the following values: "+1", "-1", "laugh", "confused", "heart", "hooray".
  239. //
  240. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-a-team-discussion
  241. func (s *ReactionsService) CreateTeamDiscussionReaction(ctx context.Context, teamID int64, discussionNumber int, content string) (*Reaction, *Response, error) {
  242. u := fmt.Sprintf("teams/%v/discussions/%v/reactions", teamID, discussionNumber)
  243. body := &Reaction{Content: String(content)}
  244. req, err := s.client.NewRequest("POST", u, body)
  245. if err != nil {
  246. return nil, nil, err
  247. }
  248. req.Header.Set("Accept", mediaTypeReactionsPreview)
  249. m := &Reaction{}
  250. resp, err := s.client.Do(ctx, req, m)
  251. if err != nil {
  252. return nil, resp, err
  253. }
  254. return m, resp, nil
  255. }
  256. // ListTeamDiscussionCommentReactions lists the reactions for a team discussion comment.
  257. //
  258. // GitHub API docs: https://developer.github.com/v3/reactions/#list-reactions-for-a-team-discussion-comment
  259. func (s *ReactionsService) ListTeamDiscussionCommentReactions(ctx context.Context, teamID int64, discussionNumber, commentNumber int, opt *ListOptions) ([]*Reaction, *Response, error) {
  260. u := fmt.Sprintf("teams/%v/discussions/%v/comments/%v/reactions", teamID, discussionNumber, commentNumber)
  261. u, err := addOptions(u, opt)
  262. if err != nil {
  263. return nil, nil, err
  264. }
  265. req, err := s.client.NewRequest("GET", u, nil)
  266. if err != nil {
  267. return nil, nil, err
  268. }
  269. req.Header.Set("Accept", mediaTypeReactionsPreview)
  270. var m []*Reaction
  271. resp, err := s.client.Do(ctx, req, &m)
  272. if err != nil {
  273. return nil, nil, err
  274. }
  275. return m, resp, nil
  276. }
  277. // CreateTeamDiscussionCommentReaction creates a reaction for a team discussion comment.
  278. // The content should have one of the following values: "+1", "-1", "laugh", "confused", "heart", "hooray".
  279. //
  280. // GitHub API docs: https://developer.github.com/v3/reactions/#create-reaction-for-a-team-discussion-comment
  281. func (s *ReactionsService) CreateTeamDiscussionCommentReaction(ctx context.Context, teamID int64, discussionNumber, commentNumber int, content string) (*Reaction, *Response, error) {
  282. u := fmt.Sprintf("teams/%v/discussions/%v/comments/%v/reactions", teamID, discussionNumber, commentNumber)
  283. body := &Reaction{Content: String(content)}
  284. req, err := s.client.NewRequest("POST", u, body)
  285. if err != nil {
  286. return nil, nil, err
  287. }
  288. req.Header.Set("Accept", mediaTypeReactionsPreview)
  289. m := &Reaction{}
  290. resp, err := s.client.Do(ctx, req, m)
  291. if err != nil {
  292. return nil, resp, err
  293. }
  294. return m, resp, nil
  295. }
  296. // DeleteReaction deletes a reaction.
  297. //
  298. // GitHub API docs: https://developer.github.com/v3/reaction/reactions/#delete-a-reaction-archive
  299. func (s *ReactionsService) DeleteReaction(ctx context.Context, id int64) (*Response, error) {
  300. u := fmt.Sprintf("reactions/%v", id)
  301. req, err := s.client.NewRequest("DELETE", u, nil)
  302. if err != nil {
  303. return nil, err
  304. }
  305. // TODO: remove custom Accept header when this API fully launches.
  306. req.Header.Set("Accept", mediaTypeReactionsPreview)
  307. return s.client.Do(ctx, req, nil)
  308. }