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