Skip to content

Commit da27875

Browse files
committed
Query identities associated with organization roles
Wrap queries to return the teams and users associated with a specific organization role
1 parent a6806bc commit da27875

2 files changed

Lines changed: 116 additions & 0 deletions

File tree

github/orgs_custom_roles.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,3 +126,49 @@ func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org, ro
126126

127127
return resp, nil
128128
}
129+
130+
// ListTeamsAssignedToOrgRole returns all teams assigned to as specific organization role.
131+
// In order to list teams assigned to an organization role, the authenticated user must be an organization owner.
132+
//
133+
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#list-teams-that-are-assigned-to-an-organization-role
134+
//
135+
//meta:operation GET /orgs/{org}/organization-roles/{role_id}/teams
136+
func (s *OrganizationsService) ListTeamsAssignedToOrgRole(ctx context.Context, org string, roleID int64) ([]*Team, *Response, error) {
137+
u := fmt.Sprintf("orgs/%v/organization-roles/%v/teams", org, roleID)
138+
139+
req, err := s.client.NewRequest("GET", u, nil)
140+
if err != nil {
141+
return nil, nil, err
142+
}
143+
144+
var teams []*Team
145+
resp, err := s.client.Do(ctx, req, &teams)
146+
if err != nil {
147+
return nil, resp, err
148+
}
149+
150+
return teams, resp, nil
151+
}
152+
153+
// ListUsersAssignedToOrgRole returns all users assigned to as specific organization role.
154+
// In order to list users assigned to an organization role, the authenticated user must be an organization owner.
155+
//
156+
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#list-users-that-are-assigned-to-an-organization-role
157+
//
158+
//meta:operation GET /orgs/{org}/organization-roles/{role_id}/users
159+
func (s *OrganizationsService) ListUsersAssignedToOrgRole(ctx context.Context, org string, roleID int64) ([]*User, *Response, error) {
160+
u := fmt.Sprintf("orgs/%v/organization-roles/%v/users", org, roleID)
161+
162+
req, err := s.client.NewRequest("GET", u, nil)
163+
if err != nil {
164+
return nil, nil, err
165+
}
166+
167+
var users []*User
168+
resp, err := s.client.Do(ctx, req, &users)
169+
if err != nil {
170+
return nil, resp, err
171+
}
172+
173+
return users, resp, nil
174+
}

github/orgs_custom_roles_test.go

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,73 @@ func TestOrganizationsService_DeleteCustomRepoRole(t *testing.T) {
159159
return err
160160
})
161161
}
162+
163+
func TestOrganizationsService_ListTeamsAssignedToOrgRole(t *testing.T) {
164+
client, mux, _, teardown := setup()
165+
defer teardown()
166+
167+
mux.HandleFunc("/orgs/o/organization-roles/1729/teams", func(w http.ResponseWriter, r *http.Request) {
168+
testMethod(t, r, "GET")
169+
testFormValues(t, r, values{"page": "2"})
170+
fmt.Fprint(w, `[{"id":1}]`)
171+
})
172+
ctx := context.Background()
173+
apps, _, err := client.Organizations.ListTeamsAssignedToOrgRole(ctx, "o", 1729)
174+
if err != nil {
175+
t.Errorf("Organizations.ListTeamsAssignedToOrgRole returned error: %v", err)
176+
}
177+
178+
want := []*Team{{ID: Int64(1)}}
179+
if !cmp.Equal(apps, want) {
180+
t.Errorf("Organizations.ListTeamsAssignedToOrgRole returned %+v, want %+v", apps, want)
181+
}
182+
183+
const methodName = "ListTeamsAssignedToOrgRole"
184+
testBadOptions(t, methodName, func() (err error) {
185+
_, _, err = client.Organizations.ListTeamsAssignedToOrgRole(ctx, "\no", 1729)
186+
return err
187+
})
188+
189+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
190+
got, resp, err := client.Organizations.ListTeamsAssignedToOrgRole(ctx, "o", 1729)
191+
if got != nil {
192+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
193+
}
194+
return resp, err
195+
})
196+
}
197+
198+
func TestOrganizationsService_ListUsersAssignedToOrgRole(t *testing.T) {
199+
client, mux, _, teardown := setup()
200+
defer teardown()
201+
202+
mux.HandleFunc("/orgs/o/organization-roles/1729/users", func(w http.ResponseWriter, r *http.Request) {
203+
testMethod(t, r, "GET")
204+
testFormValues(t, r, values{"page": "2"})
205+
fmt.Fprint(w, `[{"id":1}]`)
206+
})
207+
ctx := context.Background()
208+
apps, _, err := client.Organizations.ListUsersAssignedToOrgRole(ctx, "o", 1729)
209+
if err != nil {
210+
t.Errorf("Organizations.ListUsersAssignedToOrgRole returned error: %v", err)
211+
}
212+
213+
want := []*Team{{ID: Int64(1)}}
214+
if !cmp.Equal(apps, want) {
215+
t.Errorf("Organizations.ListUsersAssignedToOrgRole returned %+v, want %+v", apps, want)
216+
}
217+
218+
const methodName = "ListUsersAssignedToOrgRole"
219+
testBadOptions(t, methodName, func() (err error) {
220+
_, _, err = client.Organizations.ListUsersAssignedToOrgRole(ctx, "\no", 1729)
221+
return err
222+
})
223+
224+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
225+
got, resp, err := client.Organizations.ListUsersAssignedToOrgRole(ctx, "o", 1729)
226+
if got != nil {
227+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
228+
}
229+
return resp, err
230+
})
231+
}

0 commit comments

Comments
 (0)