update swagger for explore code

This commit is contained in:
aaronknudtson 2024-06-27 22:56:41 -04:00
parent 1bf78712d4
commit ed8d76be3b
5 changed files with 152 additions and 10 deletions

View File

@ -3,7 +3,7 @@
package structs // import "code.gitea.io/gitea/modules/structs"
// ExploreCodeSearchItem A single search match
// ExploreCodeSearchItem A code search match
// swagger:model
type ExploreCodeSearchItem struct {
RepoName string `json:"repoName"`
@ -12,7 +12,7 @@ type ExploreCodeSearchItem struct {
LineText string `json:"lineText"`
}
// ExploreCodeResult all returned search results
// ExploreCodeResult all returned code search results
// swagger:model
type ExploreCodeResult struct {
Total int `json:"total"`

View File

@ -17,18 +17,43 @@ import (
// 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
// type: boolean
// responses:
// "200":
// description: "SearchResults of a successful search"
// schema:
// "$ref": "#/definitions/ExploreCodeResult"
if !setting.Indexer.RepoIndexerEnabled {
ctx.NotFound("Indexer not enabled")
return
}
language := ctx.FormTrim("l")
keyword := ctx.FormTrim("q")
isFuzzy := ctx.FormOptionalBool("fuzzy").ValueOrDefault(true)
if keyword == "" {
ctx.JSON(http.StatusInternalServerError, api.SearchError{OK: false, Error: "No keyword provided"})
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: "No keyword provided",
})
return
}
@ -50,7 +75,10 @@ func Code(ctx *context.APIContext) {
if ctx.Doer == nil || !isAdmin {
repoIDs, err = repo_model.FindUserCodeAccessibleRepoIDs(ctx, ctx.Doer)
if err != nil {
ctx.ServerError("FindUserCodeAccessibleRepoIDs", err)
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: err.Error(),
})
return
}
}
@ -67,7 +95,6 @@ func Code(ctx *context.APIContext) {
Keyword: keyword,
IsKeywordFuzzy: isFuzzy,
IsHtmlSafe: false,
Language: language,
Paginator: &db.ListOptions{
Page: page,
PageSize: setting.API.DefaultPagingNum,
@ -75,7 +102,10 @@ func Code(ctx *context.APIContext) {
})
if err != nil {
if code_indexer.IsAvailable(ctx) {
ctx.ServerError("SearchResults", err)
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: err.Error(),
})
return
}
}
@ -96,7 +126,10 @@ func Code(ctx *context.APIContext) {
repoMaps, err = repo_model.GetRepositoriesMapByIDs(ctx, loadRepoIDs)
if err != nil {
ctx.ServerError("GetRepositoriesMapByIDs", err)
ctx.JSON(http.StatusInternalServerError, api.SearchError{
OK: false,
Error: err.Error(),
})
return
}

View File

@ -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 {
// out:body
Body api.ExploreCodeResult `json:"body"`
}

View File

@ -15,7 +15,7 @@ func ToExploreCodeSearchResults(total int, results []*code_indexer.Result, repoM
if repo := repoMaps[res.RepoID]; repo != nil {
for _, r := range res.Lines {
out.Results = append(out.Results, api.ExploreCodeSearchItem{
RepoName: repo.Name,
RepoName: repo.FullName(),
FilePath: res.Filename,
LineNumber: r.Num,
LineText: r.RawContent,

View File

@ -1009,6 +1009,46 @@
}
}
},
"/explore/code": {
"get": {
"produces": [
"application/json"
],
"tags": [
"explore"
],
"summary": "Search for code",
"operationId": "codeSearch",
"parameters": [
{
"type": "string",
"description": "keyword",
"name": "q",
"in": "query"
},
{
"type": "integer",
"description": "page number of results to return (1-based)",
"name": "page",
"in": "query"
},
{
"type": "boolean",
"description": "whether to search fuzzy or strict",
"name": "fuzzy",
"in": "query"
}
],
"responses": {
"200": {
"description": "SearchResults of a successful search",
"schema": {
"$ref": "#/definitions/ExploreCodeResult"
}
}
}
}
},
"/gitignore/templates": {
"get": {
"produces": [
@ -21331,6 +21371,49 @@
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"ExploreCodeResult": {
"description": "ExploreCodeResult all returned code search results",
"type": "object",
"properties": {
"results": {
"type": "array",
"items": {
"$ref": "#/definitions/ExploreCodeSearchItem"
},
"x-go-name": "Results"
},
"total": {
"type": "integer",
"format": "int64",
"x-go-name": "Total"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"ExploreCodeSearchItem": {
"description": "ExploreCodeSearchItem A code search match",
"type": "object",
"properties": {
"lineNumber": {
"type": "integer",
"format": "int64",
"x-go-name": "LineNumber"
},
"lineText": {
"type": "string",
"x-go-name": "LineText"
},
"path": {
"type": "string",
"x-go-name": "FilePath"
},
"repoName": {
"type": "string",
"x-go-name": "RepoName"
}
},
"x-go-package": "code.gitea.io/gitea/modules/structs"
},
"ExternalTracker": {
"description": "ExternalTracker represents settings for external tracker",
"type": "object",
@ -25397,6 +25480,17 @@
"$ref": "#/definitions/APIError"
}
},
"ExploreCode": {
"description": "ExploreCode",
"schema": {
"$ref": "#/definitions/ExploreCodeResult"
},
"headers": {
"body": {
"description": "out:body"
}
}
},
"FileDeleteResponse": {
"description": "FileDeleteResponse",
"schema": {
@ -26243,4 +26337,4 @@
"TOTPHeader": []
}
]
}
}