Browse Source

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.
Will Norris 12 years ago
parent
commit
cc2d2c1c98
2 changed files with 43 additions and 11 deletions
  1. +14
    -3
      github/git_refs.go
  2. +29
    -8
      github/git_refs_test.go

+ 14
- 3
github/git_refs.go View File

@ -62,17 +62,28 @@ func (s *GitService) GetRef(owner string, repo string, ref string) (*Reference,
return r, resp, err 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. // ListRefs lists all refs in a repository.
// //
// GitHub API docs: http://developer.github.com/v3/git/refs/#get-all-references // 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) req, err := s.client.NewRequest("GET", u, nil)
if err != nil { if err != nil {
return nil, nil, err return nil, nil, err
} }
var rs []*Reference
var rs []Reference
resp, err := s.client.Do(req, &rs) resp, err := s.client.Do(req, &rs)
if err != nil { if err != nil {
return nil, resp, err return nil, resp, err


+ 29
- 8
github/git_refs_test.go View File

@ -62,29 +62,29 @@ func TestGitService_ListRefs(t *testing.T) {
"ref": "refs/heads/branchA", "ref": "refs/heads/branchA",
"url": "https://api.github.com/repos/o/r/git/refs/heads/branchA", "url": "https://api.github.com/repos/o/r/git/refs/heads/branchA",
"object": { "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", "ref": "refs/heads/branchB",
"url": "https://api.github.com/repos/o/r/git/refs/heads/branchB", "url": "https://api.github.com/repos/o/r/git/refs/heads/branchB",
"object": { "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 { if err != nil {
t.Errorf("Git.ListRefs returned error: %v", err) t.Errorf("Git.ListRefs returned error: %v", err)
} }
want := []*Reference{
want := []Reference{
{ {
Ref: String("refs/heads/branchA"), Ref: String("refs/heads/branchA"),
URL: String("https://api.github.com/repos/o/r/git/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) { func TestGitService_CreateRef(t *testing.T) {
setup() setup()
defer teardown() defer teardown()


Loading…
Cancel
Save