issues_comments.go 4.5 KB

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