Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not encode / in ref names #1433

Merged
merged 4 commits into from
Feb 22, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
4 changes: 3 additions & 1 deletion github/git_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,10 +79,12 @@ func (s *GitService) GetRef(ctx context.Context, owner string, repo string, ref
return r, resp, nil
}

// refURLEscape escapes every path segment of the given ref. Those must
// not contain escaped "/" - as "%2F" - or github will not recognize it.
func refURLEscape(ref string) string {
parts := strings.Split(ref, "/")
for i, s := range parts {
parts[i] = url.QueryEscape(s)
parts[i] = url.PathEscape(s)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmmm... with this change from url.QueryEscape to url.PathEscape, I'm now concerned that we are breaking what was fixed in #1099.

Can you please add a unit test that demonstrates the URL found in #1099?

Unfortunately, I did not request that unit tests be added in #1099 to demonstrate the before/after changes. :-(
But I'm trying to fix that now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

There's one in #1118 (issue #1101). This still works, GetCommitSHA1() got the refURLEscape() also. I've added a test case for the original trailing % as given in #1099 where http.NewRequest was failing to parse the URL.

}
return strings.Join(parts, "/")
}
Expand Down
28 changes: 28 additions & 0 deletions github/git_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"fmt"
"net/http"
"reflect"
"strings"
"testing"
)

Expand Down Expand Up @@ -444,3 +445,30 @@ func TestGitService_DeleteRef(t *testing.T) {
t.Errorf("Git.DeleteRef returned error: %v", err)
}
}

func TestGitService_GetRef_pathEscape(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/repos/o/r/git/refs/heads/b", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
if strings.Contains(r.URL.RawPath, "%2F") {
t.Errorf("RawPath still contains escaped / as %%2F: %v", r.URL.RawPath)
}
fmt.Fprint(w, `
{
"ref": "refs/heads/b",
"url": "https://api.github.com/repos/o/r/git/refs/heads/b",
"object": {
"type": "commit",
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
}
}`)
})

_, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
if err != nil {
t.Fatalf("Git.GetRef returned error: %v", err)
}
}