Skip to content

Commit

Permalink
Support querying organization custom roles
Browse files Browse the repository at this point in the history
Added support for querying, creating, updating and deleting organization
custom roles.
  • Loading branch information
tomfeigin committed Apr 16, 2024
1 parent a6806bc commit 83cac3c
Show file tree
Hide file tree
Showing 4 changed files with 390 additions and 24 deletions.
54 changes: 51 additions & 3 deletions github/github-accessors.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

78 changes: 69 additions & 9 deletions github/github-accessors_test.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

133 changes: 123 additions & 10 deletions github/orgs_custom_roles.go
Expand Up @@ -10,6 +10,20 @@ import (
"fmt"
)

// OrganizationCustomRoles represents custom organization roles available in specified organization.
type OrganizationCustomRoles struct {
TotalCount *int `json:"total_count,omitempty"`
CustomRepoRoles []*CustomOrgRoles `json:"roles,omitempty"`
}

// CustomOrgRoles represents custom organization role available in specified organization.
type CustomOrgRoles struct {
ID *int64 `json:"id,omitempty"`
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}

// OrganizationCustomRepoRoles represents custom repository roles available in specified organization.
type OrganizationCustomRepoRoles struct {
TotalCount *int `json:"total_count,omitempty"`
Expand All @@ -27,6 +41,113 @@ type CustomRepoRoles struct {
Permissions []string `json:"permissions,omitempty"`
}

// CreateOrUpdateOrgRoleOptions represents options required to create or update a custom organization role.
type CreateOrUpdateOrgRoleOptions struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}

// CreateOrUpdateCustomRepoRoleOptions represents options required to create or update a custom repository role.
type CreateOrUpdateCustomRepoRoleOptions struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
BaseRole *string `json:"base_role,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}

// ListRoles lists the custom roles available in this organization.
// In order to see custom roles in an organization, the authenticated user must be an organization owner.
//
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-all-organization-roles-for-an-organization
//
//meta:operation GET /orgs/{org}/organization-roles
func (s *OrganizationsService) ListRoles(ctx context.Context, org string) (*OrganizationCustomRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/organization-roles", org)

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

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

return customRepoRoles, resp, nil
}

// 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.
//
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#create-a-custom-organization-role
//
//meta:operation POST /orgs/{org}/organization-roles
func (s *OrganizationsService) CreateCustomOrgRole(ctx context.Context, org string, opts *CreateOrUpdateOrgRoleOptions) (*CustomOrgRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/organization-roles", org)

req, err := s.client.NewRequest("POST", u, opts)
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
}

// UpdateCustomOrgRole updates a custom role in this organization.
// In order to update custom roles in an organization, the authenticated user must be an organization owner.
//
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#update-a-custom-organization-role
//
//meta:operation PATCH /orgs/{org}/organization-roles/{role_id}
func (s *OrganizationsService) UpdateCustomOrgRole(ctx context.Context, org, roleID string, opts *CreateOrUpdateOrgRoleOptions) (*CustomOrgRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID)

req, err := s.client.NewRequest("PATCH", u, opts)
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
}

// DeleteCustomOrgRole deletes an existing custom role in this organization.
// In order to delete custom roles in an organization, the authenticated user must be an organization owner.
//
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#delete-a-custom-organization-role
//
//meta:operation DELETE /orgs/{org}/organization-roles/{role_id}
func (s *OrganizationsService) DeleteCustomOrgRole(ctx context.Context, org, roleID string) (*Response, error) {
u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID)

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

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

Check warning on line 145 in github/orgs_custom_roles.go

View check run for this annotation

Codecov / codecov/patch

github/orgs_custom_roles.go#L145

Added line #L145 was not covered by tests
}

return resp, nil
}

// ListCustomRepoRoles lists the custom repository roles available in this organization.
// In order to see custom repository roles in an organization, the authenticated user must be an organization owner.
//
Expand All @@ -50,21 +171,13 @@ func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org stri
return customRepoRoles, resp, nil
}

// CreateOrUpdateCustomRoleOptions represents options required to create or update a custom repository role.
type CreateOrUpdateCustomRoleOptions struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
BaseRole *string `json:"base_role,omitempty"`
Permissions []string `json:"permissions,omitempty"`
}

// CreateCustomRepoRole creates a custom repository role in this organization.
// In order to create custom repository roles in an organization, the authenticated user must be an organization owner.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#create-a-custom-repository-role
//
//meta:operation POST /orgs/{org}/custom-repository-roles
func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRoleOptions) (*CustomRepoRoles, *Response, error) {
func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/custom-repository-roles", org)

req, err := s.client.NewRequest("POST", u, opts)
Expand All @@ -87,7 +200,7 @@ func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org str
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#update-a-custom-repository-role
//
//meta:operation PATCH /orgs/{org}/custom-repository-roles/{role_id}
func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org, roleID string, opts *CreateOrUpdateCustomRoleOptions) (*CustomRepoRoles, *Response, error) {
func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org, roleID string, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)

req, err := s.client.NewRequest("PATCH", u, opts)
Expand Down

0 comments on commit 83cac3c

Please sign in to comment.