pulls_comments.go 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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,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. // EditComment updates a pull request comment.
  112. // A non-nil comment.Body must be provided. Other comment fields should be left nil.
  113. //
  114. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#edit-a-comment
  115. func (s *PullRequestsService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
  116. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
  117. req, err := s.client.NewRequest("PATCH", u, comment)
  118. if err != nil {
  119. return nil, nil, err
  120. }
  121. c := new(PullRequestComment)
  122. resp, err := s.client.Do(ctx, req, c)
  123. if err != nil {
  124. return nil, resp, err
  125. }
  126. return c, resp, nil
  127. }
  128. // DeleteComment deletes a pull request comment.
  129. //
  130. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#delete-a-comment
  131. func (s *PullRequestsService) DeleteComment(ctx context.Context, owner string, repo string, commentID int64) (*Response, error) {
  132. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
  133. req, err := s.client.NewRequest("DELETE", u, nil)
  134. if err != nil {
  135. return nil, err
  136. }
  137. return s.client.Do(ctx, req, nil)
  138. }