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