Skip to content

Commit c4e2700

Browse files
Paginate Comment Retrieval (#2107)
* paginate comment retrieval Co-authored-by: bismuthdev[bot] <177057995+bismuthdev[bot]@users.noreply.github.com> * add safety check to body access * Update github.go - remove comment --------- Co-authored-by: bismuthdev[bot] <177057995+bismuthdev[bot]@users.noreply.github.com>
1 parent a4d23a2 commit c4e2700

File tree

1 file changed

+41
-8
lines changed

1 file changed

+41
-8
lines changed

libs/ci/github/github.go

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -168,16 +168,49 @@ func (svc GithubService) PublishComment(prNumber int, comment string) (*ci.Comme
168168
}
169169

170170
func (svc GithubService) GetComments(prNumber int) ([]ci.Comment, error) {
171-
comments, _, err := svc.Client.Issues.ListComments(context.Background(), svc.Owner, svc.RepoName, prNumber, &github.IssueListCommentsOptions{ListOptions: github.ListOptions{PerPage: 100}})
172-
commentBodies := make([]ci.Comment, len(comments))
173-
for i, comment := range comments {
174-
commentBodies[i] = ci.Comment{
175-
Id: strconv.FormatInt(*comment.ID, 10),
176-
Body: comment.Body,
177-
Url: *comment.HTMLURL,
171+
var allComments []ci.Comment
172+
opts := &github.IssueListCommentsOptions{
173+
ListOptions: github.ListOptions{PerPage: 100},
174+
}
175+
176+
for {
177+
comments, resp, err := svc.Client.Issues.ListComments(context.Background(), svc.Owner, svc.RepoName, prNumber, opts)
178+
if err != nil {
179+
slog.Error("error getting pull request comments", "error", err, "prNumber", prNumber)
180+
return nil, fmt.Errorf("error getting pull request comments: %v", err)
181+
}
182+
183+
for _, comment := range comments {
184+
// Add nil checks to prevent potential nil pointer dereference
185+
var commentId string
186+
if comment.ID != nil {
187+
commentId = strconv.FormatInt(*comment.ID, 10)
188+
}
189+
190+
var commentUrl string
191+
if comment.HTMLURL != nil {
192+
commentUrl = *comment.HTMLURL
193+
}
194+
195+
var commentBody *string
196+
if comment.Body != nil {
197+
commentBody = comment.Body
198+
}
199+
200+
allComments = append(allComments, ci.Comment{
201+
Id: commentId,
202+
Body: commentBody,
203+
Url: commentUrl,
204+
})
178205
}
206+
207+
if resp.NextPage == 0 {
208+
break
209+
}
210+
opts.Page = resp.NextPage
179211
}
180-
return commentBodies, err
212+
213+
return allComments, nil
181214
}
182215

183216
func (svc GithubService) GetApprovals(prNumber int) ([]string, error) {

0 commit comments

Comments
 (0)