[v12.0/forgejo] fix(code-search): fix broken pagination. (#9006)

**Backport:** https://codeberg.org/forgejo/forgejo/pulls/9000

Missing parameters for:
- repo: path and mode
- user: mode
- explore: mode

resolves forgejo/forgejo!8997 and codeberg/community!2098

<!--start release-notes-assistant-->

## Release notes
<!--URL:https://codeberg.org/forgejo/forgejo-->
- Bug fixes
  - [PR](https://codeberg.org/forgejo/forgejo/pulls/9000): <!--number 9000 --><!--line 0 --><!--description Zml4KGNvZGUtc2VhcmNoKTogZml4IGJyb2tlbiBwYWdpbmF0aW9uLg==-->fix(code-search): fix broken pagination.<!--description-->
<!--end release-notes-assistant-->

Co-authored-by: Shiny Nematoda <snematoda.751k2@aleeas.com>
Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/9006
Reviewed-by: Earl Warren <earl-warren@noreply.codeberg.org>
Co-authored-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
Co-committed-by: forgejo-backport-action <forgejo-backport-action@noreply.codeberg.org>
This commit is contained in:
forgejo-backport-action 2025-08-23 13:49:05 +02:00 committed by Earl Warren
commit 25484228e6
4 changed files with 44 additions and 2 deletions

View file

@ -147,6 +147,7 @@ func Code(ctx *context.Context) {
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParam(ctx, "l", "Language")
pager.AddParam(ctx, "mode", "CodeSearchMode")
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplExploreCode)

View file

@ -165,6 +165,8 @@ func Search(ctx *context.Context) {
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParam(ctx, "l", "Language")
pager.AddParam(ctx, "mode", "CodeSearchMode")
pager.AddParam(ctx, "path", "CodeSearchPath")
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplSearch)

View file

@ -131,6 +131,7 @@ func CodeSearch(ctx *context.Context) {
pager := context.NewPagination(total, setting.UI.RepoSearchPagingNum, page, 5)
pager.SetDefaultParams(ctx)
pager.AddParam(ctx, "l", "Language")
pager.AddParam(ctx, "mode", "CodeSearchMode")
ctx.Data["Page"] = pager
ctx.HTML(http.StatusOK, tplUserCode)

View file

@ -5,6 +5,7 @@ package integration
import (
"net/http"
"net/url"
"testing"
"forgejo.org/models/db"
@ -92,8 +93,8 @@ func testSearchRepo(t *testing.T, indexer bool) {
testSearch(t, "/user2/glob/search?q=file5&page=1&mode=exact", []string{}, indexer)
}
func testSearch(t *testing.T, url string, expected []string, indexer bool) {
req := NewRequest(t, "GET", url)
func testSearch(t *testing.T, rawURL string, expected []string, indexer bool) {
req := NewRequest(t, "GET", rawURL)
resp := MakeRequest(t, req, http.StatusOK)
doc := NewHTMLParser(t, resp.Body)
@ -119,4 +120,41 @@ func testSearch(t *testing.T, url string, expected []string, indexer bool) {
filenames := resultFilenames(t, doc)
assert.ElementsMatch(t, expected, filenames)
testSearchPagination(t, rawURL, doc)
}
// Tests that the variables set in the url persist for all the paginated links
func testSearchPagination(t *testing.T, rawURL string, doc *HTMLDoc) {
original, err := queryFromStr(rawURL)
require.NoError(t, err)
hrefs := doc.
Find(".pagination.menu a[href]:not(.disabled)").
Map(func(i int, el *goquery.Selection) string {
attr, ok := el.Attr("href")
require.True(t, ok)
return attr
})
query := make([]url.Values, len(hrefs))
for i, href := range hrefs {
query[i], err = queryFromStr(href)
require.NoError(t, err)
}
for key := range original {
for i, q := range query {
assert.Equal(t, original.Get(key), q.Get(key),
"failed at index '%d' with url '%v'", i, hrefs[i])
}
}
}
func queryFromStr(rawURL string) (url.Values, error) {
u, err := url.Parse(rawURL)
if err != nil {
return nil, err
}
return url.ParseQuery(u.RawQuery)
}