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)
}
+2
View File
@@ -51,6 +51,7 @@ import (
release_service "code.gitea.io/gitea/services/release"
repo_service "code.gitea.io/gitea/services/repository"
"code.gitea.io/gitea/services/repository/archiver"
sourcegraph_service "code.gitea.io/gitea/services/sourcegraph"
"code.gitea.io/gitea/services/task"
"code.gitea.io/gitea/services/uinotification"
"code.gitea.io/gitea/services/webhook"
@@ -172,6 +173,7 @@ func InitWebInstalled(ctx context.Context) {
mustInitCtx(ctx, actions_service.Init)
mustInit(repo_service.InitLicenseClassifier)
mustInit(sourcegraph_service.Init)
// Finally start up the cron
cron.Init(ctx)
+10
View File
@@ -281,4 +281,14 @@ func renderBlame(ctx *context.Context, blameParts []*gitrepo.BlamePart, commitNa
ctx.Data["EscapeStatus"] = escapeStatus
ctx.Data["BlameRows"] = rows
ctx.Data["LexerName"] = lexerName
// Sourcegraph code intelligence configuration
if setting.Sourcegraph.Enabled {
ctx.Data["SourcegraphEnabled"] = true
ctx.Data["SourcegraphConfig"] = map[string]any{
"enabled": true,
"repoFullName": ctx.Repo.Repository.FullName(),
"commitID": ctx.Repo.CommitID,
}
}
}
+10
View File
@@ -87,6 +87,16 @@ func setCompareContext(ctx *context.Context, before, head *git.Commit, headOwner
setPathsCompareContext(ctx, before, head, headOwner, headName)
setImageCompareContext(ctx)
setCsvCompareContext(ctx)
// Sourcegraph code intelligence configuration
if setting.Sourcegraph.Enabled {
ctx.Data["SourcegraphEnabled"] = true
ctx.Data["SourcegraphConfig"] = map[string]any{
"enabled": true,
"repoFullName": headOwner + "/" + headName,
"commitID": head.ID.String(),
}
}
}
// SourceCommitURL creates a relative URL for a commit in the given repository
+10
View File
@@ -256,6 +256,16 @@ func prepareFileView(ctx *context.Context, entry *git.TreeEntry) {
default:
// unable to render anything, show the "view raw" or let frontend handle it
}
// Sourcegraph code intelligence configuration
if setting.Sourcegraph.Enabled {
ctx.Data["SourcegraphEnabled"] = true
ctx.Data["SourcegraphConfig"] = map[string]any{
"enabled": true,
"repoFullName": ctx.Repo.Repository.FullName(),
"commitID": ctx.Repo.CommitID,
}
}
}
func prepareFileViewEditorButtons(ctx *context.Context) bool {