From 2741d958876b6b98c4fd35d7fd74baa83115e953 Mon Sep 17 00:00:00 2001 From: Glenn Lewis Date: Thu, 24 Mar 2016 12:03:58 -0700 Subject: [PATCH] add support for new list all orgs method Fixes #215. Change-Id: Ic17192cc0b1dbe02745e4c7f76bd81457f73141a --- github/orgs.go | 33 +++++++++++++++++++++++++++++++++ github/orgs_test.go | 23 +++++++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/github/orgs.go b/github/orgs.go index 7596873..6018a3a 100644 --- a/github/orgs.go +++ b/github/orgs.go @@ -68,6 +68,39 @@ func (p Plan) String() string { return Stringify(p) } +// OrganizationsListOptions specifies the optional parameters to the +// OrganizationsService.ListAll method. +type OrganizationsListOptions struct { + // Since filters Organizations by ID. + Since int `url:"since,omitempty"` +} + +// ListAll lists all organizations, in the order that they were created on GitHub. +// +// Note: Pagination is powered exclusively by the since parameter. To continue +// listing the next set of organizations, use the ID of the last-returned organization +// as the opts.Since parameter for the next call. +// +// GitHub API docs: https://developer.github.com/v3/orgs/#list-all-organizations +func (s *OrganizationsService) ListAll(opt *OrganizationsListOptions) ([]Organization, *Response, error) { + u, err := addOptions("organizations", opt) + if err != nil { + return nil, nil, err + } + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + orgs := []Organization{} + resp, err := s.client.Do(req, &orgs) + if err != nil { + return nil, resp, err + } + return orgs, resp, err +} + // List the organizations for a user. Passing the empty string will list // organizations for the authenticated user. // diff --git a/github/orgs_test.go b/github/orgs_test.go index 84ebc54..3c32aae 100644 --- a/github/orgs_test.go +++ b/github/orgs_test.go @@ -13,6 +13,29 @@ import ( "testing" ) +func TestOrganizationsService_ListAll(t *testing.T) { + setup() + defer teardown() + + since := 1342004 + mux.HandleFunc("/organizations", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{"since": "1342004"}) + fmt.Fprint(w, `[{"id":4314092}]`) + }) + + opt := &OrganizationsListOptions{Since: since} + orgs, _, err := client.Organizations.ListAll(opt) + if err != nil { + t.Errorf("Organizations.ListAll returned error: %v", err) + } + + want := []Organization{{ID: Int(4314092)}} + if !reflect.DeepEqual(orgs, want) { + t.Errorf("Organizations.ListAll returned %+v, want %+v", orgs, want) + } +} + func TestOrganizationsService_List_authenticatedUser(t *testing.T) { setup() defer teardown()