Skip to content

Commit c68b683

Browse files
committed
fix(repos): allow explicitly empty file content
1 parent 4e6e6c0 commit c68b683

3 files changed

Lines changed: 51 additions & 4 deletions

File tree

‎pkg/github/repositories.go‎

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,7 @@ SHA MUST be provided for existing file updates.
502502
},
503503
},
504504
scopes.RequireAll(scopes.Repo),
505-
func(ctx context.Context, deps ToolDependencies, _ *mcp.CallToolRequest, input CreateOrUpdateFileInput) (*mcp.CallToolResult, *MinimalFileContentResponse, error) {
505+
func(ctx context.Context, deps ToolDependencies, req *mcp.CallToolRequest, input CreateOrUpdateFileInput) (*mcp.CallToolResult, *MinimalFileContentResponse, error) {
506506
owner := input.Owner
507507
if owner == "" {
508508
return utils.NewToolResultError("missing required parameter: owner"), nil, nil
@@ -521,7 +521,15 @@ SHA MUST be provided for existing file updates.
521521
}
522522
content := input.Content
523523
if content == "" {
524-
return utils.NewToolResultError("missing required parameter: content"), nil, nil
524+
var args struct {
525+
Content *string `json:"content"`
526+
}
527+
if err := json.Unmarshal(req.Params.Arguments, &args); err != nil {
528+
return nil, nil, err
529+
}
530+
if args.Content == nil {
531+
return utils.NewToolResultError("missing required parameter: content"), nil, nil
532+
}
525533
}
526534
message := input.Message
527535
if message == "" {

‎pkg/github/repositories_test.go‎

Lines changed: 37 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2203,7 +2203,7 @@ func Test_CreateOrUpdateFile(t *testing.T) {
22032203
}
22042204
}
22052205

2206-
tests := []struct {
2206+
type testCase struct {
22072207
name string
22082208
mockedClient *http.Client
22092209
requestArgs map[string]any
@@ -2213,7 +2213,8 @@ func Test_CreateOrUpdateFile(t *testing.T) {
22132213
expectedErrMsgs []string
22142214
unexpectedErrMsgs []string
22152215
expectedRequestCount int
2216-
}{
2216+
}
2217+
tests := []testCase{
22172218
{
22182219
name: "successful file creation",
22192220
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
@@ -2717,6 +2718,40 @@ func Test_CreateOrUpdateFile(t *testing.T) {
27172718
},
27182719
}
27192720

2721+
for _, update := range []bool{false, true} {
2722+
name := "empty file creation"
2723+
args := map[string]any{"owner": "owner", "repo": "repo", "path": "docs/example.md", "content": "", "message": "Write empty file", "branch": "main"}
2724+
body := map[string]any{"content": "", "message": "Write empty file", "branch": "main"}
2725+
handlers := map[string]http.HandlerFunc{
2726+
"GET /repos/{owner}/{repo}/contents/{path:.*}": mockResponse(t, http.StatusNotFound, map[string]any{"message": "Not Found"}),
2727+
}
2728+
count := 2
2729+
if update {
2730+
name = "clear existing file"
2731+
args["sha"] = "abc123def456"
2732+
body["sha"] = "abc123def456"
2733+
handlers["GET /repos/{owner}/{repo}/contents/{path:.*}"] = mockResponse(t, http.StatusOK, &github.RepositoryContent{SHA: new("abc123def456"), Type: new("file")})
2734+
handlers[GetReposGitTreesByOwnerByRepoByTree] = mockPathTree("100644")
2735+
count = 4
2736+
}
2737+
handlers["PUT /repos/{owner}/{repo}/contents/{path:.*}"] = expectRequestBody(t, body).andThen(mockResponse(t, http.StatusOK, mockFileResponse))
2738+
tests = append(tests, testCase{name: name, mockedClient: MockHTTPClientWithHandlers(handlers), requestArgs: args, expectedContent: mockFileResponse, expectedRequestCount: count})
2739+
}
2740+
2741+
for _, missing := range []bool{false, true} {
2742+
args := map[string]any{"owner": "owner", "repo": "repo", "path": "docs/example.md", "message": "Write file", "branch": "main"}
2743+
name := "missing content"
2744+
if !missing {
2745+
name = "null content"
2746+
args["content"] = nil
2747+
}
2748+
tests = append(tests, testCase{
2749+
name: name, requestArgs: args,
2750+
mockedClient: MockHTTPClientWithHandlers(map[string]http.HandlerFunc{}),
2751+
expectError: true, expectedErrMsg: "missing required parameter: content",
2752+
})
2753+
}
2754+
27202755
for _, tc := range tests {
27212756
t.Run(tc.name, func(t *testing.T) {
27222757
// Setup client with mock

‎pkg/skills/catalog/skills/repository-content-changes/SKILL.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ The rules that hold everywhere:
9494

9595
## Empty and failure cases
9696

97+
- **An intentionally empty file** — pass `content: ""` to create it or clear its
98+
contents. The `content` field is still required; supply the current `sha` when
99+
clearing an existing file.
100+
97101
- **The branch already exists** — stop. Do not write into someone else's branch on the
98102
assumption that the name was meant to be reused; ask.
99103
- **A write is rejected for a missing or stale `sha`** — the file changed underneath

0 commit comments

Comments
 (0)