pulls_comments.go 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. Position *int `json:"position,omitempty"`
  19. OriginalPosition *int `json:"original_position,omitempty"`
  20. CommitID *string `json:"commit_id,omitempty"`
  21. OriginalCommitID *string `json:"original_commit_id,omitempty"`
  22. User *User `json:"user,omitempty"`
  23. Reactions *Reactions `json:"reactions,omitempty"`
  24. CreatedAt *time.Time `json:"created_at,omitempty"`
  25. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  26. URL *string `json:"url,omitempty"`
  27. HTMLURL *string `json:"html_url,omitempty"`
  28. PullRequestURL *string `json:"pull_request_url,omitempty"`
  29. }
  30. func (p PullRequestComment) String() string {
  31. return Stringify(p)
  32. }
  33. // PullRequestListCommentsOptions specifies the optional parameters to the
  34. // PullRequestsService.ListComments method.
  35. type PullRequestListCommentsOptions struct {
  36. // Sort specifies how to sort comments. Possible values are: created, updated.
  37. Sort string `url:"sort,omitempty"`
  38. // Direction in which to sort comments. Possible values are: asc, desc.
  39. Direction string `url:"direction,omitempty"`
  40. // Since filters comments by time.
  41. Since time.Time `url:"since,omitempty"`
  42. ListOptions
  43. }
  44. // ListComments lists all comments on the specified pull request. Specifying a
  45. // pull request number of 0 will return all comments on all pull requests for
  46. // the repository.
  47. //
  48. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request
  49. func (s *PullRequestsService) ListComments(ctx context.Context, owner string, repo string, number int, opt *PullRequestListCommentsOptions) ([]*PullRequestComment, *Response, error) {
  50. var u string
  51. if number == 0 {
  52. u = fmt.Sprintf("repos/%v/%v/pulls/comments", owner, repo)
  53. } else {
  54. u = fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
  55. }
  56. u, err := addOptions(u, opt)
  57. if err != nil {
  58. return nil, nil, err
  59. }
  60. req, err := s.client.NewRequest("GET", u, nil)
  61. if err != nil {
  62. return nil, nil, err
  63. }
  64. // TODO: remove custom Accept header when this API fully launches.
  65. req.Header.Set("Accept", mediaTypeReactionsPreview)
  66. var comments []*PullRequestComment
  67. resp, err := s.client.Do(ctx, req, &comments)
  68. if err != nil {
  69. return nil, resp, err
  70. }
  71. return comments, resp, nil
  72. }
  73. // GetComment fetches the specified pull request comment.
  74. //
  75. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#get-a-single-comment
  76. func (s *PullRequestsService) GetComment(ctx context.Context, owner string, repo string, number int) (*PullRequestComment, *Response, error) {
  77. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, number)
  78. req, err := s.client.NewRequest("GET", u, nil)
  79. if err != nil {
  80. return nil, nil, err
  81. }
  82. // TODO: remove custom Accept header when this API fully launches.
  83. req.Header.Set("Accept", mediaTypeReactionsPreview)
  84. comment := new(PullRequestComment)
  85. resp, err := s.client.Do(ctx, req, comment)
  86. if err != nil {
  87. return nil, resp, err
  88. }
  89. return comment, resp, nil
  90. }
  91. // CreateComment creates a new comment on the specified pull request.
  92. //
  93. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#create-a-comment
  94. func (s *PullRequestsService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
  95. u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
  96. req, err := s.client.NewRequest("POST", u, comment)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. c := new(PullRequestComment)
  101. resp, err := s.client.Do(ctx, req, c)
  102. if err != nil {
  103. return nil, resp, err
  104. }
  105. return c, resp, nil
  106. }
  107. // EditComment updates a pull request comment.
  108. //
  109. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#edit-a-comment
  110. func (s *PullRequestsService) EditComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
  111. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, number)
  112. req, err := s.client.NewRequest("PATCH", u, comment)
  113. if err != nil {
  114. return nil, nil, err
  115. }
  116. c := new(PullRequestComment)
  117. resp, err := s.client.Do(ctx, req, c)
  118. if err != nil {
  119. return nil, resp, err
  120. }
  121. return c, resp, nil
  122. }
  123. // DeleteComment deletes a pull request comment.
  124. //
  125. // GitHub API docs: https://developer.github.com/v3/pulls/comments/#delete-a-comment
  126. func (s *PullRequestsService) DeleteComment(ctx context.Context, owner string, repo string, number int) (*Response, error) {
  127. u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, number)
  128. req, err := s.client.NewRequest("DELETE", u, nil)
  129. if err != nil {
  130. return nil, err
  131. }
  132. return s.client.Do(ctx, req, nil)
  133. }