Skip to content

Commit 09946e5

Browse files
authored
feat: Add list organization fine-grained permissions (#4072)
1 parent 0571e8e commit 09946e5

2 files changed

Lines changed: 69 additions & 0 deletions

File tree

github/orgs_organization_roles.go

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ type CreateOrUpdateOrgRoleOptions struct {
3737
BaseRole *string `json:"base_role,omitempty"`
3838
}
3939

40+
// OrganizationFineGrainedPermission represents a fine-grained permission that protects organization resources.
41+
type OrganizationFineGrainedPermission struct {
42+
Name string `json:"name"`
43+
Description string `json:"description"`
44+
}
45+
4046
// ListRoles lists the custom roles available in this organization.
4147
// In order to see custom roles in an organization, the authenticated user must be an organization owner.
4248
//
@@ -293,3 +299,31 @@ func (s *OrganizationsService) ListUsersAssignedToOrgRole(ctx context.Context, o
293299

294300
return users, resp, nil
295301
}
302+
303+
// ListFineGrainedPermissions lists the fine-grained permissions that can be used in custom organization roles for an organization.
304+
//
305+
// To use this endpoint, the authenticated user must be one of:
306+
// - An administrator for the organization.
307+
// - A user, or a user on a team, with the fine-grained permissions of `read_organization_custom_org_role` in the organization.
308+
//
309+
// OAuth app tokens and personal access tokens (classic) need the `admin:org` scope to use this endpoint.
310+
//
311+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/organization-roles#list-organization-fine-grained-permissions-for-an-organization
312+
//
313+
//meta:operation GET /orgs/{org}/organization-fine-grained-permissions
314+
func (s *OrganizationsService) ListFineGrainedPermissions(ctx context.Context, org string) ([]*OrganizationFineGrainedPermission, *Response, error) {
315+
u := fmt.Sprintf("orgs/%v/organization-fine-grained-permissions", org)
316+
317+
req, err := s.client.NewRequest("GET", u, nil)
318+
if err != nil {
319+
return nil, nil, err
320+
}
321+
322+
var permissions []*OrganizationFineGrainedPermission
323+
resp, err := s.client.Do(ctx, req, &permissions)
324+
if err != nil {
325+
return nil, resp, err
326+
}
327+
328+
return permissions, resp, nil
329+
}

github/orgs_organization_roles_test.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,3 +495,38 @@ func TestOrganizationsService_ListUsersAssignedToOrgRole(t *testing.T) {
495495
return resp, err
496496
})
497497
}
498+
499+
func TestOrganizationsService_ListFineGrainedPermissions(t *testing.T) {
500+
t.Parallel()
501+
client, mux, _ := setup(t)
502+
503+
mux.HandleFunc("/orgs/o/organization-fine-grained-permissions", func(w http.ResponseWriter, r *http.Request) {
504+
testMethod(t, r, "GET")
505+
fmt.Fprint(w, `[{"name":"p1", "description":"d1"}]`)
506+
})
507+
508+
ctx := t.Context()
509+
permissions, _, err := client.Organizations.ListFineGrainedPermissions(ctx, "o")
510+
if err != nil {
511+
t.Errorf("Organizations.ListFineGrainedPermissions returned error: %v", err)
512+
}
513+
514+
want := []*OrganizationFineGrainedPermission{{Name: "p1", Description: "d1"}}
515+
if !cmp.Equal(permissions, want) {
516+
t.Errorf("Organizations.ListFineGrainedPermissions returned %+v, want %+v", permissions, want)
517+
}
518+
519+
const methodName = "ListFineGrainedPermissions"
520+
testBadOptions(t, methodName, func() (err error) {
521+
_, _, err = client.Organizations.ListFineGrainedPermissions(ctx, "\no")
522+
return err
523+
})
524+
525+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
526+
got, resp, err := client.Organizations.ListFineGrainedPermissions(ctx, "o")
527+
if got != nil {
528+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
529+
}
530+
return resp, err
531+
})
532+
}

0 commit comments

Comments
 (0)