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

154 lines
5.0KB

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