Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support getting an organization role #3285

Merged
merged 2 commits into from
Sep 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions github/orgs_custom_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ func (s *OrganizationsService) ListRoles(ctx context.Context, org string) (*Orga
return customRepoRoles, resp, nil
}

// GetOrgRole gets an organization role in this organization.
// In order to get organization roles in an organization, the authenticated user must be an organization owner, or have access via an organization role.
//
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-an-organization-role
//
//meta:operation GET /orgs/{org}/organization-roles/{role_id}
func (s *OrganizationsService) GetOrgRole(ctx context.Context, org string, roleID int64) (*CustomOrgRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID)

req, err := s.client.NewRequest("GET", u, nil)
if err != nil {
return nil, nil, err
}

resultingRole := new(CustomOrgRoles)
resp, err := s.client.Do(ctx, req, resultingRole)
if err != nil {
return nil, resp, err
}

return resultingRole, resp, err
}

// CreateCustomOrgRole creates a custom role in this organization.
// In order to create custom roles in an organization, the authenticated user must be an organization owner.
//
Expand Down
99 changes: 99 additions & 0 deletions github/orgs_custom_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,105 @@ func TestOrganizationsService_ListRoles(t *testing.T) {
})
}

func TestOrganizationsService_GetOrgRole(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

// Test built-in org role
mux.HandleFunc("/orgs/o/organization-roles/8132", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"id": 8132,
"name": "all_repo_read",
"description": "Grants read access to all repositories in the organization.",
"permissions": [],
"created_at": `+referenceTimeStr+`,
"updated_at": `+referenceTimeStr+`,
"source": "Predefined",
"base_role": "read"
}`)
})

ctx := context.Background()

gotBuiltInRole, _, err := client.Organizations.GetOrgRole(ctx, "o", 8132)
if err != nil {
t.Errorf("Organizations.GetOrgRole returned error: %v", err)
}

wantBuiltInRole := &CustomOrgRoles{
ID: Int64(8132),
Name: String("all_repo_read"),
Description: String("Grants read access to all repositories in the organization."),
Permissions: []string{},
CreatedAt: &Timestamp{referenceTime},
UpdatedAt: &Timestamp{referenceTime},
Source: String("Predefined"),
BaseRole: String("read"),
}

if !cmp.Equal(gotBuiltInRole, wantBuiltInRole) {
t.Errorf("Organizations.GetOrgRole returned %+v, want %+v", gotBuiltInRole, wantBuiltInRole)
}

// Test custom org role
mux.HandleFunc("/orgs/o/organization-roles/123456", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{
"id": 123456,
"name": "test-role",
"description": "test-role",
"permissions": [
"read_organization_custom_org_role",
"read_organization_custom_repo_role",
"write_organization_custom_org_role"
],
"created_at": `+referenceTimeStr+`,
"updated_at": `+referenceTimeStr+`,
"source": "Organization",
"base_role": null
}`)
})

gotCustomRole, _, err := client.Organizations.GetOrgRole(ctx, "o", 123456)
if err != nil {
t.Errorf("Organizations.GetOrgRole returned error: %v", err)
}

wantCustomRole := &CustomOrgRoles{
ID: Int64(123456),
Name: String("test-role"),
Description: String("test-role"),
Permissions: []string{
"read_organization_custom_org_role",
"read_organization_custom_repo_role",
"write_organization_custom_org_role",
},
CreatedAt: &Timestamp{referenceTime},
UpdatedAt: &Timestamp{referenceTime},
Source: String("Organization"),
BaseRole: nil,
}

if !cmp.Equal(gotCustomRole, wantCustomRole) {
t.Errorf("Organizations.GetOrgRole returned %+v, want %+v", gotCustomRole, wantCustomRole)
}

const methodName = "GetOrgRole"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Organizations.GetOrgRole(ctx, "\no", -8132)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.GetOrgRole(ctx, "o", 8132)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestOrganizationsService_CreateCustomOrgRole(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()
Expand Down
Loading