From 3cd8f924dfaf1448f91b5c75358e61758d6d47b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Glapa?= Date: Wed, 1 Feb 2017 14:06:02 +0100 Subject: [PATCH 1/4] Added listing outside collaborators for an organization 2fa_disabled filter is supported too. --- github/orgs_outside_collaborators.go | 51 +++++++++++++++++++++++ github/orgs_outside_collaborators_test.go | 46 ++++++++++++++++++++ 2 files changed, 97 insertions(+) create mode 100644 github/orgs_outside_collaborators.go create mode 100644 github/orgs_outside_collaborators_test.go diff --git a/github/orgs_outside_collaborators.go b/github/orgs_outside_collaborators.go new file mode 100644 index 00000000000..10da197eff2 --- /dev/null +++ b/github/orgs_outside_collaborators.go @@ -0,0 +1,51 @@ +// Copyright 2016 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import "fmt" + +// ListOutsideCollaboratorsOptions specifies optional parameters to the +// OrganizationsService.ListOutsideCollaborators method. +type ListOutsideCollaboratorsOptions struct { + // Filter outside collaborators returned in the list. Possible values are: + // 2fa_disabled, all. Default is "all". + Filter string `url:"filter,omitempty"` + + ListOptions +} + +// ListOutsideCollaborators lists outside collaborators of organization's repositories. +// This will only work if the authenticated +// user is an owner of the organization. +// +// Warning: The API may change without advance notice during the preview period. +// Preview features are not supported for production use. +// +// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/ +func (s *OrganizationsService) ListOutsideCollaborators(org string, opt *ListOutsideCollaboratorsOptions) ([]*User, *Response, error) { + u := fmt.Sprintf("orgs/%v/outside_collaborators", org) + 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 + } + + // TODO: remove custom Accept header when this API fully launches. + req.Header.Set("Accept", mediaTypeOrgMembershipPreview) + + members := new([]*User) + resp, err := s.client.Do(req, members) + if err != nil { + return nil, resp, err + } + + return *members, resp, err +} diff --git a/github/orgs_outside_collaborators_test.go b/github/orgs_outside_collaborators_test.go new file mode 100644 index 00000000000..6fb3544a383 --- /dev/null +++ b/github/orgs_outside_collaborators_test.go @@ -0,0 +1,46 @@ +// Copyright 2016 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "fmt" + "net/http" + "reflect" + "testing" +) + +func TestOrganizationsService_ListOutsideCollaborators(t *testing.T) { + setup() + defer teardown() + + mux.HandleFunc("/orgs/o/outside_collaborators", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + testFormValues(t, r, values{ + "filter": "2fa_disabled", + "page": "2", + }) + fmt.Fprint(w, `[{"id":1}]`) + }) + + opt := &ListOutsideCollaboratorsOptions{ + Filter: "2fa_disabled", + ListOptions: ListOptions{Page: 2}, + } + members, _, err := client.Organizations.ListOutsideCollaborators("o", opt) + if err != nil { + t.Errorf("Organizations.ListOutsideCollaborators returned error: %v", err) + } + + want := []*User{{ID: Int(1)}} + if !reflect.DeepEqual(members, want) { + t.Errorf("Organizations.ListOutsideCollaborators returned %+v, want %+v", members, want) + } +} + +func TestOrganizationsService_ListOutsideCollaborators_invalidOrg(t *testing.T) { + _, _, err := client.Organizations.ListOutsideCollaborators("%", nil) + testURLParseError(t, err) +} From ae25b3590987196f76a6c0cd749f38881b547061 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Glapa?= Date: Thu, 2 Feb 2017 23:54:48 +0100 Subject: [PATCH 2/4] Addressing requests for changes. --- github/orgs_outside_collaborators.go | 5 ++--- github/orgs_outside_collaborators_test.go | 3 ++- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/github/orgs_outside_collaborators.go b/github/orgs_outside_collaborators.go index 10da197eff2..4a845f4b37c 100644 --- a/github/orgs_outside_collaborators.go +++ b/github/orgs_outside_collaborators.go @@ -1,4 +1,4 @@ -// Copyright 2016 The go-github AUTHORS. All rights reserved. +// Copyright 2017 The go-github AUTHORS. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -24,7 +24,7 @@ type ListOutsideCollaboratorsOptions struct { // Warning: The API may change without advance notice during the preview period. // Preview features are not supported for production use. // -// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/ +// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/#list-outside-collaborators func (s *OrganizationsService) ListOutsideCollaborators(org string, opt *ListOutsideCollaboratorsOptions) ([]*User, *Response, error) { u := fmt.Sprintf("orgs/%v/outside_collaborators", org) u, err := addOptions(u, opt) @@ -33,7 +33,6 @@ func (s *OrganizationsService) ListOutsideCollaborators(org string, opt *ListOut } req, err := s.client.NewRequest("GET", u, nil) - if err != nil { return nil, nil, err } diff --git a/github/orgs_outside_collaborators_test.go b/github/orgs_outside_collaborators_test.go index 6fb3544a383..81415413ec4 100644 --- a/github/orgs_outside_collaborators_test.go +++ b/github/orgs_outside_collaborators_test.go @@ -1,4 +1,4 @@ -// Copyright 2016 The go-github AUTHORS. All rights reserved. +// Copyright 2017 The go-github AUTHORS. All rights reserved. // // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. @@ -22,6 +22,7 @@ func TestOrganizationsService_ListOutsideCollaborators(t *testing.T) { "filter": "2fa_disabled", "page": "2", }) + testHeader(t, r, "Accept", mediaTypeOrgMembershipPreview) fmt.Fprint(w, `[{"id":1}]`) }) From a304877a11f4fb0c063186c47e9bd2ff601a5c76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Glapa?= Date: Fri, 3 Feb 2017 22:58:43 +0100 Subject: [PATCH 3/4] Adressing the requests for changes, part 2 --- github/orgs_outside_collaborators.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/github/orgs_outside_collaborators.go b/github/orgs_outside_collaborators.go index 4a845f4b37c..4e513b48770 100644 --- a/github/orgs_outside_collaborators.go +++ b/github/orgs_outside_collaborators.go @@ -40,11 +40,11 @@ func (s *OrganizationsService) ListOutsideCollaborators(org string, opt *ListOut // TODO: remove custom Accept header when this API fully launches. req.Header.Set("Accept", mediaTypeOrgMembershipPreview) - members := new([]*User) - resp, err := s.client.Do(req, members) + var members []*User + resp, err := s.client.Do(req, &members) if err != nil { return nil, resp, err } - return *members, resp, err + return members, resp, err } From 7a5c23711b8bd1ae07e285ab84f0ee743e9d271d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Glapa?= Date: Fri, 3 Feb 2017 23:19:48 +0100 Subject: [PATCH 4/4] Adressing the requests for changes, part 3 --- github/orgs_outside_collaborators.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/orgs_outside_collaborators.go b/github/orgs_outside_collaborators.go index 4e513b48770..a796fcc0cda 100644 --- a/github/orgs_outside_collaborators.go +++ b/github/orgs_outside_collaborators.go @@ -46,5 +46,5 @@ func (s *OrganizationsService) ListOutsideCollaborators(org string, opt *ListOut return nil, resp, err } - return members, resp, err + return members, resp, nil }