From afdfedcb49c7f7868c7e04d9d0049429bd656519 Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Thu, 26 Mar 2020 21:26:09 +0800 Subject: [PATCH] Iterate the link header when listing artifact Fixes #11315 When specify no pagination in listing artifact request, the go-swagger will set the default value for them, so we need to iterate the link header to get all of artifacts Signed-off-by: Wenkai Yin --- src/replication/adapter/harbor/image_registry.go | 2 +- src/server/v2.0/handler/base.go | 9 ++++++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/replication/adapter/harbor/image_registry.go b/src/replication/adapter/harbor/image_registry.go index 2bd499c90..ebfef5701 100644 --- a/src/replication/adapter/harbor/image_registry.go +++ b/src/replication/adapter/harbor/image_registry.go @@ -149,7 +149,7 @@ func (a *adapter) listArtifacts(repository string, filters []*model.Filter) ([]* url := fmt.Sprintf("%s/api/%s/projects/%s/repositories/%s/artifacts?with_label=true", a.getURL(), api.APIVersion, project, repository) artifacts := []*artifact.Artifact{} - if err := a.client.Get(url, &artifacts); err != nil { + if err := a.client.GetAndIteratePagination(url, &artifacts); err != nil { return nil, err } var arts []*model.Artifact diff --git a/src/server/v2.0/handler/base.go b/src/server/v2.0/handler/base.go index 1e3b906ed..611a8fce0 100644 --- a/src/server/v2.0/handler/base.go +++ b/src/server/v2.0/handler/base.go @@ -135,6 +135,10 @@ func (b *BaseAPI) BuildQuery(ctx context.Context, query *string, pageNumber, pag // Links return Links based on the provided pagination information func (b *BaseAPI) Links(ctx context.Context, u *url.URL, total, pageNumber, pageSize int64) lib.Links { + var links lib.Links + if pageSize == 0 { + return links + } ul := *u // try to unescape the repository name which contains escaped slashes if escapedPath, err := url.PathUnescape(ul.Path); err == nil { @@ -142,11 +146,13 @@ func (b *BaseAPI) Links(ctx context.Context, u *url.URL, total, pageNumber, page } else { log.Errorf("failed to unescape the path %s: %v", ul.Path, err) } - var links lib.Links // prev if pageNumber > 1 && (pageNumber-1)*pageSize < total { q := ul.Query() q.Set("page", strconv.FormatInt(pageNumber-1, 10)) + // the URL may contain no "page_size", in this case the pageSize in the query is set by + // the go-swagger automatically + q.Set("page_size", strconv.FormatInt(pageSize, 10)) ul.RawQuery = q.Encode() // try to unescape the query if escapedQuery, err := url.QueryUnescape(ul.RawQuery); err == nil { @@ -164,6 +170,7 @@ func (b *BaseAPI) Links(ctx context.Context, u *url.URL, total, pageNumber, page if pageSize*pageNumber < total { q := ul.Query() q.Set("page", strconv.FormatInt(pageNumber+1, 10)) + q.Set("page_size", strconv.FormatInt(pageSize, 10)) ul.RawQuery = q.Encode() // try to unescape the query if escapedQuery, err := url.QueryUnescape(ul.RawQuery); err == nil {