Skip to content
Merged
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
19 changes: 15 additions & 4 deletions github/repos_contents.go
Original file line number Diff line number Diff line change
Expand Up @@ -224,11 +224,13 @@ func (s *RepositoriesService) DownloadContentsWithMeta(ctx context.Context, owne
// as possible, both result types will be returned but only one will contain a
// value and the other will be nil.
//
// Note: the file path is URL path escaped for you. Slashes are left as separators.
//
// GitHub API docs: https://docs.github.com/rest/repos/contents?apiVersion=2022-11-28#get-repository-content
//
//meta:operation GET /repos/{owner}/{repo}/contents/{path}
func (s *RepositoriesService) GetContents(ctx context.Context, owner, repo, path string, opts *RepositoryContentGetOptions) (fileContent *RepositoryContent, directoryContent []*RepositoryContent, resp *Response, err error) {
escapedPath := (&url.URL{Path: strings.TrimSuffix(path, "/")}).String()
escapedPath := (&url.URL{Path: strings.TrimSuffix(path, "/")}).EscapedPath()
u := fmt.Sprintf("repos/%v/%v/contents/%v", owner, repo, escapedPath)
u, err = addOptions(u, opts)
if err != nil {
Expand Down Expand Up @@ -262,11 +264,14 @@ func (s *RepositoriesService) GetContents(ctx context.Context, owner, repo, path
// CreateFile creates a new file in a repository at the given path and returns
// the commit and file metadata.
//
// Note: the file path is URL path escaped for you. Slashes are left as separators.
//
// GitHub API docs: https://docs.github.com/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents
//
//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}).EscapedPath()
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 @@ -284,11 +289,14 @@ func (s *RepositoriesService) CreateFile(ctx context.Context, owner, repo, path
// UpdateFile updates a file in a repository at the given path and returns the
// commit and file metadata. Requires the blob SHA of the file being updated.
//
// Note: the file path is URL path escaped for you. Slashes are left as separators.
//
// GitHub API docs: https://docs.github.com/rest/repos/contents?apiVersion=2022-11-28#create-or-update-file-contents
//
//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}).EscapedPath()
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 @@ -306,11 +314,14 @@ func (s *RepositoriesService) UpdateFile(ctx context.Context, owner, repo, path
// DeleteFile deletes a file from a repository and returns the commit.
// Requires the blob SHA of the file to be deleted.
//
// Note: the file path is URL path escaped for you. Slashes are left as separators.
//
// GitHub API docs: https://docs.github.com/rest/repos/contents?apiVersion=2022-11-28#delete-a-file
//
//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}).EscapedPath()
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
72 changes: 72 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,77 @@ func TestRepositoriesService_DeleteFile(t *testing.T) {
})
}

func TestRepositoriesService_CreateUpdateDeleteFile_EscapesPath(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"},
{path: "release:v1/x", escapedPath: "release:v1/x"},
} {
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