issues_comments.go 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. // IssueComment represents a comment left on an issue.
  12. type IssueComment struct {
  13. ID *int64 `json:"id,omitempty"`
  14. Body *string `json:"body,omitempty"`
  15. User *User `json:"user,omitempty"`
  16. Reactions *Reactions `json:"reactions,omitempty"`
  17. CreatedAt *time.Time `json:"created_at,omitempty"`
  18. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  19. // AuthorAssociation is the comment author's relationship to the issue's repository.
  20. // Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE".
  21. AuthorAssociation *string `json:"author_association,omitempty"`
  22. URL *string `json:"url,omitempty"`
  23. HTMLURL *string `json:"html_url,omitempty"`
  24. IssueURL *string `json:"issue_url,omitempty"`
  25. }
  26. func (i IssueComment) String() string {
  27. return Stringify(i)
  28. }
  29. // IssueListCommentsOptions specifies the optional parameters to the
  30. // IssuesService.ListComments method.
  31. type IssueListCommentsOptions struct {
  32. // Sort specifies how to sort comments. Possible values are: created, updated.
  33. Sort string `url:"sort,omitempty"`
  34. // Direction in which to sort comments. Possible values are: asc, desc.
  35. Direction string `url:"direction,omitempty"`
  36. // Since filters comments by time.
  37. Since time.Time `url:"since,omitempty"`
  38. ListOptions
  39. }
  40. // ListComments lists all comments on the specified issue. Specifying an issue
  41. // number of 0 will return all comments on all issues for the repository.
  42. //
  43. // GitHub API docs: https://developer.github.com/v3/issues/comments/#list-comments-on-an-issue
  44. func (s *IssuesService) ListComments(ctx context.Context, owner string, repo string, number int, opt *IssueListCommentsOptions) ([]*IssueComment, *Response, error) {
  45. var u string
  46. if number == 0 {
  47. u = fmt.Sprintf("repos/%v/%v/issues/comments", owner, repo)
  48. } else {
  49. u = fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number)
  50. }
  51. u, err := addOptions(u, opt)
  52. if err != nil {
  53. return nil, nil, err
  54. }
  55. req, err := s.client.NewRequest("GET", u, nil)
  56. if err != nil {
  57. return nil, nil, err
  58. }
  59. // TODO: remove custom Accept header when this API fully launches.
  60. req.Header.Set("Accept", mediaTypeReactionsPreview)
  61. var comments []*IssueComment
  62. resp, err := s.client.Do(ctx, req, &comments)
  63. if err != nil {
  64. return nil, resp, err
  65. }
  66. return comments, resp, nil
  67. }
  68. // GetComment fetches the specified issue comment.
  69. //
  70. // GitHub API docs: https://developer.github.com/v3/issues/comments/#get-a-single-comment
  71. func (s *IssuesService) GetComment(ctx context.Context, owner string, repo string, commentID int64) (*IssueComment, *Response, error) {
  72. u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID)
  73. req, err := s.client.NewRequest("GET", u, nil)
  74. if err != nil {
  75. return nil, nil, err
  76. }
  77. // TODO: remove custom Accept header when this API fully launches.
  78. req.Header.Set("Accept", mediaTypeReactionsPreview)
  79. comment := new(IssueComment)
  80. resp, err := s.client.Do(ctx, req, comment)
  81. if err != nil {
  82. return nil, resp, err
  83. }
  84. return comment, resp, nil
  85. }
  86. // CreateComment creates a new comment on the specified issue.
  87. //
  88. // GitHub API docs: https://developer.github.com/v3/issues/comments/#create-a-comment
  89. func (s *IssuesService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *IssueComment) (*IssueComment, *Response, error) {
  90. u := fmt.Sprintf("repos/%v/%v/issues/%d/comments", owner, repo, number)
  91. req, err := s.client.NewRequest("POST", u, comment)
  92. if err != nil {
  93. return nil, nil, err
  94. }
  95. c := new(IssueComment)
  96. resp, err := s.client.Do(ctx, req, c)
  97. if err != nil {
  98. return nil, resp, err
  99. }
  100. return c, resp, nil
  101. }
  102. // EditComment updates an issue comment.
  103. // A non-nil comment.Body must be provided. Other comment fields should be left nil.
  104. //
  105. // GitHub API docs: https://developer.github.com/v3/issues/comments/#edit-a-comment
  106. func (s *IssuesService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *IssueComment) (*IssueComment, *Response, error) {
  107. u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID)
  108. req, err := s.client.NewRequest("PATCH", u, comment)
  109. if err != nil {
  110. return nil, nil, err
  111. }
  112. c := new(IssueComment)
  113. resp, err := s.client.Do(ctx, req, c)
  114. if err != nil {
  115. return nil, resp, err
  116. }
  117. return c, resp, nil
  118. }
  119. // DeleteComment deletes an issue comment.
  120. //
  121. // GitHub API docs: https://developer.github.com/v3/issues/comments/#delete-a-comment
  122. func (s *IssuesService) DeleteComment(ctx context.Context, owner string, repo string, commentID int64) (*Response, error) {
  123. u := fmt.Sprintf("repos/%v/%v/issues/comments/%d", owner, repo, commentID)
  124. req, err := s.client.NewRequest("DELETE", u, nil)
  125. if err != nil {
  126. return nil, err
  127. }
  128. return s.client.Do(ctx, req, nil)
  129. }