From ed8d76be3b5f597e994b03454d99224a196c6bc9 Mon Sep 17 00:00:00 2001 From: aaronknudtson Date: Thu, 27 Jun 2024 22:56:41 -0400 Subject: [PATCH] update swagger for explore code --- modules/structs/explore.go | 4 +- routers/api/v1/explore/code.go | 45 +++++++++++++-- routers/api/v1/swagger/explore.go | 15 +++++ services/convert/explore.go | 2 +- templates/swagger/v1_json.tmpl | 96 ++++++++++++++++++++++++++++++- 5 files changed, 152 insertions(+), 10 deletions(-) create mode 100644 routers/api/v1/swagger/explore.go diff --git a/modules/structs/explore.go b/modules/structs/explore.go index db1250cd56..abb5727baa 100644 --- a/modules/structs/explore.go +++ b/modules/structs/explore.go @@ -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"` diff --git a/routers/api/v1/explore/code.go b/routers/api/v1/explore/code.go index 2f9e2922c3..11525bddb4 100644 --- a/routers/api/v1/explore/code.go +++ b/routers/api/v1/explore/code.go @@ -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 } diff --git a/routers/api/v1/swagger/explore.go b/routers/api/v1/swagger/explore.go new file mode 100644 index 0000000000..f95c04d551 --- /dev/null +++ b/routers/api/v1/swagger/explore.go @@ -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"` +} diff --git a/services/convert/explore.go b/services/convert/explore.go index ae548ee1f8..5a2eeb08ed 100644 --- a/services/convert/explore.go +++ b/services/convert/explore.go @@ -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, diff --git a/templates/swagger/v1_json.tmpl b/templates/swagger/v1_json.tmpl index 4aa64c5376..228b44e8f2 100644 --- a/templates/swagger/v1_json.tmpl +++ b/templates/swagger/v1_json.tmpl @@ -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": [] } ] -} +} \ No newline at end of file