teams_discussions.go 5.5 KB

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