From 2c4725d9afb771b1c961b33e6d37e5a72de8f78d Mon Sep 17 00:00:00 2001 From: forgejo-backport-action Date: Tue, 9 Sep 2025 07:29:08 +0200 Subject: [PATCH] [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/.md` to be be used for the release notes instead of the title. ## Release notes - Bug fixes - [PR](https://codeberg.org/forgejo/forgejo/pulls/9201): fix(api): set default pagination and Link header for `repoListTags` Co-authored-by: deadkittens Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9214 Reviewed-by: Earl Warren Co-authored-by: forgejo-backport-action Co-committed-by: forgejo-backport-action --- routers/api/v1/repo/tag.go | 3 +- routers/api/v1/repo/tag_test.go | 39 +++++++++++++++++++++++++ tests/integration/api_repo_tags_test.go | 33 +++++++++++++++++++++ 3 files changed, 74 insertions(+), 1 deletion(-) create mode 100644 routers/api/v1/repo/tag_test.go diff --git a/routers/api/v1/repo/tag.go b/routers/api/v1/repo/tag.go index f53a6da811..e47e07e8a7 100644 --- a/routers/api/v1/repo/tag.go +++ b/routers/api/v1/repo/tag.go @@ -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) } diff --git a/routers/api/v1/repo/tag_test.go b/routers/api/v1/repo/tag_test.go new file mode 100644 index 0000000000..c76b29391d --- /dev/null +++ b/routers/api/v1/repo/tag_test.go @@ -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") +} diff --git a/tests/integration/api_repo_tags_test.go b/tests/integration/api_repo_tags_test.go index 9c69ff31bb..6898a5b966 100644 --- a/tests/integration/api_repo_tags_test.go +++ b/tests/integration/api_repo_tags_test.go @@ -121,3 +121,36 @@ func TestAPIGetTagArchiveDownloadCount(t *testing.T) { assert.Equal(t, int64(1), tagInfo.ArchiveDownloadCount.TarGz) assert.Equal(t, int64(0), tagInfo.ArchiveDownloadCount.Zip) } + +func TestAPIGetTagsPaginated(t *testing.T) { + defer tests.PrepareTestEnv(t)() + user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2}) + // Login as User2. + session := loginUser(t, user.Name) + token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository) + + repoName := "repo1" + expectedTagName := "TagDownloadCount" + + for i := range 5 { + createNewTagUsingAPI(t, token, user.Name, repoName, expectedTagName+fmt.Sprintf("%d", i), "", "") + } + + // List tags with pagination + req := NewRequestf(t, "GET", "/api/v1/repos/%s/%s/tags?limit=1", user.Name, repoName). + AddTokenAuth(token) + resp := MakeRequest(t, req, http.StatusOK) + + var tags []*api.Tag + DecodeJSON(t, resp, &tags) + + assert.Len(t, tags, 1) + + assert.Equal(t, fmt.Sprintf("%s%d", expectedTagName, 0), tags[0].Name) + + // Check if Link header is present for pagination + 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") +}