Skip to content

Commit 2c4725d

Browse files
forgejo-backport-actiondeadkittens
authored andcommitted
[v12.0/forgejo] fix(api): set default pagination and Link header for repoListTags (#9214)
**Backport:** https://codeberg.org/forgejo/forgejo/pulls/9201 Resolves: [8828](https://codeberg.org/forgejo/forgejo/issues/8828) ## Checklist The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org). ### Tests - I added test coverage for Go changes... - [x] in their respective `*_test.go` for unit tests. - [x] in the `tests/integration` directory if it involves interactions with a live Forgejo server. - I added test coverage for JavaScript changes... - [ ] in `web_src/js/*.test.js` if it can be unit tested. - [ ] in `tests/e2e/*.test.e2e.js` if it requires interactions with a live Forgejo server (see also the [developer guide for JavaScript testing](https://codeberg.org/forgejo/forgejo/src/branch/forgejo/tests/e2e/README.md#end-to-end-tests)). ### Documentation - [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change. - [ ] I did not document these changes and I do not expect someone else to do it. ### Release notes - [ ] I do not want this change to show in the release notes. - [ ] I want the title to show in the release notes with a link to this pull request. - [ ] I want the content of the `release-notes/<pull request number>.md` to be be used for the release notes instead of the title. <!--start release-notes-assistant--> ## Release notes <!--URL:https://codeberg.org/forgejo/forgejo--> - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/9201): <!--number 9201 --><!--line 0 --><!--description Zml4KGFwaSk6IHNldCBkZWZhdWx0IHBhZ2luYXRpb24gYW5kIExpbmsgaGVhZGVyIGZvciBgcmVwb0xpc3RUYWdzYA==-->fix(api): set default pagination and Link header for `repoListTags`<!--description--> <!--end release-notes-assistant--> Co-authored-by: deadkittens <[email protected]> Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9214 Reviewed-by: Earl Warren <[email protected]> Co-authored-by: forgejo-backport-action <[email protected]> Co-committed-by: forgejo-backport-action <[email protected]>
1 parent cb0de90 commit 2c4725d

File tree

3 files changed

+74
-1
lines changed

3 files changed

+74
-1
lines changed

routers/api/v1/repo/tag.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ func ListTags(ctx *context.APIContext) {
5555
// "$ref": "#/responses/notFound"
5656

5757
listOpts := utils.GetListOptions(ctx)
58+
listOpts.SetDefaultValues()
5859

5960
tags, total, err := ctx.Repo.GitRepo.GetTagInfos(listOpts.Page, listOpts.PageSize)
6061
if err != nil {
@@ -72,7 +73,7 @@ func ListTags(ctx *context.APIContext) {
7273

7374
apiTags[i] = convert.ToTag(ctx.Repo.Repository, tags[i])
7475
}
75-
76+
ctx.SetLinkHeader(total, listOpts.PageSize)
7677
ctx.SetTotalCountHeader(int64(total))
7778
ctx.JSON(http.StatusOK, &apiTags)
7879
}

routers/api/v1/repo/tag_test.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// Copyright 2025 The Forgejo Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repo
5+
6+
import (
7+
"testing"
8+
9+
"forgejo.org/models/unittest"
10+
"forgejo.org/services/contexttest"
11+
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestListTagsSetsLinkHeader(t *testing.T) {
17+
unittest.PrepareTestEnv(t)
18+
19+
// limit=1 so that any repo with >=2 tags will paginate
20+
ctx, resp := contexttest.MockAPIContext(t, "GET /api/v1/repos/user2/repo1/tags?limit=1")
21+
contexttest.LoadRepo(t, ctx, 1)
22+
contexttest.LoadUser(t, ctx, 2)
23+
contexttest.LoadGitRepo(t, ctx)
24+
25+
// Ensure at least two tags exist for pagination
26+
commit, err := ctx.Repo.GitRepo.GetBranchCommit("master")
27+
require.NoError(t, err)
28+
_ = ctx.Repo.GitRepo.CreateTag("listtags-linkheader-a", commit.ID.String())
29+
_ = ctx.Repo.GitRepo.CreateTag("listtags-linkheader-b", commit.ID.String())
30+
31+
ListTags(ctx)
32+
33+
assert.Equal(t, 200, ctx.Resp.Status())
34+
35+
link := resp.Header().Get("Link")
36+
assert.NotEmpty(t, link, "Link header should be set for paginated responses")
37+
assert.Contains(t, link, "rel=\"next\"")
38+
assert.Contains(t, link, "page=2")
39+
}

tests/integration/api_repo_tags_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,36 @@ func TestAPIGetTagArchiveDownloadCount(t *testing.T) {
121121
assert.Equal(t, int64(1), tagInfo.ArchiveDownloadCount.TarGz)
122122
assert.Equal(t, int64(0), tagInfo.ArchiveDownloadCount.Zip)
123123
}
124+
125+
func TestAPIGetTagsPaginated(t *testing.T) {
126+
defer tests.PrepareTestEnv(t)()
127+
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
128+
// Login as User2.
129+
session := loginUser(t, user.Name)
130+
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
131+
132+
repoName := "repo1"
133+
expectedTagName := "TagDownloadCount"
134+
135+
for i := range 5 {
136+
createNewTagUsingAPI(t, token, user.Name, repoName, expectedTagName+fmt.Sprintf("%d", i), "", "")
137+
}
138+
139+
// List tags with pagination
140+
req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/tags?limit=1", user.Name, repoName).
141+
AddTokenAuth(token)
142+
resp := MakeRequest(t, req, http.StatusOK)
143+
144+
var tags []*api.Tag
145+
DecodeJSON(t, resp, &tags)
146+
147+
assert.Len(t, tags, 1)
148+
149+
assert.Equal(t, fmt.Sprintf("%s%d", expectedTagName, 0), tags[0].Name)
150+
151+
// Check if Link header is present for pagination
152+
link := resp.Header().Get("Link")
153+
assert.NotEmpty(t, link, "Link header should be set for paginated responses")
154+
assert.Contains(t, link, "rel=\"next\"")
155+
assert.Contains(t, link, "page=2")
156+
}

0 commit comments

Comments
 (0)