mirror of https://github.com/harness/drone.git
PullReq list API: Added pagination headers (#121)
parent
6bde210adf
commit
fe9118e074
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue