Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions pkg/github/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 == "" {
Expand Down
39 changes: 37 additions & 2 deletions pkg/github/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,7 @@ func Test_CreateOrUpdateFile(t *testing.T) {
}
}

tests := []struct {
type testCase struct {
name string
mockedClient *http.Client
requestArgs map[string]any
Expand All @@ -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{
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down