diff --git a/github/orgs_custom_roles.go b/github/orgs_custom_roles.go index dcbd79efa77..8f9dd42a6c7 100644 --- a/github/orgs_custom_roles.go +++ b/github/orgs_custom_roles.go @@ -96,7 +96,21 @@ func (s *OrganizationsService) ListRoles(ctx context.Context, org string) (*Orga 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) + var params interface{} + params = opts + + // For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON. + if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 { + params = struct { + *CreateOrUpdateOrgRoleOptions + Permissions []string `json:"permissions"` + }{ + CreateOrUpdateOrgRoleOptions: opts, + Permissions: opts.Permissions, + } + } + + req, err := s.client.NewRequest("POST", u, params) if err != nil { return nil, nil, err } @@ -119,7 +133,21 @@ func (s *OrganizationsService) CreateCustomOrgRole(ctx context.Context, org stri 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) + var params interface{} + params = opts + + // For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON. + if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 { + params = struct { + *CreateOrUpdateOrgRoleOptions + Permissions []string `json:"permissions"` + }{ + CreateOrUpdateOrgRoleOptions: opts, + Permissions: opts.Permissions, + } + } + + req, err := s.client.NewRequest("PATCH", u, params) if err != nil { return nil, nil, err } @@ -188,7 +216,21 @@ func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org stri 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) + var params interface{} + params = opts + + // For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON. + if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 { + params = struct { + *CreateOrUpdateCustomRepoRoleOptions + Permissions []string `json:"permissions"` + }{ + CreateOrUpdateCustomRepoRoleOptions: opts, + Permissions: opts.Permissions, + } + } + + req, err := s.client.NewRequest("POST", u, params) if err != nil { return nil, nil, err } @@ -211,7 +253,21 @@ func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org str 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) + var params interface{} + params = opts + + // For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON. + if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 { + params = struct { + *CreateOrUpdateCustomRepoRoleOptions + Permissions []string `json:"permissions"` + }{ + CreateOrUpdateCustomRepoRoleOptions: opts, + Permissions: opts.Permissions, + } + } + + req, err := s.client.NewRequest("PATCH", u, params) if err != nil { return nil, nil, err } diff --git a/github/orgs_custom_roles_test.go b/github/orgs_custom_roles_test.go index 338eac19fc1..65707385d06 100644 --- a/github/orgs_custom_roles_test.go +++ b/github/orgs_custom_roles_test.go @@ -138,6 +138,18 @@ func TestOrganizationsService_CreateCustomOrgRole(t *testing.T) { } return resp, err }) + + opts.Permissions = []string{} + + emptyPermissionRole, _, err := client.Organizations.CreateCustomOrgRole(ctx, "o", opts) + if err != nil { + t.Errorf("Organizations.CreateCustomOrgRole with empty permission returned error: %v", err) + } + want.Permissions = []string{} + + if !cmp.Equal(emptyPermissionRole, want) { + t.Errorf("Organizations.CreateCustomOrgRole with empty permission returned %+v, want %+v", emptyPermissionRole, want) + } } func TestOrganizationsService_UpdateCustomOrgRole(t *testing.T) { @@ -179,6 +191,18 @@ func TestOrganizationsService_UpdateCustomOrgRole(t *testing.T) { } return resp, err }) + + opts.Permissions = []string{} + + emptyPermissionRole, _, err := client.Organizations.UpdateCustomOrgRole(ctx, "o", 8030, opts) + if err != nil { + t.Errorf("Organizations.UpdateCustomOrgRole with empty permission returned error: %v", err) + } + want.Permissions = []string{} + + if !cmp.Equal(emptyPermissionRole, want) { + t.Errorf("Organizations.UpdateCustomOrgRole with empty permission returned %+v, want %+v", emptyPermissionRole, want) + } } func TestOrganizationsService_DeleteCustomOrgRole(t *testing.T) { @@ -334,6 +358,18 @@ func TestOrganizationsService_CreateCustomRepoRole(t *testing.T) { } return resp, err }) + + opts.Permissions = []string{} + + emptyPermissionRole, _, err := client.Organizations.CreateCustomRepoRole(ctx, "o", opts) + if err != nil { + t.Errorf("Organizations.CreateCustomRepoRole with empty permission returned error: %v", err) + } + want.Permissions = []string{} + + if !cmp.Equal(emptyPermissionRole, want) { + t.Errorf("Organizations.CreateCustomRepoRole with empty permission returned %+v, want %+v", emptyPermissionRole, want) + } } func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) { @@ -375,6 +411,18 @@ func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) { } return resp, err }) + + opts.Permissions = []string{} + + emptyPermissionRole, _, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, opts) + if err != nil { + t.Errorf("Organizations.UpdateCustomRepoRole with empty permission returned error: %v", err) + } + want.Permissions = []string{} + + if !cmp.Equal(emptyPermissionRole, want) { + t.Errorf("Organizations.UpdateCustomRepoRole with empty permission returned %+v, want %+v", emptyPermissionRole, want) + } } func TestOrganizationsService_DeleteCustomRepoRole(t *testing.T) {