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

Escape special characters in ref name for update ref #2454

Merged
merged 2 commits into from Sep 12, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion github/git_refs.go
Expand Up @@ -142,7 +142,7 @@ func (s *GitService) CreateRef(ctx context.Context, owner string, repo string, r
// GitHub API docs: https://docs.github.com/en/rest/git/refs#update-a-reference
func (s *GitService) UpdateRef(ctx context.Context, owner string, repo string, ref *Reference, force bool) (*Reference, *Response, error) {
refPath := strings.TrimPrefix(*ref.Ref, "refs/")
u := fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, refPath)
u := fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, refURLEscape(refPath))
req, err := s.client.NewRequest("PATCH", u, &updateRefRequest{
SHA: ref.Object.SHA,
Force: &force,
Expand Down
52 changes: 52 additions & 0 deletions github/git_refs_test.go
Expand Up @@ -624,6 +624,58 @@ func TestGitService_GetRef_pathEscape(t *testing.T) {
})
}

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

args := &updateRefRequest{
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
Force: Bool(true),
}

mux.HandleFunc("/repos/o/r/git/refs/heads/b#1", func(w http.ResponseWriter, r *http.Request) {
v := new(updateRefRequest)
json.NewDecoder(r.Body).Decode(v)

testMethod(t, r, "PATCH")
if !cmp.Equal(v, args) {
t.Errorf("Request body = %+v, want %+v", v, args)
}
fmt.Fprint(w, `
{
"ref": "refs/heads/b#1",
"url": "https://api.github.com/repos/o/r/git/refs/heads/b%231",
"object": {
"type": "commit",
"sha": "aa218f56b14c9653891f9e74264a383fa43fefbd",
"url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"
}
}`)
})

ctx := context.Background()
ref, _, err := client.Git.UpdateRef(ctx, "o", "r", &Reference{
Ref: String("refs/heads/b#1"),
Object: &GitObject{SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd")},
}, true)
if err != nil {
t.Errorf("Git.UpdateRef returned error: %v", err)
}

want := &Reference{
Ref: String("refs/heads/b#1"),
URL: String("https://api.github.com/repos/o/r/git/refs/heads/b%231"),
Object: &GitObject{
Type: String("commit"),
SHA: String("aa218f56b14c9653891f9e74264a383fa43fefbd"),
URL: String("https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd"),
},
}
if !cmp.Equal(ref, want) {
t.Errorf("Git.UpdateRef returned %+v, want %+v", ref, want)
}
}

func TestReference_Marshal(t *testing.T) {
testJSONMarshal(t, &Reference{}, "{}")

Expand Down