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 <yinw@vmware.com>
This commit is contained in:
Wenkai Yin 2020-03-26 21:26:09 +08:00
parent 73f3a305ce
commit afdfedcb49
2 changed files with 9 additions and 2 deletions

View File

@ -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

View File

@ -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 {