This commit addresses the review feedback on PR #138: - Fixed a privilege escalation vulnerability where failed ephemeral client creation would silently fall back to the global singleton token. - Refactored forgejo.Client(ctx) to return (*forgejo.Client, error). - Updated all 100+ tool handlers to properly handle client initialization errors. - Improved token extraction to handle schemes (token/bearer) case-insensitively. - Enhanced Unit Tests (pkg/forgejo/forgejo_test.go) to verify that different concurrent requests with different tokens correctly use their respective tokens in the Authorization header. - Updated README and demos to reflect case-insensitivity support.
130 lines
4 KiB
Go
130 lines
4 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
|
|
"codeberg.org/goern/forgejo-mcp/v2/operation/params"
|
|
"codeberg.org/goern/forgejo-mcp/v2/pkg/forgejo"
|
|
"codeberg.org/goern/forgejo-mcp/v2/pkg/log"
|
|
"codeberg.org/goern/forgejo-mcp/v2/pkg/to"
|
|
|
|
forgejo_sdk "codeberg.org/mvdkleijn/forgejo-sdk/forgejo/v3"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
)
|
|
|
|
const (
|
|
CreateBranchToolName = "create_branch"
|
|
DeleteBranchToolName = "delete_branch"
|
|
ListBranchesToolName = "list_branches"
|
|
)
|
|
|
|
var (
|
|
CreateBranchTool = mcp.NewTool(
|
|
CreateBranchToolName,
|
|
mcp.WithDescription("Create branch"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)),
|
|
mcp.WithString("branch", mcp.Required(), mcp.Description(params.Branch)),
|
|
mcp.WithString("old_branch", mcp.Required(), mcp.Description(params.OldBranch)),
|
|
)
|
|
|
|
DeleteBranchTool = mcp.NewTool(
|
|
DeleteBranchToolName,
|
|
mcp.WithDescription("Delete branch"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)),
|
|
mcp.WithString("branch", mcp.Required(), mcp.Description(params.Branch)),
|
|
)
|
|
|
|
ListBranchesTool = mcp.NewTool(
|
|
ListBranchesToolName,
|
|
mcp.WithDescription("List branches"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description(params.Owner)),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description(params.Repo)),
|
|
mcp.WithNumber("page", mcp.Required(), mcp.Description(params.Page), mcp.DefaultNumber(1), mcp.Min(1)),
|
|
mcp.WithNumber("limit", mcp.Required(), mcp.Description(params.Limit), mcp.DefaultNumber(100), mcp.Min(1)),
|
|
)
|
|
)
|
|
|
|
func CreateBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called CreateBranchFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(fmt.Errorf("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(fmt.Errorf("repo is required"))
|
|
}
|
|
branch, ok := req.GetArguments()["branch"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(fmt.Errorf("branch is required"))
|
|
}
|
|
oldBranch, _ := req.GetArguments()["old_branch"].(string)
|
|
|
|
client, err := forgejo.Client(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
_, _, err = client.CreateBranch(owner, repo, forgejo_sdk.CreateBranchOption{
|
|
BranchName: branch,
|
|
OldBranchName: oldBranch,
|
|
})
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("create branch error: %v", err))
|
|
}
|
|
|
|
return mcp.NewToolResultText("Branch Created"), nil
|
|
}
|
|
|
|
func DeleteBranchFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called DeleteBranchFn")
|
|
owner, _ := req.GetArguments()["owner"].(string)
|
|
repo, _ := req.GetArguments()["repo"].(string)
|
|
branch, _ := req.GetArguments()["branch"].(string)
|
|
|
|
client, err := forgejo.Client(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
success, _, err := client.DeleteRepoBranch(owner, repo, branch)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("delete branch err: %v", err))
|
|
}
|
|
if !success {
|
|
return to.ErrorResult(fmt.Errorf("failed to delete branch (status not 204)"))
|
|
}
|
|
return to.TextResult("Delete Branch Success")
|
|
}
|
|
|
|
func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called ListBranchesFn")
|
|
owner, _ := req.GetArguments()["owner"].(string)
|
|
repo, _ := req.GetArguments()["repo"].(string)
|
|
page, _ := to.Float64(req.GetArguments()["page"])
|
|
if page == 0 {
|
|
page = 1
|
|
}
|
|
limit, _ := to.Float64(req.GetArguments()["limit"])
|
|
if limit == 0 {
|
|
limit = 100
|
|
}
|
|
|
|
opt := forgejo_sdk.ListRepoBranchesOptions{
|
|
ListOptions: forgejo_sdk.ListOptions{
|
|
Page: int(page),
|
|
PageSize: int(limit),
|
|
},
|
|
}
|
|
|
|
client, err := forgejo.Client(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(err)
|
|
}
|
|
branches, _, err := client.ListRepoBranches(owner, repo, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("list branches err: %v", err))
|
|
}
|
|
return to.TextResult(branches)
|
|
}
|