PullReq list API: Added pagination headers (#121)

jobatzil/rename
Marko Gaćeša 2022-12-09 17:02:45 +01:00 committed by GitHub
parent 6bde210adf
commit fe9118e074
2 changed files with 22 additions and 6 deletions

View File

@ -6,6 +6,7 @@ package pullreq
import (
"context"
"fmt"
"github.com/harness/gitness/internal/auth"
"github.com/harness/gitness/types"
@ -18,10 +19,10 @@ func (c *Controller) List(
session *auth.Session,
repoRef string,
filter *types.PullReqFilter,
) ([]*types.PullReq, error) {
) ([]*types.PullReq, int64, error) {
repo, err := c.getRepoCheckAccess(ctx, session, repoRef, enum.PermissionRepoView)
if err != nil {
return nil, err
return nil, 0, fmt.Errorf("failed to check access to target repo: %w", err)
}
if filter.SourceRepoRef == repoRef {
@ -30,10 +31,24 @@ func (c *Controller) List(
var sourceRepo *types.Repository
sourceRepo, err = c.getRepoCheckAccess(ctx, session, filter.SourceRepoRef, enum.PermissionRepoView)
if err != nil {
return nil, err
return nil, 0, fmt.Errorf("failed to check access to source repo: %w", err)
}
filter.SourceRepoID = sourceRepo.ID
}
return c.pullreqStore.List(ctx, repo.ID, filter)
list, err := c.pullreqStore.List(ctx, repo.ID, filter)
if err != nil {
return nil, 0, fmt.Errorf("failed to list pull requests: %w", err)
}
if filter.Page == 1 && len(list) < filter.Size {
return list, int64(len(list)), nil
}
count, err := c.pullreqStore.Count(ctx, repo.ID, filter)
if err != nil {
return nil, 0, fmt.Errorf("failed to count pull requests: %w", err)
}
return list, count, nil
}

View File

@ -35,12 +35,13 @@ func HandleList(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
filter.Order = enum.OrderDesc
}
prs, err := pullreqCtrl.List(ctx, session, repoRef, filter)
list, total, err := pullreqCtrl.List(ctx, session, repoRef, filter)
if err != nil {
render.TranslatedUserError(w, err)
return
}
render.JSON(w, http.StatusOK, prs)
render.Pagination(r, w, filter.Page, filter.Size, int(total))
render.JSON(w, http.StatusOK, list)
}
}