gists_comments.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. // GistComment represents a Gist comment.
  12. type GistComment struct {
  13. ID *int64 `json:"id,omitempty"`
  14. URL *string `json:"url,omitempty"`
  15. Body *string `json:"body,omitempty"`
  16. User *User `json:"user,omitempty"`
  17. CreatedAt *time.Time `json:"created_at,omitempty"`
  18. }
  19. func (g GistComment) String() string {
  20. return Stringify(g)
  21. }
  22. // ListComments lists all comments for a gist.
  23. //
  24. // GitHub API docs: https://developer.github.com/v3/gists/comments/#list-comments-on-a-gist
  25. func (s *GistsService) ListComments(ctx context.Context, gistID string, opt *ListOptions) ([]*GistComment, *Response, error) {
  26. u := fmt.Sprintf("gists/%v/comments", gistID)
  27. u, err := addOptions(u, opt)
  28. if err != nil {
  29. return nil, nil, err
  30. }
  31. req, err := s.client.NewRequest("GET", u, nil)
  32. if err != nil {
  33. return nil, nil, err
  34. }
  35. var comments []*GistComment
  36. resp, err := s.client.Do(ctx, req, &comments)
  37. if err != nil {
  38. return nil, resp, err
  39. }
  40. return comments, resp, nil
  41. }
  42. // GetComment retrieves a single comment from a gist.
  43. //
  44. // GitHub API docs: https://developer.github.com/v3/gists/comments/#get-a-single-comment
  45. func (s *GistsService) GetComment(ctx context.Context, gistID string, commentID int64) (*GistComment, *Response, error) {
  46. u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID)
  47. req, err := s.client.NewRequest("GET", u, nil)
  48. if err != nil {
  49. return nil, nil, err
  50. }
  51. c := new(GistComment)
  52. resp, err := s.client.Do(ctx, req, c)
  53. if err != nil {
  54. return nil, resp, err
  55. }
  56. return c, resp, nil
  57. }
  58. // CreateComment creates a comment for a gist.
  59. //
  60. // GitHub API docs: https://developer.github.com/v3/gists/comments/#create-a-comment
  61. func (s *GistsService) CreateComment(ctx context.Context, gistID string, comment *GistComment) (*GistComment, *Response, error) {
  62. u := fmt.Sprintf("gists/%v/comments", gistID)
  63. req, err := s.client.NewRequest("POST", u, comment)
  64. if err != nil {
  65. return nil, nil, err
  66. }
  67. c := new(GistComment)
  68. resp, err := s.client.Do(ctx, req, c)
  69. if err != nil {
  70. return nil, resp, err
  71. }
  72. return c, resp, nil
  73. }
  74. // EditComment edits an existing gist comment.
  75. //
  76. // GitHub API docs: https://developer.github.com/v3/gists/comments/#edit-a-comment
  77. func (s *GistsService) EditComment(ctx context.Context, gistID string, commentID int64, comment *GistComment) (*GistComment, *Response, error) {
  78. u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID)
  79. req, err := s.client.NewRequest("PATCH", u, comment)
  80. if err != nil {
  81. return nil, nil, err
  82. }
  83. c := new(GistComment)
  84. resp, err := s.client.Do(ctx, req, c)
  85. if err != nil {
  86. return nil, resp, err
  87. }
  88. return c, resp, nil
  89. }
  90. // DeleteComment deletes a gist comment.
  91. //
  92. // GitHub API docs: https://developer.github.com/v3/gists/comments/#delete-a-comment
  93. func (s *GistsService) DeleteComment(ctx context.Context, gistID string, commentID int64) (*Response, error) {
  94. u := fmt.Sprintf("gists/%v/comments/%v", gistID, commentID)
  95. req, err := s.client.NewRequest("DELETE", u, nil)
  96. if err != nil {
  97. return nil, err
  98. }
  99. return s.client.Do(ctx, req, nil)
  100. }