Skip to content

Commit

Permalink
Consistent errors for GetRef when a Ref doesn't exist (#1207)
Browse files Browse the repository at this point in the history
Fixes #1208.
  • Loading branch information
pilbot authored and gmlewis committed Jul 8, 2019
1 parent 7ce0678 commit 035bec2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
5 changes: 4 additions & 1 deletion github/git_refs.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ func (s *GitService) GetRef(ctx context.Context, owner string, repo string, ref
resp, err := s.client.Do(ctx, req, r)
if _, ok := err.(*json.UnmarshalTypeError); ok {
// Multiple refs, means there wasn't an exact match.
return nil, resp, errors.New("no exact match found for this ref")
return nil, resp, errors.New("multiple matches found for this ref")
} else if resp.StatusCode == 404 {
// No ref, there was no match for the ref
return nil, resp, errors.New("no match found for this ref")
} else if err != nil {
return nil, resp, err
}
Expand Down
19 changes: 18 additions & 1 deletion github/git_refs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,23 @@ func TestGitService_GetRef_singleRef(t *testing.T) {
}
}

func TestGitService_GetRef_noRefs(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")
w.WriteHeader(http.StatusNotFound)
fmt.Fprint(w, "[]")
})

_, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
want := "no match found for this ref"
if err.Error() != want {
t.Errorf("Git.GetRef returned %+v, want %+v", err, want)
}
}

func TestGitService_GetRef_multipleRefs(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down Expand Up @@ -87,7 +104,7 @@ func TestGitService_GetRef_multipleRefs(t *testing.T) {
})

_, _, err := client.Git.GetRef(context.Background(), "o", "r", "refs/heads/b")
want := "no exact match found for this ref"
want := "multiple matches found for this ref"
if err.Error() != want {
t.Errorf("Git.GetRef returned %+v, want %+v", err, want)
}
Expand Down

0 comments on commit 035bec2

Please sign in to comment.