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