pulls_reviews.go 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. "time"
  10. )
  11. // PullRequestReview represents a review of a pull request.
  12. type PullRequestReview struct {
  13. ID *int64 `json:"id,omitempty"`
  14. User *User `json:"user,omitempty"`
  15. Body *string `json:"body,omitempty"`
  16. SubmittedAt *time.Time `json:"submitted_at,omitempty"`
  17. CommitID *string `json:"commit_id,omitempty"`
  18. HTMLURL *string `json:"html_url,omitempty"`
  19. PullRequestURL *string `json:"pull_request_url,omitempty"`
  20. State *string `json:"state,omitempty"`
  21. }
  22. func (p PullRequestReview) String() string {
  23. return Stringify(p)
  24. }
  25. // DraftReviewComment represents a comment part of the review.
  26. type DraftReviewComment struct {
  27. Path *string `json:"path,omitempty"`
  28. Position *int `json:"position,omitempty"`
  29. Body *string `json:"body,omitempty"`
  30. }
  31. func (c DraftReviewComment) String() string {
  32. return Stringify(c)
  33. }
  34. // PullRequestReviewRequest represents a request to create a review.
  35. type PullRequestReviewRequest struct {
  36. CommitID *string `json:"commit_id,omitempty"`
  37. Body *string `json:"body,omitempty"`
  38. Event *string `json:"event,omitempty"`
  39. Comments []*DraftReviewComment `json:"comments,omitempty"`
  40. }
  41. func (r PullRequestReviewRequest) String() string {
  42. return Stringify(r)
  43. }
  44. // PullRequestReviewDismissalRequest represents a request to dismiss a review.
  45. type PullRequestReviewDismissalRequest struct {
  46. Message *string `json:"message,omitempty"`
  47. }
  48. func (r PullRequestReviewDismissalRequest) String() string {
  49. return Stringify(r)
  50. }
  51. // ListReviews lists all reviews on the specified pull request.
  52. //
  53. // TODO: Follow up with GitHub support about an issue with this method's
  54. // returned error format and remove this comment once it's fixed.
  55. // Read more about it here - https://github.com/google/go-github/issues/540
  56. //
  57. // GitHub API docs: https://developer.github.com/v3/pulls/reviews/#list-reviews-on-a-pull-request
  58. func (s *PullRequestsService) ListReviews(ctx context.Context, owner, repo string, number int, opt *ListOptions) ([]*PullRequestReview, *Response, error) {
  59. u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews", owner, repo, number)
  60. u, err := addOptions(u, opt)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. req, err := s.client.NewRequest("GET", u, nil)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. var reviews []*PullRequestReview
  69. resp, err := s.client.Do(ctx, req, &reviews)
  70. if err != nil {
  71. return nil, resp, err
  72. }
  73. return reviews, resp, nil
  74. }
  75. // GetReview fetches the specified pull request review.
  76. //
  77. // TODO: Follow up with GitHub support about an issue with this method's
  78. // returned error format and remove this comment once it's fixed.
  79. // Read more about it here - https://github.com/google/go-github/issues/540
  80. //
  81. // GitHub API docs: https://developer.github.com/v3/pulls/reviews/#get-a-single-review
  82. func (s *PullRequestsService) GetReview(ctx context.Context, owner, repo string, number int, reviewID int64) (*PullRequestReview, *Response, error) {
  83. u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d", owner, repo, number, reviewID)
  84. req, err := s.client.NewRequest("GET", u, nil)
  85. if err != nil {
  86. return nil, nil, err
  87. }
  88. review := new(PullRequestReview)
  89. resp, err := s.client.Do(ctx, req, review)
  90. if err != nil {
  91. return nil, resp, err
  92. }
  93. return review, resp, nil
  94. }
  95. // DeletePendingReview deletes the specified pull request pending review.
  96. //
  97. // TODO: Follow up with GitHub support about an issue with this method's
  98. // returned error format and remove this comment once it's fixed.
  99. // Read more about it here - https://github.com/google/go-github/issues/540
  100. //
  101. // GitHub API docs: https://developer.github.com/v3/pulls/reviews/#delete-a-pending-review
  102. func (s *PullRequestsService) DeletePendingReview(ctx context.Context, owner, repo string, number int, reviewID int64) (*PullRequestReview, *Response, error) {
  103. u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d", owner, repo, number, reviewID)
  104. req, err := s.client.NewRequest("DELETE", u, nil)
  105. if err != nil {
  106. return nil, nil, err
  107. }
  108. review := new(PullRequestReview)
  109. resp, err := s.client.Do(ctx, req, review)
  110. if err != nil {
  111. return nil, resp, err
  112. }
  113. return review, resp, nil
  114. }
  115. // ListReviewComments lists all the comments for the specified review.
  116. //
  117. // TODO: Follow up with GitHub support about an issue with this method's
  118. // returned error format and remove this comment once it's fixed.
  119. // Read more about it here - https://github.com/google/go-github/issues/540
  120. //
  121. // GitHub API docs: https://developer.github.com/v3/pulls/reviews/#get-comments-for-a-single-review
  122. func (s *PullRequestsService) ListReviewComments(ctx context.Context, owner, repo string, number int, reviewID int64, opt *ListOptions) ([]*PullRequestComment, *Response, error) {
  123. u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d/comments", owner, repo, number, reviewID)
  124. u, err := addOptions(u, opt)
  125. if err != nil {
  126. return nil, nil, err
  127. }
  128. req, err := s.client.NewRequest("GET", u, nil)
  129. if err != nil {
  130. return nil, nil, err
  131. }
  132. var comments []*PullRequestComment
  133. resp, err := s.client.Do(ctx, req, &comments)
  134. if err != nil {
  135. return nil, resp, err
  136. }
  137. return comments, resp, nil
  138. }
  139. // CreateReview creates a new review on the specified pull request.
  140. //
  141. // TODO: Follow up with GitHub support about an issue with this method's
  142. // returned error format and remove this comment once it's fixed.
  143. // Read more about it here - https://github.com/google/go-github/issues/540
  144. //
  145. // GitHub API docs: https://developer.github.com/v3/pulls/reviews/#create-a-pull-request-review
  146. func (s *PullRequestsService) CreateReview(ctx context.Context, owner, repo string, number int, review *PullRequestReviewRequest) (*PullRequestReview, *Response, error) {
  147. u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews", owner, repo, number)
  148. req, err := s.client.NewRequest("POST", u, review)
  149. if err != nil {
  150. return nil, nil, err
  151. }
  152. r := new(PullRequestReview)
  153. resp, err := s.client.Do(ctx, req, r)
  154. if err != nil {
  155. return nil, resp, err
  156. }
  157. return r, resp, nil
  158. }
  159. // SubmitReview submits a specified review on the specified pull request.
  160. //
  161. // TODO: Follow up with GitHub support about an issue with this method's
  162. // returned error format and remove this comment once it's fixed.
  163. // Read more about it here - https://github.com/google/go-github/issues/540
  164. //
  165. // GitHub API docs: https://developer.github.com/v3/pulls/reviews/#submit-a-pull-request-review
  166. func (s *PullRequestsService) SubmitReview(ctx context.Context, owner, repo string, number int, reviewID int64, review *PullRequestReviewRequest) (*PullRequestReview, *Response, error) {
  167. u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d/events", owner, repo, number, reviewID)
  168. req, err := s.client.NewRequest("POST", u, review)
  169. if err != nil {
  170. return nil, nil, err
  171. }
  172. r := new(PullRequestReview)
  173. resp, err := s.client.Do(ctx, req, r)
  174. if err != nil {
  175. return nil, resp, err
  176. }
  177. return r, resp, nil
  178. }
  179. // DismissReview dismisses a specified review on the specified pull request.
  180. //
  181. // TODO: Follow up with GitHub support about an issue with this method's
  182. // returned error format and remove this comment once it's fixed.
  183. // Read more about it here - https://github.com/google/go-github/issues/540
  184. //
  185. // GitHub API docs: https://developer.github.com/v3/pulls/reviews/#dismiss-a-pull-request-review
  186. func (s *PullRequestsService) DismissReview(ctx context.Context, owner, repo string, number int, reviewID int64, review *PullRequestReviewDismissalRequest) (*PullRequestReview, *Response, error) {
  187. u := fmt.Sprintf("repos/%v/%v/pulls/%d/reviews/%d/dismissals", owner, repo, number, reviewID)
  188. req, err := s.client.NewRequest("PUT", u, review)
  189. if err != nil {
  190. return nil, nil, err
  191. }
  192. r := new(PullRequestReview)
  193. resp, err := s.client.Do(ctx, req, r)
  194. if err != nil {
  195. return nil, resp, err
  196. }
  197. return r, resp, nil
  198. }