Files
gitea/tests/integration/api_repo_code_search_test.go
T
Rafail Giavrimisandsilverwind 38389f00be feat: unify code search, add search API (#39403)
Adds GitHub-style code search endpoints and makes code search behave the
same in the web UI and the API, whether it runs on git grep, bleve or
Elasticsearch.

- `GET /repos/{owner}/{repo}/code/search` searches any branch, tag or
commit with git grep, so it works without the code indexer.
- `GET /search/code` searches the default branches of all accessible
repositories with the code indexer.
- One query syntax everywhere: every term must match a file's content or
path, case-insensitively. It supports `"phrases"`, `/regexp/` (git grep
only) and the `path:`, `language:`, `repo:`, `user:` and `org:`
qualifiers. Invalid queries return an error.
- All engines apply the same include, exclude, vendored and size rules,
and return every match. The web UI shows them as multiple snippets per
file with the matched text highlighted.

<img width="359" height="295" alt="image"
src="https://github.com/user-attachments/assets/59b7aa66-b1dc-4938-9ef9-66668ae08d7c"
/>

Fixes: https://github.com/go-gitea/gitea/issues/10706
Fixed: https://github.com/go-gitea/gitea/issues/15841
Fixes: https://github.com/go-gitea/gitea/issues/31375
Related: https://gitea.com/gitea/gitea-mcp/issues/280
Docs: https://gitea.com/gitea/docs/pulls/615

AI usage disclosure: Yes, I used AI to write code, review changes and
verify the factuality of the overview I provided. I take full
responsibility for the code in this PR.

---------

Co-authored-by: silverwind <me@silverwind.io>
2026-11-13 07:24:37 +00:00

159 lines
6.8 KiB
Go

// Copyright 2026 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package integration
import (
"net/http"
"testing"
auth_model "gitea.dev/models/auth"
repo_model "gitea.dev/models/repo"
"gitea.dev/models/unit"
"gitea.dev/models/unittest"
code_indexer "gitea.dev/modules/indexer/code"
"gitea.dev/modules/setting"
api "gitea.dev/modules/structs"
"gitea.dev/modules/test"
repo_service "gitea.dev/services/repository"
"gitea.dev/tests"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAPIRepoSearchCode(t *testing.T) {
defer tests.PrepareTestEnv(t)()
search := func(t *testing.T, url, token string, expectedStatus int) *api.CodeSearchResults {
t.Helper()
resp := MakeRequest(t, NewRequest(t, "GET", url).AddTokenAuth(token), expectedStatus)
if expectedStatus != http.StatusOK {
return nil
}
return DecodeJSON(t, resp, &api.CodeSearchResults{})
}
searchPaths := func(t *testing.T, url string) (paths []string) {
t.Helper()
for _, item := range search(t, url, "", http.StatusOK).Items {
paths = append(paths, item.Path)
}
return paths
}
t.Run("Query", func(t *testing.T) {
const prToUpdateCommit = "62fb502a7172d4453f0322a2cc85bddffa57f07a"
for _, c := range []struct {
query string
expected []string
}{
{"q=WoW&ref=" + prToUpdateCommit, []string{"File-WoW"}},
{"q=/Desc.*repo1/", []string{"README.md"}},
{"q=Description+path:docs&ref=sub-home-md-img-check", []string{"docs/README.md"}},
} {
assert.Equal(t, c.expected, searchPaths(t, "/api/v1/repos/user2/repo1/code/search?"+c.query), c.query)
}
})
t.Run("Invalid", func(t *testing.T) {
for _, query := range []string{"q=WoW+language:go", "q=WoW+repo:user2/repo1", "q=%22unterminated", "q=Description&page=334&limit=3"} {
search(t, "/api/v1/repos/user2/repo1/code/search?"+query, "", http.StatusUnprocessableEntity)
}
search(t, "/api/v1/repos/user2/repo1/code/search?q=WoW&ref=no-such-ref", "", http.StatusNotFound)
})
t.Run("EmptyRepo", func(t *testing.T) {
MakeRequest(t, NewRequestWithJSON(t, "POST", "/api/v1/user/repos", &api.CreateRepoOption{Name: "empty-code-search"}).AddTokenAuth(getUserToken(t, "user2", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)), http.StatusCreated)
search(t, "/api/v1/repos/user2/empty-code-search/code/search?q=Description&ref=no-such-ref", "", http.StatusNotFound)
res := search(t, "/api/v1/repos/user2/empty-code-search/code/search?q=Description", "", http.StatusOK)
assert.Zero(t, res.TotalCount)
assert.False(t, res.IncompleteResults)
assert.Equal(t, []*api.CodeSearchResultItem{}, res.Items)
})
t.Run("PrivateRepo", func(t *testing.T) {
const url = "/api/v1/repos/user2/repo16/code/search?q=signed"
search(t, url, "", http.StatusNotFound)
search(t, url, getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopePublicOnly), http.StatusNotFound)
res := search(t, url, getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository), http.StatusOK)
require.Len(t, res.Items, 1)
assert.Equal(t, "readme.md", res.Items[0].Path)
})
}
func TestAPISearchCode(t *testing.T) {
defer tests.PrepareTestEnv(t)()
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
repo16 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo16"})
code_indexer.UpdateRepoIndexer(repo1)
code_indexer.UpdateRepoIndexer(repo16)
token := getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopePublicOnly)
searchPaths := func(t *testing.T, query, token string) (paths []string) {
t.Helper()
resp := MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?"+query).AddTokenAuth(token), http.StatusOK)
for _, item := range DecodeJSON(t, resp, &api.CodeSearchResults{}).Items {
paths = append(paths, item.Repository.FullName+"/"+item.Path)
}
return paths
}
t.Run("Result", func(t *testing.T) {
resp := MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?q=Description").AddTokenAuth(token), http.StatusOK)
res := DecodeJSON(t, resp, &api.CodeSearchResults{})
assert.EqualValues(t, 1, res.TotalCount)
require.Len(t, res.Items, 1)
assert.Equal(t, "user2/repo1", res.Items[0].Repository.FullName)
assert.Equal(t, "README.md", res.Items[0].Path)
})
t.Run("Query", func(t *testing.T) {
for _, c := range []struct {
query string
expected []string
}{
{"q=Description+repo:User2/Repo1", []string{"user2/repo1/README.md"}},
{"q=Description+org:org3", nil},
{"q=Description+repo:nobody/missing+user:user2", []string{"user2/repo1/README.md"}},
{"q=Description+path:docs", nil},
{"q=Description+language:markdown", []string{"user2/repo1/README.md"}},
} {
assert.Equal(t, c.expected, searchPaths(t, c.query, token), c.query)
}
})
t.Run("Access", func(t *testing.T) {
MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?q=signed"), http.StatusUnauthorized)
assert.Equal(t, []string{"user2/repo16/readme.md"}, searchPaths(t, "q=signed", getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository)))
assert.Nil(t, searchPaths(t, "q=signed", getUserToken(t, "user2", auth_model.AccessTokenScopeReadRepository, auth_model.AccessTokenScopePublicOnly)))
assert.Equal(t, []string{"user2/repo16/readme.md"}, searchPaths(t, "q=signed", getUserToken(t, "user1", auth_model.AccessTokenScopeReadRepository)))
})
t.Run("Invalid", func(t *testing.T) {
for _, query := range []string{"q=/Description/", "q=%22unterminated", "q=x&page=334&limit=3"} {
MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?"+query).AddTokenAuth(token), http.StatusUnprocessableEntity)
}
})
t.Run("Disabled", func(t *testing.T) {
defer test.MockVariableValue(&setting.Service.Explore.DisableCodePage, true)()
MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?q=Description").AddTokenAuth(token), http.StatusNotFound)
})
t.Run("CodeUnitDisabled", func(t *testing.T) {
require.NoError(t, repo_service.UpdateRepositoryUnits(t.Context(), repo1, nil, []unit.Type{unit.TypeCode}))
resp := MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?q=Description").AddTokenAuth(token), http.StatusOK)
assert.Zero(t, DecodeJSON(t, resp, &api.CodeSearchResults{}).TotalCount)
disabledUnits := unit.DisabledRepoUnitsGet()
unit.DisabledRepoUnitsSet(append([]unit.Type{unit.TypeCode}, disabledUnits...))
defer unit.DisabledRepoUnitsSet(disabledUnits)
MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?q=Description").AddTokenAuth(token), http.StatusNotFound)
})
t.Run("TwoFactorRequired", func(t *testing.T) {
defer test.MockVariableValue(&setting.TwoFactorAuthEnforced, true)()
MakeRequest(t, NewRequest(t, "GET", "/api/v1/search/code?q=Description").AddTokenAuth(token), http.StatusForbidden)
})
}