本站源代码
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.

189 lines
6.5KB

  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. "time"
  10. )
  11. // PullRequestComment represents a comment left on a pull request.
  12. type PullRequestComment struct {
  13. ID *int64 `json:"id,omitempty"`
  14. InReplyTo *int64 `json:"in_reply_to_id,omitempty"`
  15. Body *string `json:"body,omitempty"`
  16. Path *string `json:"path,omitempty"`
  17. DiffHunk *string `json:"diff_hunk,omitempty"`
  18. PullRequestReviewID *int64 `json:"pull_request_review_id,omitempty"`
  19. Position *int `json:"position,omitempty"`
  20. OriginalPosition *int `json:"original_position,omitempty"`
  21. CommitID *string `json:"commit_id,omitempty"`
  22. OriginalCommitID *string `json:"original_commit_id,omitempty"`
  23. User *User `json:"user,omitempty"`
  24. Reactions *Reactions `json:"reactions,omitempty"`
  25. CreatedAt *time.Time `json:"created_at,omitempty"`
  26. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  27. // AuthorAssociation is the comment author's relationship to the pull request's repository.
  28. // Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE".
  29. AuthorAssociation *string `json:"author_association,omitempty"`
  30. URL *string `json:"url,omitempty"`
  31. HTMLURL *string `json:"html_url,omitempty"`
  32. PullRequestURL *string `json:"pull_request_url,omitempty"`
  33. }
  34. func (p PullRequestComment) String() string {
  35. return Stringify(p)
  36. }
  37. // PullRequestListCommentsOptions specifies the optional parameters to the
  38. // PullRequestsService.ListComments method.
  39. type PullRequestListCommentsOptions struct {
  40. // Sort specifies how to sort comments. Possible values are: created, updated.
  41. Sort string `url:"sort,omitempty"`
  42. // Direction in which to sort comments. Possible values are: asc, desc.
  43. Direction string `url:"direction,omitempty"`
  44. // Since filters comments by time.
  45. Since time.Time `url:"since,omitempty"`
  46. ListOptions
  47. }
  48. // ListComments lists all comments on the specified pull request. Specifying a
  49. // pull request number of 0 will return all comments on all pull requests for
  50. // the repository.
  51. //
  52. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request
  53. func (s *PullRequestsService) ListComments(ctx context.Context, owner string, repo string, number int, opt *PullRequestListCommentsOptions) ([]*PullRequestComment, *Response, error) {
  54. var u string
  55. if number == 0 {
  56. u = fmt.Sprintf("repos/%v/%v/pulls/comments", owner, repo)
  57. } else {
  58. u = fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
  59. }
  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. // TODO: remove custom Accept header when this API fully launches.
  69. req.Header.Set("Accept", mediaTypeReactionsPreview)
  70. var comments []*PullRequestComment
  71. resp, err := s.client.Do(ctx, req, &comments)
  72. if err != nil {
  73. return nil, resp, err
  74. }
  75. return comments, resp, nil
  76. }
  77. // GetComment fetches the specified pull request comment.
  78. //
  79. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#get-a-single-comment
  80. func (s *PullRequestsService) GetComment(ctx context.Context, owner string, repo string, commentID int64) (*PullRequestComment, *Response, error) {
  81. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
  82. req, err := s.client.NewRequest("GET", u, nil)
  83. if err != nil {
  84. return nil, nil, err
  85. }
  86. // TODO: remove custom Accept header when this API fully launches.
  87. req.Header.Set("Accept", mediaTypeReactionsPreview)
  88. comment := new(PullRequestComment)
  89. resp, err := s.client.Do(ctx, req, comment)
  90. if err != nil {
  91. return nil, resp, err
  92. }
  93. return comment, resp, nil
  94. }
  95. // CreateComment creates a new comment on the specified pull request.
  96. //
  97. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#create-a-comment
  98. func (s *PullRequestsService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
  99. u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
  100. req, err := s.client.NewRequest("POST", u, comment)
  101. if err != nil {
  102. return nil, nil, err
  103. }
  104. c := new(PullRequestComment)
  105. resp, err := s.client.Do(ctx, req, c)
  106. if err != nil {
  107. return nil, resp, err
  108. }
  109. return c, resp, nil
  110. }
  111. // CreateCommentInReplyTo creates a new comment as a reply to an existing pull request comment.
  112. //
  113. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#alternative-input
  114. func (s *PullRequestsService) CreateCommentInReplyTo(ctx context.Context, owner string, repo string, number int, body string, commentID int64) (*PullRequestComment, *Response, error) {
  115. comment := &struct {
  116. Body string `json:"body,omitempty"`
  117. InReplyTo int64 `json:"in_reply_to,omitempty"`
  118. }{
  119. Body: body,
  120. InReplyTo: commentID,
  121. }
  122. u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
  123. req, err := s.client.NewRequest("POST", u, comment)
  124. if err != nil {
  125. return nil, nil, err
  126. }
  127. c := new(PullRequestComment)
  128. resp, err := s.client.Do(ctx, req, c)
  129. if err != nil {
  130. return nil, resp, err
  131. }
  132. return c, resp, nil
  133. }
  134. // EditComment updates a pull request comment.
  135. // A non-nil comment.Body must be provided. Other comment fields should be left nil.
  136. //
  137. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#edit-a-comment
  138. func (s *PullRequestsService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
  139. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
  140. req, err := s.client.NewRequest("PATCH", u, comment)
  141. if err != nil {
  142. return nil, nil, err
  143. }
  144. c := new(PullRequestComment)
  145. resp, err := s.client.Do(ctx, req, c)
  146. if err != nil {
  147. return nil, resp, err
  148. }
  149. return c, resp, nil
  150. }
  151. // DeleteComment deletes a pull request comment.
  152. //
  153. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#delete-a-comment
  154. func (s *PullRequestsService) DeleteComment(ctx context.Context, owner string, repo string, commentID int64) (*Response, error) {
  155. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
  156. req, err := s.client.NewRequest("DELETE", u, nil)
  157. if err != nil {
  158. return nil, err
  159. }
  160. return s.client.Do(ctx, req, nil)
  161. }
上海开阖软件有限公司 沪ICP备12045867号-1