diff --git a/github/repos.go b/github/repos.go index 5fb7a3e..8f16043 100644 --- a/github/repos.go +++ b/github/repos.go @@ -230,6 +230,60 @@ func (s *RepositoriesService) Edit(owner, repo string, repository *Repository) ( return r, resp, err } +// Contributor represents a repository contributor +type Contributor struct { + Login *string `json:"login,omitempty"` + ID *int `json:"id,omitempty"` + AvatarURL *string `json:"avatar_url,omitempty"` + GravatarID *string `json:"gravatar_id,omitempty"` + URL *string `json:"url,omitempty"` + HTMLURL *string `json:"html_url,omitempty"` + FollowersURL *string `json:"followers_url,omitempty"` + FollowingURL *string `json:"following_url,omitempty"` + GistsURL *string `json:"gists_url,omitempty"` + StarredURL *string `json:"starred_url,omitempty"` + SubscriptionsURL *string `json:"subscriptions_url,omitempty"` + OrganizationsURL *string `json:"organizations_url,omitempty"` + ReposURL *string `json:"repos_url,omitempty"` + EventsURL *string `json:"events_url,omitempty"` + ReceivedEventsURL *string `json:"received_events_url,omitempty"` + Type *string `json:"type,omitempty"` + SiteAdmin *bool `json:"site_admin"` + Contributions *int `json:"contributions,omitempty"` +} + +// ListContributorsOptions specifies the optional parameters to the +// RepositoriesService.ListContributors method. +type ListContributorsOptions struct { + // Include anonymous contributors in results or not + Anon string `url:"anon,omitempty"` +} + +// ListContributors lists contributors for a repository. +// +// GitHub API docs: http://developer.github.com/v3/repos/#list-contributors +func (s *RepositoriesService) ListContributors(owner string, repository string, opt *ListContributorsOptions) ([]Contributor, *Response, error) { + u := fmt.Sprintf("/repos/%v/%v/contributors", owner, repository) + + u, err := addOptions(u, opt) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + contributor := new([]Contributor) + resp, err := s.client.Do(req, contributor) + if err != nil { + return nil, nil, err + } + + return *contributor, resp, err +} + // ListLanguages lists languages for the specified repository. The returned map // specifies the languages and the number of bytes of code written in that // language. For example: diff --git a/github/repos_test.go b/github/repos_test.go index d30d32c..b3bbf91 100644 --- a/github/repos_test.go +++ b/github/repos_test.go @@ -244,6 +244,28 @@ func TestRepositoriesService_Edit_invalidOwner(t *testing.T) { testURLParseError(t, err) } +func TestRepositoriesService_ListContributors(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/repos/o/r/contributors", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `[{"contributions":42}]`) + }) + + opts := &ListContributorsOptions{Anon: "true"} + contributors, _, err := client.Repositories.ListContributors("o", "r", opts) + + if err != nil { + t.Errorf("Repositories.ListContributors returned error: %v", err) + } + + want := []Contributor{{Contributions: Int(42)}} + if !reflect.DeepEqual(contributors, want) { + t.Errorf("Repositories.ListContributors returned %+v, want %+v", contributors, want) + } +} + func TestRepositoriesService_ListLanguages(t *testing.T) { setup() defer teardown()