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
3 changes: 2 additions & 1 deletion pkg/github/repositories.go
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,8 @@ SHA MUST be provided for existing file updates.
// If file not found, no previous SHA needed (new file creation)
}

fileContent, resp, err := client.Repositories.CreateFile(ctx, owner, repo, path, opts)
// CreateFile puts path into the URL as is, unlike GetContents.
fileContent, resp, err := client.Repositories.CreateFile(ctx, owner, repo, escapeGitTreeish(path), opts)
if err != nil {
return ghErrors.NewGitHubAPIErrorResponse(ctx,
"failed to create/update file",
Expand Down
33 changes: 33 additions & 0 deletions pkg/github/repositories_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6823,3 +6823,36 @@ func Test_ListRepositoryCollaborators(t *testing.T) {
})
}
}

func Test_CreateOrUpdateFile_EscapesPath(t *testing.T) {
serverTool := CreateOrUpdateFile(translations.NullTranslationHelper)

var putPath string
mockedClient := MockHTTPClientWithHandlers(map[string]http.HandlerFunc{
"": func(w http.ResponseWriter, r *http.Request) {
if r.Method == http.MethodPut {
putPath = r.URL.EscapedPath()
mockResponse(t, http.StatusCreated, &github.RepositoryContentResponse{
Content: &github.RepositoryContent{Path: new("docs/C#/intro?.md")},
})(w, r)
return
}
mockResponse(t, http.StatusNotFound, map[string]string{"message": "Not Found"})(w, r)
},
})
deps := BaseDeps{Client: mustNewGHClient(t, mockedClient)}
handler := serverTool.Handler(deps)

request := createMCPRequest(map[string]any{
"owner": "owner",
"repo": "repo",
"path": "docs/C#/intro?.md",
"content": "# Intro",
"message": "Add intro",
"branch": "main",
})
result, err := handler(ContextWithDeps(context.Background(), deps), &request)
require.NoError(t, err)
require.False(t, result.IsError, getTextResult(t, result).Text)
assert.Equal(t, "/repos/owner/repo/contents/docs/C%23/intro%3F.md", putPath)
}