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

162 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. // RepositoryComment represents a comment for a commit, file, or line in a repository.
  12. type RepositoryComment struct {
  13. HTMLURL *string `json:"html_url,omitempty"`
  14. URL *string `json:"url,omitempty"`
  15. ID *int64 `json:"id,omitempty"`
  16. CommitID *string `json:"commit_id,omitempty"`
  17. User *User `json:"user,omitempty"`
  18. Reactions *Reactions `json:"reactions,omitempty"`
  19. CreatedAt *time.Time `json:"created_at,omitempty"`
  20. UpdatedAt *time.Time `json:"updated_at,omitempty"`
  21. // User-mutable fields
  22. Body *string `json:"body"`
  23. // User-initialized fields
  24. Path *string `json:"path,omitempty"`
  25. Position *int `json:"position,omitempty"`
  26. }
  27. func (r RepositoryComment) String() string {
  28. return Stringify(r)
  29. }
  30. // ListComments lists all the comments for the repository.
  31. //
  32. // GitHub API docs: https://developer.github.com/v3/repos/comments/#list-commit-comments-for-a-repository
  33. func (s *RepositoriesService) ListComments(ctx context.Context, owner, repo string, opt *ListOptions) ([]*RepositoryComment, *Response, error) {
  34. u := fmt.Sprintf("repos/%v/%v/comments", owner, repo)
  35. u, err := addOptions(u, opt)
  36. if err != nil {
  37. return nil, nil, err
  38. }
  39. req, err := s.client.NewRequest("GET", u, nil)
  40. if err != nil {
  41. return nil, nil, err
  42. }
  43. // TODO: remove custom Accept header when this API fully launches.
  44. req.Header.Set("Accept", mediaTypeReactionsPreview)
  45. var comments []*RepositoryComment
  46. resp, err := s.client.Do(ctx, req, &comments)
  47. if err != nil {
  48. return nil, resp, err
  49. }
  50. return comments, resp, nil
  51. }
  52. // ListCommitComments lists all the comments for a given commit SHA.
  53. //
  54. // GitHub API docs: https://developer.github.com/v3/repos/comments/#list-comments-for-a-single-commit
  55. func (s *RepositoriesService) ListCommitComments(ctx context.Context, owner, repo, sha string, opt *ListOptions) ([]*RepositoryComment, *Response, error) {
  56. u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
  57. u, err := addOptions(u, opt)
  58. if err != nil {
  59. return nil, nil, err
  60. }
  61. req, err := s.client.NewRequest("GET", u, nil)
  62. if err != nil {
  63. return nil, nil, err
  64. }
  65. // TODO: remove custom Accept header when this API fully launches.
  66. req.Header.Set("Accept", mediaTypeReactionsPreview)
  67. var comments []*RepositoryComment
  68. resp, err := s.client.Do(ctx, req, &comments)
  69. if err != nil {
  70. return nil, resp, err
  71. }
  72. return comments, resp, nil
  73. }
  74. // CreateComment creates a comment for the given commit.
  75. // Note: GitHub allows for comments to be created for non-existing files and positions.
  76. //
  77. // GitHub API docs: https://developer.github.com/v3/repos/comments/#create-a-commit-comment
  78. func (s *RepositoriesService) CreateComment(ctx context.Context, owner, repo, sha string, comment *RepositoryComment) (*RepositoryComment, *Response, error) {
  79. u := fmt.Sprintf("repos/%v/%v/commits/%v/comments", owner, repo, sha)
  80. req, err := s.client.NewRequest("POST", u, comment)
  81. if err != nil {
  82. return nil, nil, err
  83. }
  84. c := new(RepositoryComment)
  85. resp, err := s.client.Do(ctx, req, c)
  86. if err != nil {
  87. return nil, resp, err
  88. }
  89. return c, resp, nil
  90. }
  91. // GetComment gets a single comment from a repository.
  92. //
  93. // GitHub API docs: https://developer.github.com/v3/repos/comments/#get-a-single-commit-comment
  94. func (s *RepositoriesService) GetComment(ctx context.Context, owner, repo string, id int64) (*RepositoryComment, *Response, error) {
  95. u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
  96. req, err := s.client.NewRequest("GET", u, nil)
  97. if err != nil {
  98. return nil, nil, err
  99. }
  100. // TODO: remove custom Accept header when this API fully launches.
  101. req.Header.Set("Accept", mediaTypeReactionsPreview)
  102. c := new(RepositoryComment)
  103. resp, err := s.client.Do(ctx, req, c)
  104. if err != nil {
  105. return nil, resp, err
  106. }
  107. return c, resp, nil
  108. }
  109. // UpdateComment updates the body of a single comment.
  110. //
  111. // GitHub API docs: https://developer.github.com/v3/repos/comments/#update-a-commit-comment
  112. func (s *RepositoriesService) UpdateComment(ctx context.Context, owner, repo string, id int64, comment *RepositoryComment) (*RepositoryComment, *Response, error) {
  113. u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
  114. req, err := s.client.NewRequest("PATCH", u, comment)
  115. if err != nil {
  116. return nil, nil, err
  117. }
  118. c := new(RepositoryComment)
  119. resp, err := s.client.Do(ctx, req, c)
  120. if err != nil {
  121. return nil, resp, err
  122. }
  123. return c, resp, nil
  124. }
  125. // DeleteComment deletes a single comment from a repository.
  126. //
  127. // GitHub API docs: https://developer.github.com/v3/repos/comments/#delete-a-commit-comment
  128. func (s *RepositoriesService) DeleteComment(ctx context.Context, owner, repo string, id int64) (*Response, error) {
  129. u := fmt.Sprintf("repos/%v/%v/comments/%v", owner, repo, id)
  130. req, err := s.client.NewRequest("DELETE", u, nil)
  131. if err != nil {
  132. return nil, err
  133. }
  134. return s.client.Do(ctx, req, nil)
  135. }
上海开阖软件有限公司 沪ICP备12045867号-1