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