diff --git a/github/git_commits.go b/github/git_commits.go index 6e73420..c96fc99 100644 --- a/github/git_commits.go +++ b/github/git_commits.go @@ -19,6 +19,7 @@ type Commit struct { Tree *Tree `json:"tree,omitempty"` Parents []Commit `json:"parents,omitempty"` Stats *CommitStats `json:"stats,omitempty"` + URL *string `json:url,omitempty"` } func (c Commit) String() string { diff --git a/github/repos.go b/github/repos.go index 8f16043..775cbc5 100644 --- a/github/repos.go +++ b/github/repos.go @@ -309,3 +309,28 @@ func (s *RepositoriesService) ListLanguages(owner string, repository string) (ma return languages, resp, err } + +// Branch represents a repository branch +type Branch struct { + Name *string `json:"name,omitempty"` + Commit *Commit +} + +// ListBranches lists branches for the specified repository. +// +// GitHub API docs: http://developer.github.com/v3/repos/#list-branches +func (s *RepositoriesService) ListBranches(owner string, repository string) ([]Branch, *Response, error) { + u := fmt.Sprintf("/repos/%v/%v/branches", owner, repository) + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + branches := new([]Branch) + resp, err := s.client.Do(req, branches) + if err != nil { + return nil, resp, err + } + + return *branches, resp, err +} diff --git a/github/repos_test.go b/github/repos_test.go index b3bbf91..76503a5 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -286,6 +286,26 @@ func TestRepositoriesService_ListLanguages(t *testing.T) { } } +func TestRepositoriesService_ListBranches(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/branches", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"name":"master", "commit" : {"sha" : "a57781", "url" : "https://api.github.com/repos/o/r/commits/a57781"}}]`) + }) + + branches, _, err := client.Repositories.ListBranches("o", "r") + if err != nil { + t.Errorf("Repositories.ListBranches returned error: %v", err) + } + + want := []Branch{{Name: String("master"), Commit: &Commit{SHA: String("a57781"), URL: String("https://api.github.com/repos/o/r/commits/a57781")}}} + if !reflect.DeepEqual(branches, want) { + t.Errorf("Repositories.ListBranches returned %+v, want %+v", branches, want) + } +} + func TestRepositoriesService_ListLanguages_invalidOwner(t *testing.T) { _, _, err := client.Repositories.ListLanguages("%", "%") testURLParseError(t, err)