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