Rename the fork from gitea-mcp to gitea-mcp-extended to reflect the significantly expanded tool coverage (299 vs upstream's 93 tools). - Rename Go module path and all import references - Rename binary to gitea-mcp-extended in Makefile, Dockerfile, .gitignore - Point .goreleaser.yaml gitea_urls to git.lethalbits.com - Replace release-tag workflow with goreleaser + Generic Package Registry publishing - Replace release-nightly workflow with cross-platform build + nightly package publishing - Update CLAUDE.md project description and tool count
148 lines
4.4 KiB
Go
148 lines
4.4 KiB
Go
package repo
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
|
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/gitea"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/log"
|
|
"git.lethalbits.com/lethalbits/gitea-mcp-extended/pkg/to"
|
|
|
|
gitea_sdk "code.gitea.io/sdk/gitea"
|
|
"github.com/mark3labs/mcp-go/mcp"
|
|
"github.com/mark3labs/mcp-go/server"
|
|
)
|
|
|
|
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("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to create")),
|
|
mcp.WithString("old_branch", mcp.Required(), mcp.Description("Name of the old branch to create from")),
|
|
)
|
|
|
|
DeleteBranchTool = mcp.NewTool(
|
|
DeleteBranchToolName,
|
|
mcp.WithDescription("Delete branch"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
mcp.WithString("branch", mcp.Required(), mcp.Description("Name of the branch to delete")),
|
|
)
|
|
|
|
ListBranchesTool = mcp.NewTool(
|
|
ListBranchesToolName,
|
|
mcp.WithDescription("List branches"),
|
|
mcp.WithString("owner", mcp.Required(), mcp.Description("repository owner")),
|
|
mcp.WithString("repo", mcp.Required(), mcp.Description("repository name")),
|
|
)
|
|
)
|
|
|
|
func init() {
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: CreateBranchTool,
|
|
Handler: CreateBranchFn,
|
|
})
|
|
Tool.RegisterWrite(server.ServerTool{
|
|
Tool: DeleteBranchTool,
|
|
Handler: DeleteBranchFn,
|
|
})
|
|
Tool.RegisterRead(server.ServerTool{
|
|
Tool: ListBranchesTool,
|
|
Handler: ListBranchesFn,
|
|
})
|
|
}
|
|
|
|
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(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
branch, ok := req.GetArguments()["branch"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("branch is required"))
|
|
}
|
|
oldBranch, _ := req.GetArguments()["old_branch"].(string)
|
|
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
_, _, err = client.CreateBranch(owner, repo, gitea_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, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
branch, ok := req.GetArguments()["branch"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("branch is required"))
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
_, _, err = client.DeleteRepoBranch(owner, repo, branch)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("delete branch error: %v", err))
|
|
}
|
|
|
|
return to.TextResult("Branch Deleted")
|
|
}
|
|
|
|
func ListBranchesFn(ctx context.Context, req mcp.CallToolRequest) (*mcp.CallToolResult, error) {
|
|
log.Debugf("Called ListBranchesFn")
|
|
owner, ok := req.GetArguments()["owner"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("owner is required"))
|
|
}
|
|
repo, ok := req.GetArguments()["repo"].(string)
|
|
if !ok {
|
|
return to.ErrorResult(errors.New("repo is required"))
|
|
}
|
|
opt := gitea_sdk.ListRepoBranchesOptions{
|
|
ListOptions: gitea_sdk.ListOptions{
|
|
Page: 1,
|
|
PageSize: 100,
|
|
},
|
|
}
|
|
client, err := gitea.ClientFromContext(ctx)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
|
}
|
|
branches, _, err := client.ListRepoBranches(owner, repo, opt)
|
|
if err != nil {
|
|
return to.ErrorResult(fmt.Errorf("list branches error: %v", err))
|
|
}
|
|
|
|
return to.TextResult(branches)
|
|
}
|