Files
silverwind 7d22bc125b fix: honor new_branch_name on the file update path (#228)
The update path of `create_or_update_file` built `UpdateFileOptions` without `NewBranchName`, so a call asking for a new branch committed straight to the base branch and still reported success.

The success text now names the branch that got the commit.

Fixes: https://gitea.com/gitea/gitea-mcp/issues/217
Co-authored-by: bircni <bircni@icloud.com>
Reviewed-on: https://gitea.com/gitea/gitea-mcp/pulls/228
Reviewed-by: bircni <bircni@icloud.com>
Co-authored-by: silverwind <2021+silverwind@noreply.gitea.com>
2026-08-09 10:10:58 +00:00

82 lines
2.0 KiB
Go

package repo
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"sync"
"testing"
"gitea.com/gitea/gitea-mcp/pkg/flag"
"github.com/modelcontextprotocol/go-sdk/mcp"
)
func TestCreateOrUpdateFileFnNewBranch(t *testing.T) {
const (
owner = "octo"
repo = "demo"
filePath = "README.md"
baseBranch = "main"
newBranch = "feature-x"
)
var (
mu sync.Mutex
gotBody map[string]any
)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
var body map[string]any
_ = json.NewDecoder(r.Body).Decode(&body)
mu.Lock()
gotBody = body
mu.Unlock()
_, _ = w.Write([]byte(`{}`))
}))
defer server.Close()
origHost, origToken := flag.Host, flag.Token
flag.Host, flag.Token = server.URL, ""
defer func() { flag.Host, flag.Token = origHost, origToken }()
for _, tc := range []struct {
name string
sha string
wantResult string
}{
{"create", "", "Create file success on branch " + newBranch},
{"update", "blobsha", "Update file success on branch " + newBranch},
} {
t.Run(tc.name, func(t *testing.T) {
result, err := CreateOrUpdateFileFn(context.Background(), map[string]any{
"owner": owner,
"repo": repo,
"path": filePath,
"content": "hello",
"message": "update readme",
"branch_name": baseBranch,
"new_branch_name": newBranch,
"sha": tc.sha,
})
if err != nil {
t.Fatalf("CreateOrUpdateFileFn() error = %v", err)
}
mu.Lock()
defer mu.Unlock()
if gotBody["new_branch"] != newBranch {
t.Fatalf("new_branch = %v, want %s", gotBody["new_branch"], newBranch)
}
if gotBody["branch"] != baseBranch {
t.Fatalf("branch = %v, want %s", gotBody["branch"], baseBranch)
}
if text := result.Content[0].(*mcp.TextContent).Text; !strings.Contains(text, tc.wantResult) {
t.Fatalf("result = %s, want it to contain %q", text, tc.wantResult)
}
})
}
}