diff --git a/github/git_refs.go b/github/git_refs.go index 883975cc0f..e839c30f66 100644 --- a/github/git_refs.go +++ b/github/git_refs.go @@ -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, diff --git a/github/git_refs_test.go b/github/git_refs_test.go index 834748a232..d37504945d 100644 --- a/github/git_refs_test.go +++ b/github/git_refs_test.go @@ -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{}, "{}")