Files
gitea/routers/api/v1/repo/sourcegraph.go
2026-02-03 19:14:07 -10:00

260 lines
6.3 KiB
Go

// Copyright 2025 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package repo
import (
"net/http"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/services/context"
sourcegraph_service "code.gitea.io/gitea/services/sourcegraph"
)
// SourcegraphHover returns hover information at a position
func SourcegraphHover(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/sourcegraph/hover repository repoSourcegraphHover
// ---
// summary: Get code intelligence hover info
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: path
// in: query
// description: file path
// type: string
// required: true
// - name: line
// in: query
// description: line number (0-indexed)
// type: integer
// required: true
// - name: character
// in: query
// description: character position (0-indexed)
// type: integer
// required: true
// - name: ref
// in: query
// description: git ref (commit SHA, branch, tag)
// type: string
// required: true
// responses:
// "200":
// description: hover information
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
// "503":
// description: Sourcegraph integration not available
if !setting.Sourcegraph.Enabled {
ctx.APIError(http.StatusServiceUnavailable, "Sourcegraph integration is not enabled")
return
}
path := ctx.FormString("path")
line := ctx.FormInt("line")
char := ctx.FormInt("character")
ref := ctx.FormString("ref")
if path == "" || ref == "" {
ctx.APIError(http.StatusBadRequest, "path and ref are required")
return
}
client := sourcegraph_service.GetClient()
if client == nil {
ctx.APIError(http.StatusServiceUnavailable, "Sourcegraph client not initialized")
return
}
result, err := client.Hover(ctx, ctx.Repo.Repository.FullName(), ref, path, line, char)
if err != nil {
ctx.APIError(http.StatusBadGateway, err)
return
}
if result == nil {
ctx.JSON(http.StatusOK, map[string]any{})
return
}
ctx.JSON(http.StatusOK, result)
}
// SourcegraphDefinition returns definition locations for a symbol
func SourcegraphDefinition(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/sourcegraph/definition repository repoSourcegraphDefinition
// ---
// summary: Get code intelligence definition locations
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: path
// in: query
// description: file path
// type: string
// required: true
// - name: line
// in: query
// description: line number (0-indexed)
// type: integer
// required: true
// - name: character
// in: query
// description: character position (0-indexed)
// type: integer
// required: true
// - name: ref
// in: query
// description: git ref (commit SHA, branch, tag)
// type: string
// required: true
// responses:
// "200":
// description: definition locations
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
// "503":
// description: Sourcegraph integration not available
if !setting.Sourcegraph.Enabled {
ctx.APIError(http.StatusServiceUnavailable, "Sourcegraph integration is not enabled")
return
}
path := ctx.FormString("path")
line := ctx.FormInt("line")
char := ctx.FormInt("character")
ref := ctx.FormString("ref")
if path == "" || ref == "" {
ctx.APIError(http.StatusBadRequest, "path and ref are required")
return
}
client := sourcegraph_service.GetClient()
if client == nil {
ctx.APIError(http.StatusServiceUnavailable, "Sourcegraph client not initialized")
return
}
result, err := client.Definition(ctx, ctx.Repo.Repository.FullName(), ref, path, line, char)
if err != nil {
ctx.APIError(http.StatusBadGateway, err)
return
}
if result == nil {
result = []sourcegraph_service.Location{}
}
ctx.JSON(http.StatusOK, result)
}
// SourcegraphReferences returns reference locations for a symbol
func SourcegraphReferences(ctx *context.APIContext) {
// swagger:operation GET /repos/{owner}/{repo}/sourcegraph/references repository repoSourcegraphReferences
// ---
// summary: Get code intelligence reference locations
// produces:
// - application/json
// parameters:
// - name: owner
// in: path
// description: owner of the repo
// type: string
// required: true
// - name: repo
// in: path
// description: name of the repo
// type: string
// required: true
// - name: path
// in: query
// description: file path
// type: string
// required: true
// - name: line
// in: query
// description: line number (0-indexed)
// type: integer
// required: true
// - name: character
// in: query
// description: character position (0-indexed)
// type: integer
// required: true
// - name: ref
// in: query
// description: git ref (commit SHA, branch, tag)
// type: string
// required: true
// responses:
// "200":
// description: reference locations
// "400":
// "$ref": "#/responses/error"
// "404":
// "$ref": "#/responses/notFound"
// "503":
// description: Sourcegraph integration not available
if !setting.Sourcegraph.Enabled {
ctx.APIError(http.StatusServiceUnavailable, "Sourcegraph integration is not enabled")
return
}
path := ctx.FormString("path")
line := ctx.FormInt("line")
char := ctx.FormInt("character")
ref := ctx.FormString("ref")
if path == "" || ref == "" {
ctx.APIError(http.StatusBadRequest, "path and ref are required")
return
}
client := sourcegraph_service.GetClient()
if client == nil {
ctx.APIError(http.StatusServiceUnavailable, "Sourcegraph client not initialized")
return
}
result, err := client.References(ctx, ctx.Repo.Repository.FullName(), ref, path, line, char)
if err != nil {
ctx.APIError(http.StatusBadGateway, err)
return
}
if result == nil {
result = []sourcegraph_service.Location{}
}
ctx.JSON(http.StatusOK, result)
}