Go: add context (#17354)

Signed-off-by: Jin Hai <haijin.chn@gmail.com>
This commit is contained in:
Jin Hai
2026-07-24 20:19:41 +08:00
committed by GitHub
parent 64dbc518aa
commit d9e359d481
25 changed files with 438 additions and 351 deletions

View File

@@ -17,6 +17,7 @@
package handler
import (
"context"
"fmt"
"ragflow/internal/common"
"ragflow/internal/dao"
@@ -28,15 +29,15 @@ import (
// fileCommitService is the consumer-side interface for FileCommitHandler's service dependency.
type fileCommitService interface {
CreateCommit(folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error)
ListCommits(folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error)
GetCommit(commitID string) (*entity.FileCommit, error)
ListCommitFiles(commitID string) ([]*entity.FileCommitItem, error)
DiffCommits(fromID, toID string) ([]entity.DiffEntry, error)
GetUncommittedChanges(folderID string) ([]entity.DiffEntry, error)
GetCommitTree(commitID string) (map[string]interface{}, error)
GetCommitFileContent(folderID, commitID, fileID string) ([]byte, error)
GetFileVersionHistory(fileID string) ([]entity.VersionEntry, error)
CreateCommit(ctx context.Context, folderID, authorID, message string, changes []entity.FileChange) (*entity.FileCommit, error)
ListCommits(ctx context.Context, folderID string, page, pageSize int, orderBy string, desc bool) ([]*entity.FileCommit, int64, error)
GetCommit(ctx context.Context, commitID string) (*entity.FileCommit, error)
ListCommitFiles(ctx context.Context, commitID string) ([]*entity.FileCommitItem, error)
DiffCommits(ctx context.Context, fromID, toID string) ([]entity.DiffEntry, error)
GetUncommittedChanges(ctx context.Context, folderID string) ([]entity.DiffEntry, error)
GetCommitTree(ctx context.Context, commitID string) (map[string]interface{}, error)
GetCommitFileContent(ctx context.Context, folderID, commitID, fileID string) ([]byte, error)
GetFileVersionHistory(ctx context.Context, fileID string) ([]entity.VersionEntry, error)
}
// FileCommitHandler file commit handler
@@ -57,10 +58,10 @@ func NewFileCommitHandler(commitService fileCommitService) *FileCommitHandler {
// ResolveFolderID resolves a resource ID (dataset/memory/skill) to its folder_id.
// entityType is the plural resource name (e.g. "datasets", "memories", "skills").
func (h *FileCommitHandler) ResolveFolderID(entityType, entityID string) (string, error) {
func (h *FileCommitHandler) ResolveFolderID(ctx context.Context, entityType, entityID string) (string, error) {
switch entityType {
case "datasets":
return h.resolveDatasetFolderID(entityID)
return h.resolveDatasetFolderID(ctx, entityID)
default:
return "", fmt.Errorf("unsupported entity type: %s", entityType)
}
@@ -80,7 +81,8 @@ func CommitFolderResolver(h *FileCommitHandler, entityType, urlParam string) gin
c.Abort()
return
}
folderID, err := h.ResolveFolderID(entityType, id)
ctx := c.Request.Context()
folderID, err := h.ResolveFolderID(ctx, entityType, id)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, fmt.Sprintf("%s folder not found", entityType))
c.Abort()
@@ -91,12 +93,15 @@ func CommitFolderResolver(h *FileCommitHandler, entityType, urlParam string) gin
}
}
func (h *FileCommitHandler) resolveDatasetFolderID(datasetID string) (string, error) {
func (h *FileCommitHandler) resolveDatasetFolderID(ctx context.Context, datasetID string) (string, error) {
kb, err := h.kbDAO.GetByID(datasetID)
if err != nil {
return "", err
}
files := h.fileDAO.Query(kb.Name, "", kb.TenantID)
files, err := h.fileDAO.Query(ctx, dao.DB, kb.Name, "", kb.TenantID)
if err != nil {
return "", err
}
for _, f := range files {
if f.SourceType == string(entity.FileSourceKnowledgebase) && f.Type == "folder" && f.TenantID == kb.TenantID {
return f.ID, nil
@@ -115,8 +120,6 @@ type CreateCommitRequest struct {
// @Summary Create Commit
// @Description Create a new commit with file changes for a workspace folder
// @Tags file_commit
// @Accept json
// @Produce json
// @Param folder_id path string true "workspace folder ID"
// @Param body body CreateCommitRequest true "commit request"
// @Success 200 {object} map[string]interface{}
@@ -140,7 +143,8 @@ func (h *FileCommitHandler) CreateCommit(c *gin.Context) {
return
}
commit, err := h.commitService.CreateCommit(folderID, user.ID, req.Message, req.Files)
ctx := c.Request.Context()
commit, err := h.commitService.CreateCommit(ctx, folderID, user.ID, req.Message, req.Files)
if err != nil {
jsonInternalError(c, err)
return
@@ -212,7 +216,8 @@ func (h *FileCommitHandler) ListCommits(c *gin.Context) {
desc = descStr != "false"
}
commits, total, err := h.commitService.ListCommits(folderID, page, pageSize, orderBy, desc)
ctx := c.Request.Context()
commits, total, err := h.commitService.ListCommits(ctx, folderID, page, pageSize, orderBy, desc)
if err != nil {
jsonInternalError(c, err)
return
@@ -267,7 +272,8 @@ func (h *FileCommitHandler) GetCommit(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -278,7 +284,7 @@ func (h *FileCommitHandler) GetCommit(c *gin.Context) {
return
}
items, err := h.commitService.ListCommitFiles(commitID)
items, err := h.commitService.ListCommitFiles(ctx, commitID)
if err != nil {
items = []*entity.FileCommitItem{}
}
@@ -324,7 +330,8 @@ func (h *FileCommitHandler) ListCommitFiles(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -334,7 +341,7 @@ func (h *FileCommitHandler) ListCommitFiles(c *gin.Context) {
return
}
items, err := h.commitService.ListCommitFiles(commitID)
items, err := h.commitService.ListCommitFiles(ctx, commitID)
if err != nil {
jsonInternalError(c, err)
return
@@ -369,12 +376,13 @@ func (h *FileCommitHandler) DiffCommits(c *gin.Context) {
return
}
fromCommit, err := h.commitService.GetCommit(fromID)
ctx := c.Request.Context()
fromCommit, err := h.commitService.GetCommit(ctx, fromID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
}
toCommit, err := h.commitService.GetCommit(toID)
toCommit, err := h.commitService.GetCommit(ctx, toID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -384,7 +392,7 @@ func (h *FileCommitHandler) DiffCommits(c *gin.Context) {
return
}
diff, err := h.commitService.DiffCommits(fromID, toID)
diff, err := h.commitService.DiffCommits(ctx, fromID, toID)
if err != nil {
jsonInternalError(c, err)
return
@@ -415,7 +423,8 @@ func (h *FileCommitHandler) GetUncommittedChanges(c *gin.Context) {
return
}
changes, err := h.commitService.GetUncommittedChanges(folderID)
ctx := c.Request.Context()
changes, err := h.commitService.GetUncommittedChanges(ctx, folderID)
if err != nil {
jsonInternalError(c, err)
return
@@ -448,7 +457,8 @@ func (h *FileCommitHandler) GetCommitTree(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -458,7 +468,7 @@ func (h *FileCommitHandler) GetCommitTree(c *gin.Context) {
return
}
tree, err := h.commitService.GetCommitTree(commitID)
tree, err := h.commitService.GetCommitTree(ctx, commitID)
if err != nil {
jsonInternalError(c, err)
return
@@ -494,7 +504,8 @@ func (h *FileCommitHandler) GetCommitFileContent(c *gin.Context) {
return
}
commit, err := h.commitService.GetCommit(commitID)
ctx := c.Request.Context()
commit, err := h.commitService.GetCommit(ctx, commitID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, "Commit not found")
return
@@ -504,7 +515,7 @@ func (h *FileCommitHandler) GetCommitFileContent(c *gin.Context) {
return
}
content, err := h.commitService.GetCommitFileContent(folderID, commitID, fileID)
content, err := h.commitService.GetCommitFileContent(ctx, folderID, commitID, fileID)
if err != nil {
common.ResponseWithCodeData(c, common.CodeNotFound, nil, err.Error())
return
@@ -535,7 +546,8 @@ func (h *FileCommitHandler) GetFileVersionHistory(c *gin.Context) {
return
}
versions, err := h.commitService.GetFileVersionHistory(fileID)
ctx := c.Request.Context()
versions, err := h.commitService.GetFileVersionHistory(ctx, fileID)
if err != nil {
jsonInternalError(c, err)
return