Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

feat: list issue comments page token (#11493)

p0ny987e1ad9584f
diff --git a/backend/api/v1/issue_service.go b/backend/api/v1/issue_service.go
index b925693078..f3e1983260 100644
--- a/backend/api/v1/issue_service.go
+++ b/backend/api/v1/issue_service.go
@@ -328,29 +328,23 @@ func (s *IssueService) ListIssues(ctx context.Context, request *v1pb.ListIssuesR
return nil, status.Errorf(codes.Internal, "failed to search issue, error: %v", err)
}
+ var nextPageToken string
if len(issues) == limitPlusOne {
- nextPageToken, err := getPageToken(limit, offset+limit)
+ pageToken, err := getPageToken(limit, offset+limit)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to get next page token, error: %v", err)
}
- converted, err := convertToIssues(ctx, s.store, issues[:limit])
- if err != nil {
- return nil, status.Errorf(codes.Internal, "failed to convert to issue, error: %v", err)
- }
- return &v1pb.ListIssuesResponse{
- Issues: converted,
- NextPageToken: nextPageToken,
- }, nil
+ nextPageToken = pageToken
+ issues = issues[:limit]
}
- // No subsequent pages.
converted, err := convertToIssues(ctx, s.store, issues)
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to convert to issue, error: %v", err)
}
return &v1pb.ListIssuesResponse{
Issues: converted,
- NextPageToken: "",
+ NextPageToken: nextPageToken,
}, nil
}
@@ -1657,7 +1651,6 @@ func (s *IssueService) ListIssueComments(ctx context.Context, request *v1pb.List
if request.PageSize < 0 {
return nil, status.Errorf(codes.InvalidArgument, fmt.Sprintf("page size must be non-negative: %d", request.PageSize))
}
- // TODO(p0ny): respect page size and page token.
_, issueUID, err := common.GetProjectIDIssueUID(request.Parent)
if err != nil {
return nil, status.Errorf(codes.InvalidArgument, err.Error())
@@ -1673,15 +1666,45 @@ func (s *IssueService) ListIssueComments(ctx context.Context, request *v1pb.List
if err := s.canUserGetIssue(ctx, issue, user); err != nil {
return nil, err
}
- issueComments, err := s.store.ListIssueComment(ctx, &store.FindIssueCommentMessage{IssueUID: &issue.UID})
+
+ limit := int(request.PageSize)
+ offset := 0
+ if request.PageToken != "" {
+ var pageToken storepb.PageToken
+ if err := unmarshalPageToken(request.PageToken, &pageToken); err != nil {
+ return nil, status.Errorf(codes.InvalidArgument, "invalid page token: %v", err)
+ }
+ offset = int(pageToken.Offset)
+ }
+ if limit == 0 {
+ limit = 10
+ }
+ if limit > 1000 {
+ limit = 1000
+ }
+ limitPlusOne := limit + 1
+
+ issueComments, err := s.store.ListIssueComment(ctx, &store.FindIssueCommentMessage{
+ IssueUID: &issue.UID,
+ Limit: &limitPlusOne,
+ Offset: &offset,
+ })
if err != nil {
return nil, status.Errorf(codes.Internal, "failed to list issue comments, err: %v", err)
}
- ics := convertToIssueComments(request.Parent, issueComments)
+ var nextPageToken string
+ if len(issueComments) == limitPlusOne {
+ pageToken, err := getPageToken(limit, offset+limit)
+ if err != nil {
+ return nil, status.Errorf(codes.Internal, "failed to get next page token, error: %v", err)
+ }
+ nextPageToken = pageToken
+ issueComments = issueComments[:limit]
+ }
return &v1pb.ListIssueCommentsResponse{
- IssueComments: ics,
- NextPageToken: "",
+ IssueComments: convertToIssueComments(request.Parent, issueComments),
+ NextPageToken: nextPageToken,
}, nil
}
diff --git a/backend/store/issue_comment.go b/backend/store/issue_comment.go
index 8e0585cf4e..9d737ce33d 100644
--- a/backend/store/issue_comment.go
+++ b/backend/store/issue_comment.go
@@ -25,6 +25,9 @@ type IssueCommentMessage struct {
type FindIssueCommentMessage struct {
IssueUID *int
+
+ Limit *int
+ Offset *int
}
func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMessage) ([]*IssueCommentMessage, error) {
@@ -35,6 +38,14 @@ func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMess
args = append(args, *v)
}
+ limitOffsetClause := ""
+ if v := find.Limit; v != nil {
+ limitOffsetClause = fmt.Sprintf(" LIMIT %d", *v)
+ }
+ if v := find.Offset; v != nil {
+ limitOffsetClause += fmt.Sprintf(" OFFSET %d", *v)
+ }
+
rows, err := s.db.db.QueryContext(ctx, fmt.Sprintf(`
SELECT
id,
@@ -48,7 +59,8 @@ func (s *Store) ListIssueComment(ctx context.Context, find *FindIssueCommentMess
issue_comment
WHERE %s
ORDER BY id ASC
- `, strings.Join(where, " AND ")), args...)
+ %s
+ `, strings.Join(where, " AND "), limitOffsetClause), args...)
if err != nil {
return nil, errors.Wrapf(err, "failed to query context")
}