Skip to content

Commit 4579bc4

Browse files
authored
fix: Escape refs in commit, tag and archive paths (#4733)
1 parent 87b7b3f commit 4579bc4

4 files changed

Lines changed: 92 additions & 4 deletions

File tree

‎github/repos_commits.go‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,14 @@ func (s *RepositoriesService) ListCommits(ctx context.Context, owner, repo strin
150150

151151
// GetCommit fetches the specified commit, including all details about it.
152152
//
153+
// The sha parameter can be a commit SHA, a branch name, or a tag name.
154+
// It is URL path escaped for you. Slashes are left as separators.
155+
//
153156
// GitHub API docs: https://docs.github.com/rest/commits/commits?apiVersion=2022-11-28#get-a-commit
154157
//
155158
//meta:operation GET /repos/{owner}/{repo}/commits/{ref}
156159
func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha string, opts *ListOptions) (*RepositoryCommit, *Response, error) {
157-
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
160+
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, refURLEscape(sha))
158161
u, err := addOptions(u, opts)
159162
if err != nil {
160163
return nil, nil, err
@@ -176,11 +179,14 @@ func (s *RepositoriesService) GetCommit(ctx context.Context, owner, repo, sha st
176179

177180
// GetCommitRaw fetches the specified commit in raw (diff or patch) format.
178181
//
182+
// The sha parameter can be a commit SHA, a branch name, or a tag name.
183+
// It is URL path escaped for you. Slashes are left as separators.
184+
//
179185
// GitHub API docs: https://docs.github.com/rest/commits/commits?apiVersion=2022-11-28#get-a-commit
180186
//
181187
//meta:operation GET /repos/{owner}/{repo}/commits/{ref}
182188
func (s *RepositoriesService) GetCommitRaw(ctx context.Context, owner, repo, sha string, opts RawOptions) (string, *Response, error) {
183-
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, sha)
189+
u := fmt.Sprintf("repos/%v/%v/commits/%v", owner, repo, refURLEscape(sha))
184190
req, err := s.client.NewRequest(ctx, "GET", u, nil)
185191
if err != nil {
186192
return "", nil, err

‎github/repos_commits_test.go‎

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package github
77

88
import (
9+
"context"
910
"fmt"
1011
"net/http"
1112
"net/url"
@@ -685,3 +686,80 @@ func TestRepositoriesService_ListBranchesHeadCommit(t *testing.T) {
685686
return resp, err
686687
})
687688
}
689+
690+
func TestRepositoriesService_refIsEscaped(t *testing.T) {
691+
t.Parallel()
692+
for _, ref := range []struct {
693+
name string
694+
escapedRef string
695+
}{
696+
{name: "feature/x", escapedRef: "feature/x"},
697+
{name: "fix#12", escapedRef: "fix%2312"},
698+
{name: "what?", escapedRef: "what%3F"},
699+
{name: "100%", escapedRef: "100%25"},
700+
{name: "a b", escapedRef: "a%20b"},
701+
{name: "50%25", escapedRef: "50%2525"},
702+
} {
703+
for _, tt := range []struct {
704+
method string
705+
wantPath string
706+
call func(ctx context.Context, client *Client) error
707+
}{
708+
{
709+
method: "GetCommit",
710+
wantPath: "/repos/o/r/commits/",
711+
call: func(ctx context.Context, client *Client) error {
712+
_, _, err := client.Repositories.GetCommit(ctx, "o", "r", ref.name, nil)
713+
return err
714+
},
715+
},
716+
{
717+
method: "GetCommitRaw",
718+
wantPath: "/repos/o/r/commits/",
719+
call: func(ctx context.Context, client *Client) error {
720+
_, _, err := client.Repositories.GetCommitRaw(ctx, "o", "r", ref.name, RawOptions{Type: Diff})
721+
return err
722+
},
723+
},
724+
{
725+
method: "GetReleaseByTag",
726+
wantPath: "/repos/o/r/releases/tags/",
727+
call: func(ctx context.Context, client *Client) error {
728+
_, _, err := client.Repositories.GetReleaseByTag(ctx, "o", "r", ref.name)
729+
return err
730+
},
731+
},
732+
{
733+
method: "GetArchiveLink",
734+
wantPath: "/repos/o/r/tarball/",
735+
call: func(ctx context.Context, client *Client) error {
736+
_, _, err := client.Repositories.GetArchiveLink(ctx, "o", "r", Tarball, &RepositoryContentGetOptions{Ref: ref.name}, 0)
737+
return err
738+
},
739+
},
740+
} {
741+
t.Run(tt.method+"/"+ref.name, func(t *testing.T) {
742+
t.Parallel()
743+
client, mux, _ := setup(t)
744+
745+
mux.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
746+
if got, want := r.URL.EscapedPath(), tt.wantPath+ref.escapedRef; got != want {
747+
t.Errorf("Repositories.%v requested path %q, want %q", tt.method, got, want)
748+
}
749+
if r.URL.RawQuery != "" {
750+
t.Errorf("Repositories.%v requested query %q, want none", tt.method, r.URL.RawQuery)
751+
}
752+
if tt.method == "GetArchiveLink" {
753+
http.Redirect(w, r, "https://github.com/a", http.StatusFound)
754+
return
755+
}
756+
fmt.Fprint(w, `{}`)
757+
})
758+
759+
if err := tt.call(t.Context(), client); err != nil {
760+
t.Errorf("Repositories.%v returned error: %v", tt.method, err)
761+
}
762+
})
763+
}
764+
}
765+
}

‎github/repos_contents.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ const (
351351
// repository. The archiveFormat can be specified by either the github.Tarball
352352
// or github.Zipball constant.
353353
//
354+
// Note: the opts.Ref is URL path escaped for you. Slashes are left as separators.
355+
//
354356
// GitHub API docs: https://docs.github.com/rest/repos/contents?apiVersion=2022-11-28#download-a-repository-archive-tar
355357
//
356358
// GitHub API docs: https://docs.github.com/rest/repos/contents?apiVersion=2022-11-28#download-a-repository-archive-zip
@@ -360,7 +362,7 @@ const (
360362
func (s *RepositoriesService) GetArchiveLink(ctx context.Context, owner, repo string, archiveformat ArchiveFormat, opts *RepositoryContentGetOptions, maxRedirects int) (*url.URL, *Response, error) {
361363
u := fmt.Sprintf("repos/%v/%v/%v", owner, repo, archiveformat)
362364
if opts != nil && opts.Ref != "" {
363-
u += fmt.Sprintf("/%v", opts.Ref)
365+
u += fmt.Sprintf("/%v", refURLEscape(opts.Ref))
364366
}
365367

366368
if s.client.rateLimitRedirectionalEndpoints {

‎github/repos_releases.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,11 +140,13 @@ func (s *RepositoriesService) GetLatestRelease(ctx context.Context, owner, repo
140140

141141
// GetReleaseByTag fetches a release with the specified tag.
142142
//
143+
// Note: the tag is URL path escaped for you. Slashes are left as separators.
144+
//
143145
// GitHub API docs: https://docs.github.com/rest/releases/releases?apiVersion=2022-11-28#get-a-release-by-tag-name
144146
//
145147
//meta:operation GET /repos/{owner}/{repo}/releases/tags/{tag}
146148
func (s *RepositoriesService) GetReleaseByTag(ctx context.Context, owner, repo, tag string) (*RepositoryRelease, *Response, error) {
147-
u := fmt.Sprintf("repos/%v/%v/releases/tags/%v", owner, repo, tag)
149+
u := fmt.Sprintf("repos/%v/%v/releases/tags/%v", owner, repo, refURLEscape(tag))
148150
return s.getSingleRelease(ctx, u)
149151
}
150152

0 commit comments

Comments
 (0)