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 querying organization custom roles #3129

Merged
merged 3 commits into from May 15, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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.

135 changes: 124 additions & 11 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 string, roleID int64, 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 string, roleID int64) (*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
}

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) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tomfeigin - if I'm not mistaken, this line and line 203 below are both breaking API changes, as code written by a user of this repo that calls one or both of these endpoints will no longer compile correctly without changing their code. Agreed?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh you are right, should I keep the previous naming then?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's just have the breaking change, I'll fix the other APIs to use the int64 ID

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, honestly I don't have a problem making breaking API changes, which I suppose might be obvious from the current version number of this repo. 😂

Also, I think it is always a good idea to make things clearer and easier-to-understand for users of this repo... so I think your name change here was perfectly appropriate and that's also why I recommended fixing the bad field type while we are making a breaking API change.

So I'll leave it up to you, but I think the breaking API change is appropriate and am willing to move forward with it.

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 string, roleID int64, 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 All @@ -110,7 +223,7 @@ func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org, ro
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#delete-a-custom-repository-role
//
//meta:operation DELETE /orgs/{org}/custom-repository-roles/{role_id}
func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org, roleID string) (*Response, error) {
func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org string, roleID int64) (*Response, error) {
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)

req, err := s.client.NewRequest("DELETE", u, nil)
Expand Down