Permalink
Browse files

Add Outside Collaborators API to OrganizationsService. (#812)

This change implements two methods on OrganizationsService
corresponding to endpoints of the Outside Collaborators API:

-	RemoveOutsideCollaborator
-	ConvertMemberToOutsideCollaborator

(ListOutsideCollaborators was already implemented.)

GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators.

Resolves #524.
  • Loading branch information...
kshitij10496 authored and dmitshur committed Jan 6, 2018
1 parent fc3c571 commit 218ecc238cf77bbaa2e16a54cada252ec87e7d6e
Showing with 115 additions and 0 deletions.
  1. +31 −0 github/orgs_outside_collaborators.go
  2. +84 −0 github/orgs_outside_collaborators_test.go
@@ -48,3 +48,34 @@ func (s *OrganizationsService) ListOutsideCollaborators(ctx context.Context, org
return members, resp, nil
}
// RemoveOutsideCollaborator removes a user from the list of outside collaborators;
// consequently, removing them from all the organization's repositories.
//
// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/#remove-outside-collaborator
func (s *OrganizationsService) RemoveOutsideCollaborator(ctx context.Context, org string, user string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/outside_collaborators/%v", org, user)
req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
// ConvertMemberToOutsideCollaborator reduces the permission level of a member of the
// organization to that of an outside collaborator. Therefore, they will only
// have access to the repositories that their current team membership allows.
// Responses for converting a non-member or the last owner to an outside collaborator
// are listed in GitHub API docs.
//
// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/#convert-member-to-outside-collaborator
func (s *OrganizationsService) ConvertMemberToOutsideCollaborator(ctx context.Context, org string, user string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/outside_collaborators/%v", org, user)
req, err := s.client.NewRequest("PUT", u, nil)
if err != nil {
return nil, err
}
return s.client.Do(ctx, req, nil)
}
@@ -48,3 +48,87 @@ func TestOrganizationsService_ListOutsideCollaborators_invalidOrg(t *testing.T)
_, _, err := client.Organizations.ListOutsideCollaborators(context.Background(), "%", nil)
testURLParseError(t, err)
}
func TestOrganizationsService_RemoveOutsideCollaborator(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
handler := func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
}
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
if err != nil {
t.Errorf("Organizations.RemoveOutsideCollaborator returned error: %v", err)
}
}
func TestOrganizationsService_RemoveOutsideCollaborator_NonMember(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
handler := func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNotFound)
}
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
if err, ok := err.(*ErrorResponse); !ok {
t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
} else if err.Response.StatusCode != http.StatusNotFound {
t.Errorf("Organizations.RemoveOutsideCollaborator did not return 404 status code")
}
}
func TestOrganizationsService_RemoveOutsideCollaborator_Member(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
handler := func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusUnprocessableEntity)
}
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
if err, ok := err.(*ErrorResponse); !ok {
t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
} else if err.Response.StatusCode != http.StatusUnprocessableEntity {
t.Errorf("Organizations.RemoveOutsideCollaborator did not return 422 status code")
}
}
func TestOrganizationsService_ConvertMemberToOutsideCollaborator(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
handler := func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
}
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
_, err := client.Organizations.ConvertMemberToOutsideCollaborator(context.Background(), "o", "u")
if err != nil {
t.Errorf("Organizations.ConvertMemberToOutsideCollaborator returned error: %v", err)
}
}
func TestOrganizationsService_ConvertMemberToOutsideCollaborator_NonMemberOrLastOwner(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
handler := func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PUT")
w.WriteHeader(http.StatusForbidden)
}
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
_, err := client.Organizations.ConvertMemberToOutsideCollaborator(context.Background(), "o", "u")
if err, ok := err.(*ErrorResponse); !ok {
t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return an error")
} else if err.Response.StatusCode != http.StatusForbidden {
t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return 403 status code")
}
}

0 comments on commit 218ecc2

Please sign in to comment.