1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "strings"
12 "time"
13 )
14
15
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
39
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
45 SubjectType *string `json:"subject_type,omitempty"`
46 }
47
48 func (p PullRequestComment) String() string {
49 return Stringify(p)
50 }
51
52
53
54 type PullRequestListCommentsOptions struct {
55
56 Sort string `url:"sort,omitempty"`
57
58
59 Direction string `url:"direction,omitempty"`
60
61
62 Since time.Time `url:"since,omitempty"`
63
64 ListOptions
65 }
66
67
68
69
70
71
72
73 func (s *PullRequestsService) ListComments(ctx context.Context, owner, repo string, number int, opts *PullRequestListCommentsOptions) ([]*PullRequestComment, *Response, error) {
74 var u string
75 if number == 0 {
76 u = fmt.Sprintf("repos/%v/%v/pulls/comments", owner, repo)
77 } else {
78 u = fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
79 }
80 u, err := addOptions(u, opts)
81 if err != nil {
82 return nil, nil, err
83 }
84
85 req, err := s.client.NewRequest("GET", u, nil)
86 if err != nil {
87 return nil, nil, err
88 }
89
90
91 acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
92 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
93
94 var comments []*PullRequestComment
95 resp, err := s.client.Do(ctx, req, &comments)
96 if err != nil {
97 return nil, resp, err
98 }
99
100 return comments, resp, nil
101 }
102
103
104
105
106 func (s *PullRequestsService) GetComment(ctx context.Context, owner, repo string, commentID int64) (*PullRequestComment, *Response, error) {
107 u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
108 req, err := s.client.NewRequest("GET", u, nil)
109 if err != nil {
110 return nil, nil, err
111 }
112
113
114 acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
115 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
116
117 comment := new(PullRequestComment)
118 resp, err := s.client.Do(ctx, req, comment)
119 if err != nil {
120 return nil, resp, err
121 }
122
123 return comment, resp, nil
124 }
125
126
127
128
129 func (s *PullRequestsService) CreateComment(ctx context.Context, owner, repo string, number int, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
130 u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
131 req, err := s.client.NewRequest("POST", u, comment)
132 if err != nil {
133 return nil, nil, err
134 }
135
136 acceptHeaders := []string{mediaTypeReactionsPreview, mediaTypeMultiLineCommentsPreview}
137 req.Header.Set("Accept", strings.Join(acceptHeaders, ", "))
138
139 c := new(PullRequestComment)
140 resp, err := s.client.Do(ctx, req, c)
141 if err != nil {
142 return nil, resp, err
143 }
144
145 return c, resp, nil
146 }
147
148
149
150
151 func (s *PullRequestsService) CreateCommentInReplyTo(ctx context.Context, owner, repo string, number int, body string, commentID int64) (*PullRequestComment, *Response, error) {
152 comment := &struct {
153 Body string `json:"body,omitempty"`
154 InReplyTo int64 `json:"in_reply_to,omitempty"`
155 }{
156 Body: body,
157 InReplyTo: commentID,
158 }
159 u := fmt.Sprintf("repos/%v/%v/pulls/%d/comments", owner, repo, number)
160 req, err := s.client.NewRequest("POST", u, comment)
161 if err != nil {
162 return nil, nil, err
163 }
164
165 c := new(PullRequestComment)
166 resp, err := s.client.Do(ctx, req, c)
167 if err != nil {
168 return nil, resp, err
169 }
170
171 return c, resp, nil
172 }
173
174
175
176
177
178 func (s *PullRequestsService) EditComment(ctx context.Context, owner, repo string, commentID int64, comment *PullRequestComment) (*PullRequestComment, *Response, error) {
179 u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
180 req, err := s.client.NewRequest("PATCH", u, comment)
181 if err != nil {
182 return nil, nil, err
183 }
184
185 c := new(PullRequestComment)
186 resp, err := s.client.Do(ctx, req, c)
187 if err != nil {
188 return nil, resp, err
189 }
190
191 return c, resp, nil
192 }
193
194
195
196
197 func (s *PullRequestsService) DeleteComment(ctx context.Context, owner, repo string, commentID int64) (*Response, error) {
198 u := fmt.Sprintf("repos/%v/%v/pulls/comments/%d", owner, repo, commentID)
199 req, err := s.client.NewRequest("DELETE", u, nil)
200 if err != nil {
201 return nil, err
202 }
203 return s.client.Do(ctx, req, nil)
204 }
205
View as plain text