WIP add sourcegraph

This commit is contained in:
2026-02-03 19:14:07 -10:00
parent 65d93d819b
commit dfff777d04
19 changed files with 1367 additions and 3 deletions
+5
View File
@@ -1443,6 +1443,11 @@ func Routes() *web.Router {
m.Get("/issue_config/validate", context.ReferencesGitRepo(), repo.ValidateIssueConfig)
m.Get("/languages", reqRepoReader(unit.TypeCode), repo.GetLanguages)
m.Get("/licenses", reqRepoReader(unit.TypeCode), repo.GetLicenses)
m.Group("/sourcegraph", func() {
m.Get("/hover", repo.SourcegraphHover)
m.Get("/definition", repo.SourcegraphDefinition)
m.Get("/references", repo.SourcegraphReferences)
}, reqRepoReader(unit.TypeCode))
m.Get("/activities/feeds", repo.ListRepoActivityFeeds)
m.Get("/new_pin_allowed", repo.AreNewIssuePinsAllowed)
m.Group("/avatar", func() {
+259
View File
@@ -0,0 +1,259 @@
// 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)
}