Skip to content

Commit

Permalink
fix missing pagination handling when listing repositories
Browse files Browse the repository at this point in the history
  • Loading branch information
xrstf committed Feb 23, 2022
1 parent 994d4fa commit 8386b02
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions pkg/quay/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (r *Repository) Visibility() RepositoryVisibility {

type getRepositoriesReponse struct {
Repositories []Repository `json:"repositories"`
NextPage string `json:"next_page"`
}

type GetRepositoriesOptions struct {
Expand All @@ -69,20 +70,54 @@ func (o *GetRepositoriesOptions) Apply(v url.Values) url.Values {
return v
}

type internalGetRepositoriesOptions struct {
GetRepositoriesOptions

NextPage string
}

func (o *internalGetRepositoriesOptions) Apply(v url.Values) url.Values {
o.GetRepositoriesOptions.Apply(v)

if o.NextPage != "" {
v.Set("next_page", o.NextPage)
}

return v
}

type RepoByName []Repository

func (a RepoByName) Len() int { return len(a) }
func (a RepoByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a RepoByName) Less(i, j int) bool { return a[i].Name < a[j].Name }

func (c *Client) GetRepositories(ctx context.Context, options GetRepositoriesOptions) ([]Repository, error) {
response := getRepositoriesReponse{}
err := c.call(ctx, "GET", "/repository", &options, nil, &response)
opt := &internalGetRepositoriesOptions{
GetRepositoriesOptions: options,
}

result := []Repository{}

for {
response := getRepositoriesReponse{}
err := c.call(ctx, "GET", "/repository", opt, nil, &response)
if err != nil {
return nil, err
}

result = append(result, response.Repositories...)

if response.NextPage == "" {
break
}

opt.NextPage = response.NextPage
}

repositories := response.Repositories
sort.Sort(RepoByName(repositories))
sort.Sort(RepoByName(result))

return repositories, err
return result, nil
}

type CreateRepositoryOptions struct {
Expand Down

0 comments on commit 8386b02

Please sign in to comment.