Skip to content

Commit

Permalink
Allow create/update custom repository/organization roles without perm…
Browse files Browse the repository at this point in the history
…issions google#3226

Signed-off-by: Andríyun <[email protected]>
  • Loading branch information
Andríyun committed Aug 15, 2024
1 parent f5d2850 commit c22ae81
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 4 deletions.
64 changes: 60 additions & 4 deletions github/orgs_custom_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down Expand Up @@ -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
}
Expand All @@ -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
}
Expand Down
48 changes: 48 additions & 0 deletions github/orgs_custom_roles_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit c22ae81

Please sign in to comment.