Skip to content

Commit eda7049

Browse files
authored
feat: Implement organization role assignments for both users and teams (#3281)
Fixes: #3280.
1 parent c0f5841 commit eda7049

File tree

2 files changed

+204
-0
lines changed

2 files changed

+204
-0
lines changed

github/orgs_custom_roles.go

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,94 @@ func (s *OrganizationsService) DeleteCustomOrgRole(ctx context.Context, org stri
180180
return resp, nil
181181
}
182182

183+
// AssignOrgRoleToTeam assigns an existing organization role to a team in this organization.
184+
// In order to assign organization roles in an organization, the authenticated user must be an organization owner.
185+
//
186+
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#assign-an-organization-role-to-a-team
187+
//
188+
//meta:operation PUT /orgs/{org}/organization-roles/teams/{team_slug}/{role_id}
189+
func (s *OrganizationsService) AssignOrgRoleToTeam(ctx context.Context, org, teamSlug string, roleID int64) (*Response, error) {
190+
u := fmt.Sprintf("orgs/%v/organization-roles/teams/%v/%v", org, teamSlug, roleID)
191+
192+
req, err := s.client.NewRequest("PUT", u, nil)
193+
if err != nil {
194+
return nil, err
195+
}
196+
197+
resp, err := s.client.Do(ctx, req, nil)
198+
if err != nil {
199+
return resp, err
200+
}
201+
202+
return resp, nil
203+
}
204+
205+
// RemoveOrgRoleFromTeam removes an existing organization role assignment from a team in this organization.
206+
// In order to remove organization role assignments in an organization, the authenticated user must be an organization owner.
207+
//
208+
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#remove-an-organization-role-from-a-team
209+
//
210+
//meta:operation DELETE /orgs/{org}/organization-roles/teams/{team_slug}/{role_id}
211+
func (s *OrganizationsService) RemoveOrgRoleFromTeam(ctx context.Context, org, teamSlug string, roleID int64) (*Response, error) {
212+
u := fmt.Sprintf("orgs/%v/organization-roles/teams/%v/%v", org, teamSlug, roleID)
213+
214+
req, err := s.client.NewRequest("DELETE", u, nil)
215+
if err != nil {
216+
return nil, err
217+
}
218+
219+
resp, err := s.client.Do(ctx, req, nil)
220+
if err != nil {
221+
return resp, err
222+
}
223+
224+
return resp, nil
225+
}
226+
227+
// AssignOrgRoleToUser assigns an existing organization role to a user in this organization.
228+
// In order to assign organization roles in an organization, the authenticated user must be an organization owner.
229+
//
230+
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#assign-an-organization-role-to-a-user
231+
//
232+
//meta:operation PUT /orgs/{org}/organization-roles/users/{username}/{role_id}
233+
func (s *OrganizationsService) AssignOrgRoleToUser(ctx context.Context, org, username string, roleID int64) (*Response, error) {
234+
u := fmt.Sprintf("orgs/%v/organization-roles/users/%v/%v", org, username, roleID)
235+
236+
req, err := s.client.NewRequest("PUT", u, nil)
237+
if err != nil {
238+
return nil, err
239+
}
240+
241+
resp, err := s.client.Do(ctx, req, nil)
242+
if err != nil {
243+
return resp, err
244+
}
245+
246+
return resp, nil
247+
}
248+
249+
// RemoveOrgRoleFromUser removes an existing organization role assignment from a user in this organization.
250+
// In order to remove organization role assignments in an organization, the authenticated user must be an organization owner.
251+
//
252+
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#remove-an-organization-role-from-a-user
253+
//
254+
//meta:operation DELETE /orgs/{org}/organization-roles/users/{username}/{role_id}
255+
func (s *OrganizationsService) RemoveOrgRoleFromUser(ctx context.Context, org, username string, roleID int64) (*Response, error) {
256+
u := fmt.Sprintf("orgs/%v/organization-roles/users/%v/%v", org, username, roleID)
257+
258+
req, err := s.client.NewRequest("DELETE", u, nil)
259+
if err != nil {
260+
return nil, err
261+
}
262+
263+
resp, err := s.client.Do(ctx, req, nil)
264+
if err != nil {
265+
return resp, err
266+
}
267+
268+
return resp, nil
269+
}
270+
183271
// ListCustomRepoRoles lists the custom repository roles available in this organization.
184272
// In order to see custom repository roles in an organization, the authenticated user must be an organization owner.
185273
//

github/orgs_custom_roles_test.go

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,122 @@ func TestOrganizationsService_DeleteCustomOrgRole(t *testing.T) {
311311
})
312312
}
313313

314+
func TestOrganizationsService_AssignOrgRoleToTeam(t *testing.T) {
315+
client, mux, _, teardown := setup()
316+
defer teardown()
317+
318+
mux.HandleFunc("/orgs/o/organization-roles/teams/t/8030", func(w http.ResponseWriter, r *http.Request) {
319+
testMethod(t, r, "PUT")
320+
w.WriteHeader(http.StatusNoContent)
321+
})
322+
323+
ctx := context.Background()
324+
resp, err := client.Organizations.AssignOrgRoleToTeam(ctx, "o", "t", 8030)
325+
if err != nil {
326+
t.Errorf("Organization.AssignOrgRoleToTeam return error: %v", err)
327+
}
328+
if !cmp.Equal(resp.StatusCode, http.StatusNoContent) {
329+
t.Errorf("Organizations.AssignOrgRoleToTeam returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent)
330+
}
331+
332+
const methodName = "AssignOrgRoleToTeam"
333+
testBadOptions(t, methodName, func() (err error) {
334+
_, err = client.Organizations.AssignOrgRoleToTeam(ctx, "\no", "\nt", -8030)
335+
return err
336+
})
337+
338+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
339+
return client.Organizations.AssignOrgRoleToTeam(ctx, "o", "t", 8030)
340+
})
341+
}
342+
343+
func TestOrganizationsService_RemoveOrgRoleFromTeam(t *testing.T) {
344+
client, mux, _, teardown := setup()
345+
defer teardown()
346+
347+
mux.HandleFunc("/orgs/o/organization-roles/teams/t/8030", func(w http.ResponseWriter, r *http.Request) {
348+
testMethod(t, r, "DELETE")
349+
w.WriteHeader(http.StatusNoContent)
350+
})
351+
352+
ctx := context.Background()
353+
resp, err := client.Organizations.RemoveOrgRoleFromTeam(ctx, "o", "t", 8030)
354+
if err != nil {
355+
t.Errorf("Organization.RemoveOrgRoleFromTeam return error: %v", err)
356+
}
357+
if !cmp.Equal(resp.StatusCode, http.StatusNoContent) {
358+
t.Errorf("Organizations.RemoveOrgRoleFromTeam returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent)
359+
}
360+
361+
const methodName = "RemoveOrgRoleFromTeam"
362+
testBadOptions(t, methodName, func() (err error) {
363+
_, err = client.Organizations.RemoveOrgRoleFromTeam(ctx, "\no", "\nt", -8030)
364+
return err
365+
})
366+
367+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
368+
return client.Organizations.RemoveOrgRoleFromTeam(ctx, "o", "t", 8030)
369+
})
370+
}
371+
372+
func TestOrganizationsService_AssignOrgRoleToUser(t *testing.T) {
373+
client, mux, _, teardown := setup()
374+
defer teardown()
375+
376+
mux.HandleFunc("/orgs/o/organization-roles/users/t/8030", func(w http.ResponseWriter, r *http.Request) {
377+
testMethod(t, r, "PUT")
378+
w.WriteHeader(http.StatusNoContent)
379+
})
380+
381+
ctx := context.Background()
382+
resp, err := client.Organizations.AssignOrgRoleToUser(ctx, "o", "t", 8030)
383+
if err != nil {
384+
t.Errorf("Organization.AssignOrgRoleToUser return error: %v", err)
385+
}
386+
if !cmp.Equal(resp.StatusCode, http.StatusNoContent) {
387+
t.Errorf("Organizations.AssignOrgRoleToUser returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent)
388+
}
389+
390+
const methodName = "AssignOrgRoleToUser"
391+
testBadOptions(t, methodName, func() (err error) {
392+
_, err = client.Organizations.AssignOrgRoleToUser(ctx, "\no", "\nt", -8030)
393+
return err
394+
})
395+
396+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
397+
return client.Organizations.AssignOrgRoleToUser(ctx, "o", "t", 8030)
398+
})
399+
}
400+
401+
func TestOrganizationsService_RemoveOrgRoleFromUser(t *testing.T) {
402+
client, mux, _, teardown := setup()
403+
defer teardown()
404+
405+
mux.HandleFunc("/orgs/o/organization-roles/users/t/8030", func(w http.ResponseWriter, r *http.Request) {
406+
testMethod(t, r, "DELETE")
407+
w.WriteHeader(http.StatusNoContent)
408+
})
409+
410+
ctx := context.Background()
411+
resp, err := client.Organizations.RemoveOrgRoleFromUser(ctx, "o", "t", 8030)
412+
if err != nil {
413+
t.Errorf("Organization.RemoveOrgRoleFromUser return error: %v", err)
414+
}
415+
if !cmp.Equal(resp.StatusCode, http.StatusNoContent) {
416+
t.Errorf("Organizations.RemoveOrgRoleFromUser returned status code %+v, want %+v", resp.StatusCode, http.StatusNoContent)
417+
}
418+
419+
const methodName = "RemoveOrgRoleFromUser"
420+
testBadOptions(t, methodName, func() (err error) {
421+
_, err = client.Organizations.RemoveOrgRoleFromUser(ctx, "\no", "\nt", -8030)
422+
return err
423+
})
424+
425+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
426+
return client.Organizations.RemoveOrgRoleFromUser(ctx, "o", "t", 8030)
427+
})
428+
}
429+
314430
func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) {
315431
client, mux, _, teardown := setup()
316432
defer teardown()

0 commit comments

Comments
 (0)