-
-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Add API route for explore/code search #31515
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
knudtty
wants to merge
4
commits into
go-gitea:main
Choose a base branch
from
knudtty:code-search-api
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,6 +26,7 @@ type SearchOptions struct { | |
Language string | ||
|
||
IsKeywordFuzzy bool | ||
IsHTMLSafe bool | ||
|
||
db.Paginator | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package structs | ||
|
||
// ExploreCodeSearchItem A code search match | ||
// swagger:model | ||
type ExploreCodeSearchItem struct { | ||
RepoName string `json:"repoName"` | ||
FilePath string `json:"path"` | ||
LineNumber int `json:"lineNumber"` | ||
LineText string `json:"lineText"` | ||
} | ||
|
||
// ExploreCodeResult all returned code search results | ||
// swagger:model | ||
type ExploreCodeResult struct { | ||
Total int `json:"total"` | ||
Results []ExploreCodeSearchItem `json:"results"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package explore | ||
|
||
import ( | ||
"net/http" | ||
"slices" | ||
|
||
"code.gitea.io/gitea/models/db" | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
code_indexer "code.gitea.io/gitea/modules/indexer/code" | ||
"code.gitea.io/gitea/modules/setting" | ||
api "code.gitea.io/gitea/modules/structs" | ||
"code.gitea.io/gitea/services/context" | ||
"code.gitea.io/gitea/services/convert" | ||
) | ||
|
||
// Code explore code | ||
func Code(ctx *context.APIContext) { | ||
// swagger:operation GET /explore/code explore codeSearch | ||
// --- | ||
// summary: Search for code | ||
// produces: | ||
// - application/json | ||
// parameters: | ||
// - name: q | ||
// in: query | ||
// description: keyword | ||
// type: string | ||
// - name: page | ||
// in: query | ||
// description: page number of results to return (1-based) | ||
// type: integer | ||
// - name: fuzzy | ||
// in: query | ||
// description: whether to search fuzzy or strict (defaults to true) | ||
// type: boolean | ||
// responses: | ||
// "200": | ||
// description: "SearchResults of a successful search" | ||
// schema: | ||
// "$ref": "#/definitions/ExploreCodeResult" | ||
if !setting.Indexer.RepoIndexerEnabled { | ||
ctx.NotFound("Indexer not enabled") | ||
return | ||
} | ||
|
||
keyword := ctx.FormTrim("q") | ||
|
||
isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true) | ||
knudtty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if keyword == "" { | ||
ctx.JSON(http.StatusOK, api.ExploreCodeResult{ | ||
Total: 0, | ||
Results: make([]api.ExploreCodeSearchItem, 0), | ||
}) | ||
return | ||
} | ||
|
||
page := ctx.FormInt("page") | ||
if page <= 0 { | ||
page = 1 | ||
} | ||
|
||
var ( | ||
repoIDs []int64 | ||
err error | ||
isAdmin bool | ||
) | ||
if ctx.Doer != nil { | ||
isAdmin = ctx.Doer.IsAdmin | ||
} | ||
|
||
if ctx.Doer == nil || !isAdmin { | ||
repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, api.SearchError{ | ||
OK: false, | ||
Error: err.Error(), | ||
}) | ||
return | ||
} | ||
} | ||
|
||
var ( | ||
total int | ||
searchResults []*code_indexer.Result | ||
repoMaps map[int64]*repo_model.Repository | ||
) | ||
|
||
if (len(repoIDs) > 0) || isAdmin { | ||
total, searchResults, _, err = code_indexer.PerformSearch(ctx, &code_indexer.SearchOptions{ | ||
RepoIDs: repoIDs, | ||
Keyword: keyword, | ||
IsKeywordFuzzy: isFuzzy, | ||
IsHTMLSafe: false, | ||
Paginator: &db.ListOptions{ | ||
Page: page, | ||
PageSize: setting.API.DefaultPagingNum, | ||
}, | ||
}) | ||
if err != nil { | ||
if code_indexer.IsAvailable(ctx) { | ||
ctx.JSON(http.StatusInternalServerError, api.SearchError{ | ||
OK: false, | ||
Error: err.Error(), | ||
}) | ||
return | ||
} | ||
} | ||
|
||
loadRepoIDs := make([]int64, 0, len(searchResults)) | ||
for _, result := range searchResults { | ||
if !slices.Contains(loadRepoIDs, result.RepoID) { | ||
loadRepoIDs = append(loadRepoIDs, result.RepoID) | ||
} | ||
} | ||
|
||
repoMaps, err = repo_model.GetRepositoriesMapByIDs(ctx, loadRepoIDs) | ||
if err != nil { | ||
ctx.JSON(http.StatusInternalServerError, api.SearchError{ | ||
OK: false, | ||
Error: err.Error(), | ||
}) | ||
return | ||
} | ||
|
||
if len(loadRepoIDs) != len(repoMaps) { | ||
// Remove deleted repos from search results | ||
cleanedSearchResults := make([]*code_indexer.Result, 0, len(repoMaps)) | ||
for _, sr := range searchResults { | ||
if _, found := repoMaps[sr.RepoID]; found { | ||
cleanedSearchResults = append(cleanedSearchResults, sr) | ||
} | ||
} | ||
|
||
searchResults = cleanedSearchResults | ||
} | ||
} | ||
|
||
ctx.JSON(http.StatusOK, convert.ToExploreCodeSearchResults(total, searchResults, repoMaps)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package swagger | ||
|
||
import ( | ||
api "code.gitea.io/gitea/modules/structs" | ||
) | ||
|
||
// ExploreCode | ||
// swagger:response ExploreCode | ||
type swaggerResponseExploreCode struct { | ||
// in:body | ||
Body api.ExploreCodeResult `json:"body"` | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Copyright 2024 The Gitea Authors. All rights reserved. | ||
// SPDX-License-Identifier: MIT | ||
|
||
package convert | ||
|
||
import ( | ||
repo_model "code.gitea.io/gitea/models/repo" | ||
code_indexer "code.gitea.io/gitea/modules/indexer/code" | ||
api "code.gitea.io/gitea/modules/structs" | ||
) | ||
|
||
func ToExploreCodeSearchResults(total int, results []*code_indexer.Result, repoMaps map[int64]*repo_model.Repository) api.ExploreCodeResult { | ||
out := api.ExploreCodeResult{ | ||
Total: total, | ||
Results: make([]api.ExploreCodeSearchItem, 0, len(results)), | ||
} | ||
for _, res := range results { | ||
if repo := repoMaps[res.RepoID]; repo != nil { | ||
for _, r := range res.Lines { | ||
out.Results = append(out.Results, api.ExploreCodeSearchItem{ | ||
RepoName: repo.FullName(), | ||
FilePath: res.Filename, | ||
LineNumber: r.Num, | ||
LineText: r.RawContent, | ||
}) | ||
} | ||
} | ||
} | ||
return out | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't the API route be /search/code to align with the naming conventions of the GitHub API?