From cc2d2c1c98dd97f0614912debdafe8afd9d714eb Mon Sep 17 00:00:00 2001 From: Will Norris Date: Fri, 11 Apr 2014 08:50:42 -0700 Subject: [PATCH] add options to Git.ListRefs this is a breaking change, but this was only recently added so I'm not too worried. This also seems the best way to handle sub-namespaces on this method. --- github/git_refs.go | 17 ++++++++++++++--- github/git_refs_test.go | 37 +++++++++++++++++++++++++++++-------- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/github/git_refs.go b/github/git_refs.go index f863fec..280464a 100644 --- a/github/git_refs.go +++ b/github/git_refs.go @@ -62,17 +62,28 @@ func (s *GitService) GetRef(owner string, repo string, ref string) (*Reference, return r, resp, err } +// ReferenceListOptions specifies optional parameters to the +// GitService.ListRefs method. +type ReferenceListOptions struct { + Type string `url:"-"` +} + // ListRefs lists all refs in a repository. // // GitHub API docs: http://developer.github.com/v3/git/refs/#get-all-references -func (s *GitService) ListRefs(owner string, repo string) ([]*Reference, *Response, error) { - u := fmt.Sprintf("repos/%v/%v/git/refs", owner, repo) +func (s *GitService) ListRefs(owner, repo string, opt *ReferenceListOptions) ([]Reference, *Response, error) { + var u string + if opt != nil && opt.Type != "" { + u = fmt.Sprintf("repos/%v/%v/git/refs/%v", owner, repo, opt.Type) + } else { + u = fmt.Sprintf("repos/%v/%v/git/refs", owner, repo) + } req, err := s.client.NewRequest("GET", u, nil) if err != nil { return nil, nil, err } - var rs []*Reference + var rs []Reference resp, err := s.client.Do(req, &rs) if err != nil { return nil, resp, err diff --git a/github/git_refs_test.go b/github/git_refs_test.go index f7c20e5..e158a34 100644 --- a/github/git_refs_test.go +++ b/github/git_refs_test.go @@ -62,29 +62,29 @@ func TestGitService_ListRefs(t *testing.T) { "ref": "refs/heads/branchA", "url": "https://api.github.com/repos/o/r/git/refs/heads/branchA", "object": { - "type": "commit", - "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", - "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" + "type": "commit", + "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", + "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" } }, { "ref": "refs/heads/branchB", "url": "https://api.github.com/repos/o/r/git/refs/heads/branchB", "object": { - "type": "commit", - "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", - "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" + "type": "commit", + "sha": "aa218f56b14c9653891f9e74264a383fa43fefbd", + "url": "https://api.github.com/repos/o/r/git/commits/aa218f56b14c9653891f9e74264a383fa43fefbd" } } ]`) }) - refs, _, err := client.Git.ListRefs("o", "r") + refs, _, err := client.Git.ListRefs("o", "r", nil) if err != nil { t.Errorf("Git.ListRefs returned error: %v", err) } - want := []*Reference{ + want := []Reference{ { Ref: String("refs/heads/branchA"), URL: String("https://api.github.com/repos/o/r/git/refs/heads/branchA"), @@ -109,6 +109,27 @@ func TestGitService_ListRefs(t *testing.T) { } } +func TestGitService_ListRefs_options(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/git/refs/t", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"ref": "r"}]`) + }) + + opt := &ReferenceListOptions{Type: "t"} + refs, _, err := client.Git.ListRefs("o", "r", opt) + if err != nil { + t.Errorf("Git.ListRefs returned error: %v", err) + } + + want := []Reference{{Ref: String("r")}} + if !reflect.DeepEqual(refs, want) { + t.Errorf("Git.ListRefs returned %+v, want %+v", refs, want) + } +} + func TestGitService_CreateRef(t *testing.T) { setup() defer teardown()