1// Copyright 2013 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	"time"
12)
13
14// PullRequestComment represents a comment left on a pull request.
15type PullRequestComment struct {
16	ID                  *int64     `json:"id,omitempty"`
17	InReplyTo           *int64     `json:"in_reply_to_id,omitempty"`
18	Body                *string    `json:"body,omitempty"`
19	Path                *string    `json:"path,omitempty"`
20	DiffHunk            *string    `json:"diff_hunk,omitempty"`
21	PullRequestReviewID *int64     `json:"pull_request_review_id,omitempty"`
22	Position            *int       `json:"position,omitempty"`
23	OriginalPosition    *int       `json:"original_position,omitempty"`
24	CommitID            *string    `json:"commit_id,omitempty"`
25	OriginalCommitID    *string    `json:"original_commit_id,omitempty"`
26	User                *User      `json:"user,omitempty"`
27	Reactions           *Reactions `json:"reactions,omitempty"`
28	CreatedAt           *time.Time `json:"created_at,omitempty"`
29	UpdatedAt           *time.Time `json:"updated_at,omitempty"`
30	// AuthorAssociation is the comment author's relationship to the pull request's repository.
31	// Possible values are "COLLABORATOR", "CONTRIBUTOR", "FIRST_TIMER", "FIRST_TIME_CONTRIBUTOR", "MEMBER", "OWNER", or "NONE".
32	AuthorAssociation *string `json:"author_association,omitempty"`
33	URL               *string `json:"url,omitempty"`
34	HTMLURL           *string `json:"html_url,omitempty"`
35	PullRequestURL    *string `json:"pull_request_url,omitempty"`
36}
37
38func (p PullRequestComment) String() string {
39	return Stringify(p)
40}
41
42// PullRequestListCommentsOptions specifies the optional parameters to the
43// PullRequestsService.ListComments method.
44type PullRequestListCommentsOptions struct {
45	// Sort specifies how to sort comments. Possible values are: created, updated.
46	Sort string `url:"sort,omitempty"`
47
48	// Direction in which to sort comments. Possible values are: asc, desc.
49	Direction string `url:"direction,omitempty"`
50
51	// Since filters comments by time.
52	Since time.Time `url:"since,omitempty"`
53
54	ListOptions
55}
56
57// ListComments lists all comments on the specified pull request. Specifying a
58// pull request number of 0 will return all comments on all pull requests for
59// the repository.
60//
61// GitHub API docs: https://developer.github.com/v3/pulls/comments/#list-comments-on-a-pull-request
62func (s *PullRequestsService) ListComments(ctx context.Context, owner string, repo string, number int, opt *PullRequestListCommentsOptions) ([]*PullRequestComment, *Response, error) {
63	var u string
64	if number == 0 {
65		u = fmt.Sprintf("repos/%v/%v/pulls/comments", owner, repo)
66	} else {
67		u = fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
68	}
69	u, err := addOptions(u, opt)
70	if err != nil {
71		return nil, nil, err
72	}
73
74	req, err := s.client.NewRequest("GET", u, nil)
75	if err != nil {
76		return nil, nil, err
77	}
78
79	// TODO: remove custom Accept header when this API fully launches.
80	req.Header.Set("Accept", mediaTypeReactionsPreview)
81
82	var comments []*PullRequestComment
83	resp, err := s.client.Do(ctx, req, &comments)
84	if err != nil {
85		return nil, resp, err
86	}
87
88	return comments, resp, nil
89}
90
91// GetComment fetches the specified pull request comment.
92//
93// GitHub API docs: https://developer.github.com/v3/pulls/comments/#get-a-single-comment
94func (s *PullRequestsService) GetComment(ctx context.Context, owner string, repo string, commentID int64) (*PullRequestComment, *Response, error) {
95	u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
96	req, err := s.client.NewRequest("GET", u, nil)
97	if err != nil {
98		return nil, nil, err
99	}
100
101	// TODO: remove custom Accept header when this API fully launches.
102	req.Header.Set("Accept", mediaTypeReactionsPreview)
103
104	comment := new(PullRequestComment)
105	resp, err := s.client.Do(ctx, req, comment)
106	if err != nil {
107		return nil, resp, err
108	}
109
110	return comment, resp, nil
111}
112
113// CreateComment creates a new comment on the specified pull request.
114//
115// GitHub API docs: https://developer.github.com/v3/pulls/comments/#create-a-comment
116func (s *PullRequestsService) CreateComment(ctx context.Context, owner string, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
117	u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
118	req, err := s.client.NewRequest("POST", u, comment)
119	if err != nil {
120		return nil, nil, err
121	}
122
123	c := new(PullRequestComment)
124	resp, err := s.client.Do(ctx, req, c)
125	if err != nil {
126		return nil, resp, err
127	}
128
129	return c, resp, nil
130}
131
132// CreateCommentInReplyTo creates a new comment as a reply to an existing pull request comment.
133//
134// GitHub API docs: https://developer.github.com/v3/pulls/comments/#alternative-input
135func (s *PullRequestsService) CreateCommentInReplyTo(ctx context.Context, owner string, repo string, number int, body string, commentID int64) (*PullRequestComment, *Response, error) {
136	comment := &struct {
137		Body      string `json:"body,omitempty"`
138		InReplyTo int64  `json:"in_reply_to,omitempty"`
139	}{
140		Body:      body,
141		InReplyTo: commentID,
142	}
143	u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
144	req, err := s.client.NewRequest("POST", u, comment)
145	if err != nil {
146		return nil, nil, err
147	}
148
149	c := new(PullRequestComment)
150	resp, err := s.client.Do(ctx, req, c)
151	if err != nil {
152		return nil, resp, err
153	}
154
155	return c, resp, nil
156}
157
158// EditComment updates a pull request comment.
159// A non-nil comment.Body must be provided. Other comment fields should be left nil.
160//
161// GitHub API docs: https://developer.github.com/v3/pulls/comments/#edit-a-comment
162func (s *PullRequestsService) EditComment(ctx context.Context, owner string, repo string, commentID int64, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
163	u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
164	req, err := s.client.NewRequest("PATCH", u, comment)
165	if err != nil {
166		return nil, nil, err
167	}
168
169	c := new(PullRequestComment)
170	resp, err := s.client.Do(ctx, req, c)
171	if err != nil {
172		return nil, resp, err
173	}
174
175	return c, resp, nil
176}
177
178// DeleteComment deletes a pull request comment.
179//
180// GitHub API docs: https://developer.github.com/v3/pulls/comments/#delete-a-comment
181func (s *PullRequestsService) DeleteComment(ctx context.Context, owner string, repo string, commentID int64) (*Response, error) {
182	u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
183	req, err := s.client.NewRequest("DELETE", u, nil)
184	if err != nil {
185		return nil, err
186	}
187	return s.client.Do(ctx, req, nil)
188}
189