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
9 changes: 6 additions & 3 deletions github/repos_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,8 @@ func (s *RepositoriesService) GetContents(ctx context.Context, owner, repo, path
//
//meta:operation PUT /repos/{owner}/{repo}/contents/{path}
func (s *RepositoriesService) CreateFile(ctx context.Context, owner, repo, path string, body *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, path)
escapedPath := (&url.URL{Path: path}).String()
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath)
Comment on lines +269 to +270

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a comment that path is now escaped just like we do for the other endpoints.
Here and everywhere else:

Suggested change
escapedPath := (&url.URL{Path: path}).String()
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath)
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, url.QueryEscape(path))

req, err := s.client.NewRequest(ctx, "PUT", u, body)
if err != nil {
return nil, nil, err
Expand All @@ -288,7 +289,8 @@ func (s *RepositoriesService) CreateFile(ctx context.Context, owner, repo, path
//
//meta:operation PUT /repos/{owner}/{repo}/contents/{path}
func (s *RepositoriesService) UpdateFile(ctx context.Context, owner, repo, path string, body *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, path)
escapedPath := (&url.URL{Path: path}).String()
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath)
req, err := s.client.NewRequest(ctx, "PUT", u, body)
if err != nil {
return nil, nil, err
Expand All @@ -310,7 +312,8 @@ func (s *RepositoriesService) UpdateFile(ctx context.Context, owner, repo, path
//
//meta:operation DELETE /repos/{owner}/{repo}/contents/{path}
func (s *RepositoriesService) DeleteFile(ctx context.Context, owner, repo, path string, opts *RepositoryContentFileOptions) (*RepositoryContentResponse, *Response, error) {
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, path)
escapedPath := (&url.URL{Path: path}).String()
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath)
req, err := s.client.NewRequest(ctx, "DELETE", u, opts)
if err != nil {
return nil, nil, err
Expand Down
71 changes: 71 additions & 0 deletions github/repos_contents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package github

import (
"context"
"errors"
"fmt"
"io"
Expand Down Expand Up @@ -945,6 +946,76 @@ func TestRepositoriesService_DeleteFile(t *testing.T) {
})
}

func TestRepositoriesService_filePath_isEscaped(t *testing.T) {
t.Parallel()
for _, file := range []struct {
path string
escapedPath string
}{
{path: "p", escapedPath: "p"},
{path: "some directory/file.go", escapedPath: "some%20directory/file.go"},
{path: "docs/C#.md", escapedPath: "docs/C%23.md"},
{path: "docs/why?.md", escapedPath: "docs/why%3F.md"},
{path: "docs/100%.md", escapedPath: "docs/100%25.md"},
{path: "docs/50%25.md", escapedPath: "docs/50%2525.md"},
} {
for _, tt := range []struct {
method string
wantMethod string
call func(ctx context.Context, client *Client) error
}{
{
method: "CreateFile",
wantMethod: "PUT",
call: func(ctx context.Context, client *Client) error {
_, _, err := client.Repositories.CreateFile(ctx, "o", "r", file.path, &RepositoryContentFileOptions{})
return err
},
},
{
method: "UpdateFile",
wantMethod: "PUT",
call: func(ctx context.Context, client *Client) error {
_, _, err := client.Repositories.UpdateFile(ctx, "o", "r", file.path, &RepositoryContentFileOptions{})
return err
},
},
{
method: "DeleteFile",
wantMethod: "DELETE",
call: func(ctx context.Context, client *Client) error {
_, _, err := client.Repositories.DeleteFile(ctx, "o", "r", file.path, &RepositoryContentFileOptions{})
return err
},
},
} {
t.Run(tt.method+"/"+file.path, func(t *testing.T) {
t.Parallel()
client, mux, _ := setup(t)

const wantPath = "/repos/o/r/contents/"
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, tt.wantMethod)
if got, want := r.URL.EscapedPath(), wantPath+file.escapedPath; got != want {
t.Errorf("Request path = %q, want %q", got, want)
}
if got, want := r.URL.Path, wantPath+file.path; got != want {
t.Errorf("Decoded request path = %q, want %q", got, want)
}
if got := r.URL.RawQuery; got != "" {
t.Errorf("Request query = %q, want none", got)
}
fmt.Fprint(w, `{}`)
})

if err := tt.call(t.Context(), client); err != nil {
t.Fatalf("Repositories.%v returned error: %v", tt.method, err)
}
})
}
}
}

func TestRepositoriesService_GetArchiveLink(t *testing.T) {
t.Parallel()
tcs := []struct {
Expand Down
Loading