mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2025-09-22 18:55:56 +00:00
fix(api): set default pagination and Link header for repoListTags
(#9201)
- Set default pagination, so the API allows cases like `?limit=1`. - Set the Link header when there are more items, but not shown because of pagination. - Resolves forgejo/forgejo#8828 Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9201 Reviewed-by: Gusted <gusted@noreply.codeberg.org> Co-authored-by: deadkittens <montage_inches78@icloud.com> Co-committed-by: deadkittens <montage_inches78@icloud.com>
This commit is contained in:
parent
1d8fb306d7
commit
6d5bdce9dd
3 changed files with 74 additions and 1 deletions
|
@ -55,6 +55,7 @@ func ListTags(ctx *context.APIContext) {
|
|||
// "$ref": "#/responses/notFound"
|
||||
|
||||
listOpts := utils.GetListOptions(ctx)
|
||||
listOpts.SetDefaultValues()
|
||||
|
||||
tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize)
|
||||
if err != nil {
|
||||
|
@ -72,7 +73,7 @@ func ListTags(ctx *context.APIContext) {
|
|||
|
||||
apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
|
||||
}
|
||||
|
||||
ctx.SetLinkHeader(total, listOpts.PageSize)
|
||||
ctx.SetTotalCountHeader(int64(total))
|
||||
ctx.JSON(http.StatusOK, &apiTags)
|
||||
}
|
||||
|
|
39
routers/api/v1/repo/tag_test.go
Normal file
39
routers/api/v1/repo/tag_test.go
Normal file
|
@ -0,0 +1,39 @@
|
|||
// Copyright 2025 The Forgejo Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"forgejo.org/models/unittest"
|
||||
"forgejo.org/services/contexttest"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
func TestListTagsSetsLinkHeader(t *testing.T) {
|
||||
unittest.PrepareTestEnv(t)
|
||||
|
||||
// limit=1 so that any repo with >=2 tags will paginate
|
||||
ctx, resp := contexttest.MockAPIContext(t, "GET /api/v1/repos/user2/repo1/tags?limit=1")
|
||||
contexttest.LoadRepo(t, ctx, 1)
|
||||
contexttest.LoadUser(t, ctx, 2)
|
||||
contexttest.LoadGitRepo(t, ctx)
|
||||
|
||||
// Ensure at least two tags exist for pagination
|
||||
commit, err := ctx.Repo.GitRepo.GetBranchCommit("master")
|
||||
require.NoError(t, err)
|
||||
_ = ctx.Repo.GitRepo.CreateTag("listtags-linkheader-a", commit.ID.String())
|
||||
_ = ctx.Repo.GitRepo.CreateTag("listtags-linkheader-b", commit.ID.String())
|
||||
|
||||
ListTags(ctx)
|
||||
|
||||
assert.Equal(t, 200, ctx.Resp.Status())
|
||||
|
||||
link := resp.Header().Get("Link")
|
||||
assert.NotEmpty(t, link, "Link header should be set for paginated responses")
|
||||
assert.Contains(t, link, "rel=\"next\"")
|
||||
assert.Contains(t, link, "page=2")
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue