mirror of
https://gitea.com/gitea/gitea-mcp.git
synced 2026-08-25 09:37:44 +00:00
feat(repo): add rename_branch tool (#245)
Adds a native `rename_branch` write tool so MCP clients can rename branches without emulating it via create + delete. Changes: - Add `rename_branch` to the repository branch tools with `owner`, `repo`, `branch`, and `new_name` parameters. - Call the Gitea SDK's `RenameRepoBranch` operation and propagate API errors. - Add handler/registration tests and README tool table entries. Closes https://gitea.com/gitea/gitea-mcp/issues/236 Assisted by Codet Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/245 Reviewed-by: silverwind <2021+silverwind@noreply.gitea.com>
This commit is contained in:
@@ -180,6 +180,7 @@ Once configured, try `list all my repositories` in the chat box.
|
||||
| create_branch | branch | Write | Create a new branch |
|
||||
| delete_branch | branch | Write | Delete a branch |
|
||||
| list_branches | branch | Read | List repository branches |
|
||||
| rename_branch | branch | Write | Rename a branch |
|
||||
| create_tag | tag | Write | Create a tag |
|
||||
| delete_tag | tag | Write | Delete a tag |
|
||||
| get_tag | tag | Read | Get tag details |
|
||||
|
||||
@@ -180,6 +180,7 @@ Cursor 等客户端可使用 stdio 命令:
|
||||
| create_branch | branch | 写入 | 创建新分支 |
|
||||
| delete_branch | branch | 写入 | 删除分支 |
|
||||
| list_branches | branch | 读取 | 列出仓库分支 |
|
||||
| rename_branch | branch | 写入 | 重命名分支 |
|
||||
| create_tag | tag | 写入 | 创建标签 |
|
||||
| delete_tag | tag | 写入 | 删除标签 |
|
||||
| get_tag | tag | 读取 | 获取标签详情 |
|
||||
|
||||
@@ -180,6 +180,7 @@ Cursor 等客戶端可使用 stdio 命令:
|
||||
| create_branch | branch | 寫入 | 創建新分支 |
|
||||
| delete_branch | branch | 寫入 | 刪除分支 |
|
||||
| list_branches | branch | 讀取 | 列出倉庫分支 |
|
||||
| rename_branch | branch | 寫入 | 重新命名分支 |
|
||||
| create_tag | tag | 寫入 | 創建標籤 |
|
||||
| delete_tag | tag | 寫入 | 刪除標籤 |
|
||||
| get_tag | tag | 讀取 | 取得標籤詳情 |
|
||||
|
||||
@@ -2,6 +2,7 @@ package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/annotation"
|
||||
@@ -21,6 +22,7 @@ const (
|
||||
CreateBranchToolName = "create_branch"
|
||||
DeleteBranchToolName = "delete_branch"
|
||||
ListBranchesToolName = "list_branches"
|
||||
RenameBranchToolName = "rename_branch"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -52,6 +54,16 @@ var (
|
||||
tool.Number("page", tool.Description(params.PageDesc), tool.Default(1)),
|
||||
tool.Number("per_page", tool.Description(params.PaginationDesc), tool.Default(30)),
|
||||
)
|
||||
|
||||
RenameBranchTool = tool.NewDefinition(
|
||||
RenameBranchToolName,
|
||||
"Rename an existing branch in a repository.",
|
||||
annotation.Write("Rename a branch"),
|
||||
tool.String("owner", tool.Required(), tool.Description(params.OwnerDesc)),
|
||||
tool.String("repo", tool.Required(), tool.Description(params.RepoDesc)),
|
||||
tool.String("branch", tool.Required()),
|
||||
tool.String("new_name", tool.Required()),
|
||||
)
|
||||
)
|
||||
|
||||
func init() {
|
||||
@@ -67,6 +79,10 @@ func init() {
|
||||
Tool: ListBranchesTool,
|
||||
Handler: ListBranchesFn,
|
||||
})
|
||||
BranchTool.RegisterWrite(tool.ServerTool{
|
||||
Tool: RenameBranchTool,
|
||||
Handler: RenameBranchFn,
|
||||
})
|
||||
}
|
||||
|
||||
func CreateBranchFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
||||
@@ -149,3 +165,36 @@ func ListBranchesFn(ctx context.Context, args map[string]any) (*mcp.CallToolResu
|
||||
|
||||
return to.TextResult(slimBranches(branches))
|
||||
}
|
||||
|
||||
func RenameBranchFn(ctx context.Context, args map[string]any) (*mcp.CallToolResult, error) {
|
||||
owner, err := params.GetString(args, "owner")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
repo, err := params.GetString(args, "repo")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
branch, err := params.GetString(args, "branch")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
newName, err := params.GetString(args, "new_name")
|
||||
if err != nil {
|
||||
return to.ErrorResult(err)
|
||||
}
|
||||
|
||||
client, err := gitea.ClientFromContext(ctx)
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("get gitea client err: %v", err))
|
||||
}
|
||||
successful, _, err := client.Repositories.RenameRepoBranch(ctx, owner, repo, branch, gitea_sdk.RenameRepoBranchOption{Name: newName})
|
||||
if err != nil {
|
||||
return to.ErrorResult(fmt.Errorf("rename branch error: %v", err))
|
||||
}
|
||||
if !successful {
|
||||
return to.ErrorResult(errors.New("rename branch error: unsuccessful"))
|
||||
}
|
||||
|
||||
return to.TextResult("Branch renamed")
|
||||
}
|
||||
|
||||
@@ -0,0 +1,136 @@
|
||||
package repo
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
|
||||
"gitea.com/gitea/gitea-mcp/pkg/flag"
|
||||
|
||||
"github.com/modelcontextprotocol/go-sdk/mcp"
|
||||
)
|
||||
|
||||
func TestRenameBranchFnMissingArgs(t *testing.T) {
|
||||
fullArgs := map[string]any{
|
||||
"owner": "octo",
|
||||
"repo": "demo",
|
||||
"branch": "old-name",
|
||||
"new_name": "new-name",
|
||||
}
|
||||
|
||||
for _, missing := range []string{"owner", "repo", "branch", "new_name"} {
|
||||
t.Run(missing, func(t *testing.T) {
|
||||
args := map[string]any{}
|
||||
for k, v := range fullArgs {
|
||||
if k != missing {
|
||||
args[k] = v
|
||||
}
|
||||
}
|
||||
|
||||
result, err := RenameBranchFn(context.Background(), args)
|
||||
if err != nil {
|
||||
t.Fatalf("RenameBranchFn() error = %v", err)
|
||||
}
|
||||
if !result.IsError {
|
||||
t.Fatalf("RenameBranchFn() with missing %q, want error result", missing)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameBranchFn(t *testing.T) {
|
||||
const (
|
||||
owner = "octo"
|
||||
repo = "demo"
|
||||
branch = "old-name"
|
||||
newName = "new-name"
|
||||
)
|
||||
|
||||
for _, tc := range []struct {
|
||||
name string
|
||||
serverStatus int
|
||||
wantErr bool
|
||||
wantContains string
|
||||
}{
|
||||
{"success", http.StatusNoContent, false, "Branch renamed"},
|
||||
{"server error", http.StatusInternalServerError, true, "rename branch error"},
|
||||
} {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
var (
|
||||
mu sync.Mutex
|
||||
gotBody map[string]any
|
||||
)
|
||||
|
||||
renamePath := fmt.Sprintf("/api/v1/repos/%s/%s/branches/%s", owner, repo, branch)
|
||||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
switch r.URL.Path {
|
||||
case "/api/v1/version":
|
||||
_, _ = w.Write([]byte(`{"version":"1.24.0"}`))
|
||||
case renamePath:
|
||||
mu.Lock()
|
||||
_ = json.NewDecoder(r.Body).Decode(&gotBody)
|
||||
mu.Unlock()
|
||||
w.WriteHeader(tc.serverStatus)
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
}
|
||||
}))
|
||||
defer server.Close()
|
||||
|
||||
origHost, origToken := flag.Host, flag.Token
|
||||
flag.Host, flag.Token = server.URL, ""
|
||||
defer func() { flag.Host, flag.Token = origHost, origToken }()
|
||||
|
||||
result, err := RenameBranchFn(context.Background(), map[string]any{
|
||||
"owner": owner,
|
||||
"repo": repo,
|
||||
"branch": branch,
|
||||
"new_name": newName,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatalf("RenameBranchFn() error = %v", err)
|
||||
}
|
||||
|
||||
if result.IsError != tc.wantErr {
|
||||
t.Fatalf("RenameBranchFn() IsError = %v, want %v (result: %v)", result.IsError, tc.wantErr, result)
|
||||
}
|
||||
|
||||
text := result.Content[0].(*mcp.TextContent).Text
|
||||
if !strings.Contains(text, tc.wantContains) {
|
||||
t.Fatalf("result = %s, want it to contain %q", text, tc.wantContains)
|
||||
}
|
||||
|
||||
if !tc.wantErr {
|
||||
mu.Lock()
|
||||
defer mu.Unlock()
|
||||
if gotBody["name"] != newName {
|
||||
t.Fatalf("request body name = %v, want %s", gotBody["name"], newName)
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestRenameBranchToolRegistration(t *testing.T) {
|
||||
found := false
|
||||
for _, registered := range BranchTool.WriteTools() {
|
||||
if registered.Tool.Name == RenameBranchToolName {
|
||||
found = true
|
||||
break
|
||||
}
|
||||
}
|
||||
if !found {
|
||||
t.Fatalf("%q is not registered as a write tool", RenameBranchToolName)
|
||||
}
|
||||
|
||||
for _, registered := range BranchTool.ReadTools() {
|
||||
if registered.Tool.Name == RenameBranchToolName {
|
||||
t.Fatalf("%q is registered as a read tool, want write only", RenameBranchToolName)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user