Skip to content

Commit 81ff3c9

Browse files
authored
Query identities associated with organization roles (#3130)
1 parent 5c79fa2 commit 81ff3c9

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

github/orgs_custom_roles.go

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

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

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+
fmt.Fprint(w, `[{"id":1}]`)
170+
})
171+
opt := &ListOptions{Page: 2}
172+
ctx := context.Background()
173+
apps, _, err := client.Organizations.ListTeamsAssignedToOrgRole(ctx, "o", 1729, opt)
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, opt)
186+
return err
187+
})
188+
189+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
190+
got, resp, err := client.Organizations.ListTeamsAssignedToOrgRole(ctx, "o", 1729, opt)
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+
fmt.Fprint(w, `[{"id":1}]`)
205+
})
206+
opt := &ListOptions{Page: 2}
207+
ctx := context.Background()
208+
apps, _, err := client.Organizations.ListUsersAssignedToOrgRole(ctx, "o", 1729, opt)
209+
if err != nil {
210+
t.Errorf("Organizations.ListUsersAssignedToOrgRole returned error: %v", err)
211+
}
212+
213+
want := []*User{{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, opt)
221+
return err
222+
})
223+
224+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
225+
got, resp, err := client.Organizations.ListUsersAssignedToOrgRole(ctx, "o", 1729, opt)
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)