feat: [CODE-2483]: skip permission check for space pullreq API and fix the inner loop (#2791)

* skip permission check for space pullreq API and fix the inner loop
pull/3571/head
Marko Gaćeša 2024-10-09 10:43:32 +00:00 committed by Harness
parent f2e382acbf
commit c628d3307d
2 changed files with 9 additions and 10 deletions

View File

@ -20,7 +20,6 @@ import (
"github.com/harness/gitness/app/auth"
"github.com/harness/gitness/types"
"github.com/harness/gitness/types/enum"
)
// ListPullReqs returns a list of pull requests from the provided space.
@ -31,11 +30,14 @@ func (c *Controller) ListPullReqs(
includeSubspaces bool,
filter *types.PullReqFilter,
) ([]types.PullReqRepo, error) {
space, err := c.getSpaceCheckAuth(ctx, session, spaceRef, enum.PermissionSpaceView)
space, err := c.spaceStore.FindByRef(ctx, spaceRef)
if err != nil {
return nil, fmt.Errorf("failed to acquire access to space: %w", err)
return nil, fmt.Errorf("space not found: %w", err)
}
// We deliberately don't check for space permission because the pull request service
// will check for repo-view permission for every returned pull request.
pullReqs, err := c.prListService.ListForSpace(ctx, session, space, includeSubspaces, filter)
if err != nil {
return nil, fmt.Errorf("failed to fetch pull requests from space: %w", err)

View File

@ -185,8 +185,9 @@ func (c *ListService) streamPullReqs(
pullReqs := make([]*types.PullReq, 0, opts.Size)
ch, chErr := c.pullreqStore.Stream(ctx, opts)
for pr := range ch {
if pr == nil {
return pullReqs, repoUnchecked, nil
if len(pullReqs) >= pullReqLimit || len(repoUnchecked) >= newRepoLimit {
cancelFn() // the loop must be exited by canceling the context
continue
}
if _, ok := repoWhitelist[pr.TargetRepoID]; !ok {
@ -194,13 +195,9 @@ func (c *ListService) streamPullReqs(
}
pullReqs = append(pullReqs, pr)
if len(pullReqs) >= pullReqLimit || len(repoUnchecked) >= newRepoLimit {
break
}
}
if err := <-chErr; err != nil {
if err := <-chErr; err != nil && !errors.Is(err, context.Canceled) {
return nil, nil, fmt.Errorf("failed to stream pull requests: %w", err)
}