From c68b6831e444663bce3453fd4d935cb648ee84fd Mon Sep 17 00:00:00 2001 From: Elon-Jiang <191222665+Elon-Jiang@users.noreply.github.com> Date: Wed, 18 Nov 2026 17:42:50 +0800 Subject: [PATCH] fix(repos): allow explicitly empty file content --- pkg/github/repositories.go | 12 +++++- pkg/github/repositories_test.go | 39 ++++++++++++++++++- .../repository-content-changes/SKILL.md | 4 ++ 3 files changed, 51 insertions(+), 4 deletions(-) diff --git a/pkg/github/repositories.go b/pkg/github/repositories.go index b884f4888d..32b7c5810a 100644 --- a/pkg/github/repositories.go +++ b/pkg/github/repositories.go @@ -502,7 +502,7 @@ SHA MUST be provided for existing file updates. }, }, scopes.RequireAll(scopes.Repo), - func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input CreateOrUpdateFileInput) (*mcp.CallToolResult, *MinimalFileContentResponse, error) { + func(ctx context.Context, deps ToolDependencies, req *mcp.CallToolRequest, input CreateOrUpdateFileInput) (*mcp.CallToolResult, *MinimalFileContentResponse, error) { owner := input.Owner if owner == "" { return utils.NewToolResultError("missing required parameter: owner"), nil, nil @@ -521,7 +521,15 @@ SHA MUST be provided for existing file updates. } content := input.Content if content == "" { - return utils.NewToolResultError("missing required parameter: content"), nil, nil + var args struct { + Content *string `json:"content"` + } + if err := json.Unmarshal(req.Params.Arguments, &args); err != nil { + return nil, nil, err + } + if args.Content == nil { + return utils.NewToolResultError("missing required parameter: content"), nil, nil + } } message := input.Message if message == "" { diff --git a/pkg/github/repositories_test.go b/pkg/github/repositories_test.go index 9b40c932ff..8042263f25 100644 --- a/pkg/github/repositories_test.go +++ b/pkg/github/repositories_test.go @@ -2203,7 +2203,7 @@ func Test_CreateOrUpdateFile(t *testing.T) { } } - tests := []struct { + type testCase struct { name string mockedClient *http.Client requestArgs map[string]any @@ -2213,7 +2213,8 @@ func Test_CreateOrUpdateFile(t *testing.T) { expectedErrMsgs []string unexpectedErrMsgs []string expectedRequestCount int - }{ + } + tests := []testCase{ { name: "successful file creation", mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{ @@ -2717,6 +2718,40 @@ func Test_CreateOrUpdateFile(t *testing.T) { }, } + for _, update := range []bool{false, true} { + name := "empty file creation" + args := map[string]any{"owner": "owner", "repo": "repo", "path": "docs/example.md", "content": "", "message": "Write empty file", "branch": "main"} + body := map[string]any{"content": "", "message": "Write empty file", "branch": "main"} + handlers := map[string]http.HandlerFunc{ + "GET /repos/{owner}/{repo}/contents/{path:.*}": mockResponse(t, http.StatusNotFound, map[string]any{"message": "Not Found"}), + } + count := 2 + if update { + name = "clear existing file" + args["sha"] = "abc123def456" + body["sha"] = "abc123def456" + handlers["GET /repos/{owner}/{repo}/contents/{path:.*}"] = mockResponse(t, http.StatusOK, &github.RepositoryContent{SHA: new("abc123def456"), Type: new("file")}) + handlers[GetReposGitTreesByOwnerByRepoByTree] = mockPathTree("100644") + count = 4 + } + handlers["PUT /repos/{owner}/{repo}/contents/{path:.*}"] = expectRequestBody(t, body).andThen(mockResponse(t, http.StatusOK, mockFileResponse)) + tests = append(tests, testCase{name: name, mockedClient: MockHTTPClientWithHandlers(handlers), requestArgs: args, expectedContent: mockFileResponse, expectedRequestCount: count}) + } + + for _, missing := range []bool{false, true} { + args := map[string]any{"owner": "owner", "repo": "repo", "path": "docs/example.md", "message": "Write file", "branch": "main"} + name := "missing content" + if !missing { + name = "null content" + args["content"] = nil + } + tests = append(tests, testCase{ + name: name, requestArgs: args, + mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}), + expectError: true, expectedErrMsg: "missing required parameter: content", + }) + } + for _, tc := range tests { t.Run(tc.name, func(t *testing.T) { // Setup client with mock diff --git a/pkg/skills/catalog/skills/repository-content-changes/SKILL.md b/pkg/skills/catalog/skills/repository-content-changes/SKILL.md index e4d92d34c1..65cd719d61 100644 --- a/pkg/skills/catalog/skills/repository-content-changes/SKILL.md +++ b/pkg/skills/catalog/skills/repository-content-changes/SKILL.md @@ -94,6 +94,10 @@ The rules that hold everywhere: ## Empty and failure cases +- **An intentionally empty file** — pass `content: ""` to create it or clear its + contents. The `content` field is still required; supply the current `sha` when + clearing an existing file. + - **The branch already exists** — stop. Do not write into someone else's branch on the assumption that the name was meant to be reused; ask. - **A write is rejected for a missing or stale `sha`** — the file changed underneath