Skip to content

Commit 218ecc2

Browse files
Kshitij Saraogidmitshur
authored andcommitted
Add Outside Collaborators API to OrganizationsService. (google#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 google#524.
1 parent fc3c571 commit 218ecc2

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

github/orgs_outside_collaborators.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,34 @@ func (s *OrganizationsService) ListOutsideCollaborators(ctx context.Context, org
4848

4949
return members, resp, nil
5050
}
51+
52+
// RemoveOutsideCollaborator removes a user from the list of outside collaborators;
53+
// consequently, removing them from all the organization's repositories.
54+
//
55+
// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/#remove-outside-collaborator
56+
func (s *OrganizationsService) RemoveOutsideCollaborator(ctx context.Context, org string, user string) (*Response, error) {
57+
u := fmt.Sprintf("orgs/%v/outside_collaborators/%v", org, user)
58+
req, err := s.client.NewRequest("DELETE", u, nil)
59+
if err != nil {
60+
return nil, err
61+
}
62+
63+
return s.client.Do(ctx, req, nil)
64+
}
65+
66+
// ConvertMemberToOutsideCollaborator reduces the permission level of a member of the
67+
// organization to that of an outside collaborator. Therefore, they will only
68+
// have access to the repositories that their current team membership allows.
69+
// Responses for converting a non-member or the last owner to an outside collaborator
70+
// are listed in GitHub API docs.
71+
//
72+
// GitHub API docs: https://developer.github.com/v3/orgs/outside_collaborators/#convert-member-to-outside-collaborator
73+
func (s *OrganizationsService) ConvertMemberToOutsideCollaborator(ctx context.Context, org string, user string) (*Response, error) {
74+
u := fmt.Sprintf("orgs/%v/outside_collaborators/%v", org, user)
75+
req, err := s.client.NewRequest("PUT", u, nil)
76+
if err != nil {
77+
return nil, err
78+
}
79+
80+
return s.client.Do(ctx, req, nil)
81+
}

github/orgs_outside_collaborators_test.go

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,87 @@ func TestOrganizationsService_ListOutsideCollaborators_invalidOrg(t *testing.T)
4848
_, _, err := client.Organizations.ListOutsideCollaborators(context.Background(), "%", nil)
4949
testURLParseError(t, err)
5050
}
51+
52+
func TestOrganizationsService_RemoveOutsideCollaborator(t *testing.T) {
53+
client, mux, _, teardown := setup()
54+
defer teardown()
55+
56+
handler := func(w http.ResponseWriter, r *http.Request) {
57+
testMethod(t, r, "DELETE")
58+
}
59+
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
60+
61+
_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
62+
if err != nil {
63+
t.Errorf("Organizations.RemoveOutsideCollaborator returned error: %v", err)
64+
}
65+
}
66+
67+
func TestOrganizationsService_RemoveOutsideCollaborator_NonMember(t *testing.T) {
68+
client, mux, _, teardown := setup()
69+
defer teardown()
70+
71+
handler := func(w http.ResponseWriter, r *http.Request) {
72+
testMethod(t, r, "DELETE")
73+
w.WriteHeader(http.StatusNotFound)
74+
}
75+
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
76+
77+
_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
78+
if err, ok := err.(*ErrorResponse); !ok {
79+
t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
80+
} else if err.Response.StatusCode != http.StatusNotFound {
81+
t.Errorf("Organizations.RemoveOutsideCollaborator did not return 404 status code")
82+
}
83+
}
84+
85+
func TestOrganizationsService_RemoveOutsideCollaborator_Member(t *testing.T) {
86+
client, mux, _, teardown := setup()
87+
defer teardown()
88+
89+
handler := func(w http.ResponseWriter, r *http.Request) {
90+
testMethod(t, r, "DELETE")
91+
w.WriteHeader(http.StatusUnprocessableEntity)
92+
}
93+
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
94+
95+
_, err := client.Organizations.RemoveOutsideCollaborator(context.Background(), "o", "u")
96+
if err, ok := err.(*ErrorResponse); !ok {
97+
t.Errorf("Organizations.RemoveOutsideCollaborator did not return an error")
98+
} else if err.Response.StatusCode != http.StatusUnprocessableEntity {
99+
t.Errorf("Organizations.RemoveOutsideCollaborator did not return 422 status code")
100+
}
101+
}
102+
103+
func TestOrganizationsService_ConvertMemberToOutsideCollaborator(t *testing.T) {
104+
client, mux, _, teardown := setup()
105+
defer teardown()
106+
107+
handler := func(w http.ResponseWriter, r *http.Request) {
108+
testMethod(t, r, "PUT")
109+
}
110+
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
111+
112+
_, err := client.Organizations.ConvertMemberToOutsideCollaborator(context.Background(), "o", "u")
113+
if err != nil {
114+
t.Errorf("Organizations.ConvertMemberToOutsideCollaborator returned error: %v", err)
115+
}
116+
}
117+
118+
func TestOrganizationsService_ConvertMemberToOutsideCollaborator_NonMemberOrLastOwner(t *testing.T) {
119+
client, mux, _, teardown := setup()
120+
defer teardown()
121+
122+
handler := func(w http.ResponseWriter, r *http.Request) {
123+
testMethod(t, r, "PUT")
124+
w.WriteHeader(http.StatusForbidden)
125+
}
126+
mux.HandleFunc("/orgs/o/outside_collaborators/u", handler)
127+
128+
_, err := client.Organizations.ConvertMemberToOutsideCollaborator(context.Background(), "o", "u")
129+
if err, ok := err.(*ErrorResponse); !ok {
130+
t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return an error")
131+
} else if err.Response.StatusCode != http.StatusForbidden {
132+
t.Errorf("Organizations.ConvertMemberToOutsideCollaborator did not return 403 status code")
133+
}
134+
}

0 commit comments

Comments
 (0)