teams_discussion_comments.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // Copyright 2018 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. )
  10. // DiscussionComment represents a GitHub dicussion in a team.
  11. type DiscussionComment struct {
  12. Author *User `json:"author,omitempty"`
  13. Body *string `json:"body,omitempty"`
  14. BodyHTML *string `json:"body_html,omitempty"`
  15. BodyVersion *string `json:"body_version,omitempty"`
  16. CreatedAt *Timestamp `json:"created_at,omitempty"`
  17. LastEditedAt *Timestamp `json:"last_edited_at,omitempty"`
  18. DiscussionURL *string `json:"discussion_url,omitempty"`
  19. HTMLURL *string `json:"html_url,omitempty"`
  20. NodeID *string `json:"node_id,omitempty"`
  21. Number *int64 `json:"number,omitempty"`
  22. UpdatedAt *Timestamp `json:"updated_at,omitempty"`
  23. URL *string `json:"url,omitempty"`
  24. }
  25. func (c DiscussionComment) String() string {
  26. return Stringify(c)
  27. }
  28. // DiscussionCommentListOptions specifies optional parameters to the
  29. // TeamServices.ListComments method.
  30. type DiscussionCommentListOptions struct {
  31. // Sorts the discussion comments by the date they were created.
  32. // Accepted values are asc and desc. Default is desc.
  33. Direction string `url:"direction,omitempty"`
  34. }
  35. // ListComments lists all comments on a team discussion.
  36. // Authenticated user must grant read:discussion scope.
  37. //
  38. // GitHub API docs: https://developer.github.com/v3/teams/discussion_comments/#list-comments
  39. func (s *TeamsService) ListComments(ctx context.Context, teamID int64, discussionNumber int, options *DiscussionCommentListOptions) ([]*DiscussionComment, *Response, error) {
  40. u := fmt.Sprintf("teams/%v/discussions/%v/comments", teamID, discussionNumber)
  41. u, err := addOptions(u, options)
  42. if err != nil {
  43. return nil, nil, err
  44. }
  45. req, err := s.client.NewRequest("GET", u, nil)
  46. if err != nil {
  47. return nil, nil, err
  48. }
  49. // TODO: remove custom Accept header when this API fully launches.
  50. req.Header.Set("Accept", mediaTypeTeamDiscussionsPreview)
  51. var comments []*DiscussionComment
  52. resp, err := s.client.Do(ctx, req, &comments)
  53. if err != nil {
  54. return nil, resp, err
  55. }
  56. return comments, resp, nil
  57. }
  58. // GetComment gets a specific comment on a team discussion.
  59. // Authenticated user must grant read:discussion scope.
  60. //
  61. // GitHub API docs: https://developer.github.com/v3/teams/discussion_comments/#get-a-single-comment
  62. func (s *TeamsService) GetComment(ctx context.Context, teamID int64, discussionNumber, commentNumber int) (*DiscussionComment, *Response, error) {
  63. u := fmt.Sprintf("teams/%v/discussions/%v/comments/%v", teamID, discussionNumber, commentNumber)
  64. req, err := s.client.NewRequest("GET", u, nil)
  65. if err != nil {
  66. return nil, nil, err
  67. }
  68. // TODO: remove custom Accept header when this API fully launches.
  69. req.Header.Set("Accept", mediaTypeTeamDiscussionsPreview)
  70. discussionComment := &DiscussionComment{}
  71. resp, err := s.client.Do(ctx, req, discussionComment)
  72. if err != nil {
  73. return nil, resp, err
  74. }
  75. return discussionComment, resp, nil
  76. }
  77. // CreateComment creates a new discussion post on a team discussion.
  78. // Authenticated user must grant write:discussion scope.
  79. //
  80. // GitHub API docs: https://developer.github.com/v3/teams/discussion_comments/#create-a-comment
  81. func (s *TeamsService) CreateComment(ctx context.Context, teamID int64, discsusionNumber int, comment DiscussionComment) (*DiscussionComment, *Response, error) {
  82. u := fmt.Sprintf("teams/%v/discussions/%v/comments", teamID, discsusionNumber)
  83. req, err := s.client.NewRequest("POST", u, comment)
  84. if err != nil {
  85. return nil, nil, err
  86. }
  87. // TODO: remove custom Accept header when this API fully launches.
  88. req.Header.Set("Accept", mediaTypeTeamDiscussionsPreview)
  89. discussionComment := &DiscussionComment{}
  90. resp, err := s.client.Do(ctx, req, discussionComment)
  91. if err != nil {
  92. return nil, resp, err
  93. }
  94. return discussionComment, resp, nil
  95. }
  96. // EditComment edits the body text of a discussion comment.
  97. // Authenticated user must grant write:discussion scope.
  98. // User is allowed to edit body of a comment only.
  99. //
  100. // GitHub API docs: https://developer.github.com/v3/teams/discussion_comments/#edit-a-comment
  101. func (s *TeamsService) EditComment(ctx context.Context, teamID int64, discussionNumber, commentNumber int, comment DiscussionComment) (*DiscussionComment, *Response, error) {
  102. u := fmt.Sprintf("teams/%v/discussions/%v/comments/%v", teamID, discussionNumber, commentNumber)
  103. req, err := s.client.NewRequest("PATCH", u, comment)
  104. if err != nil {
  105. return nil, nil, err
  106. }
  107. // TODO: remove custom Accept header when this API fully launches.
  108. req.Header.Set("Accept", mediaTypeTeamDiscussionsPreview)
  109. discussionComment := &DiscussionComment{}
  110. resp, err := s.client.Do(ctx, req, discussionComment)
  111. if err != nil {
  112. return nil, resp, err
  113. }
  114. return discussionComment, resp, nil
  115. }
  116. // DeleteComment deletes a comment on a team discussion.
  117. // Authenticated user must grant write:discussion scope.
  118. //
  119. // GitHub API docs: https://developer.github.com/v3/teams/discussion_comments/#delete-a-comment
  120. func (s *TeamsService) DeleteComment(ctx context.Context, teamID int64, discussionNumber, commentNumber int) (*Response, error) {
  121. u := fmt.Sprintf("teams/%v/discussions/%v/comments/%v", teamID, discussionNumber, commentNumber)
  122. req, err := s.client.NewRequest("DELETE", u, nil)
  123. if err != nil {
  124. return nil, err
  125. }
  126. // TODO: remove custom Accept header when this API fully launches.
  127. req.Header.Set("Accept", mediaTypeTeamDiscussionsPreview)
  128. return s.client.Do(ctx, req, nil)
  129. }