Skip to content

Commit

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

Signed-off-by: Andríyun <[email protected]>
  • Loading branch information
Andríyun committed Aug 14, 2024
1 parent f5d2850 commit 27c0bcf
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 2 deletions.
32 changes: 30 additions & 2 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 set change the type of Permission property, so int will not be omited during coversion to JSON.

Check failure on line 102 in github/orgs_custom_roles.go

View workflow job for this annotation

GitHub Actions / lint

`omited` is a misspelling of `omitted` (misspell)
if 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 Down Expand Up @@ -188,7 +202,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 set change the type of Permission property, so int will not be omited during coversion to JSON.

Check failure on line 208 in github/orgs_custom_roles.go

View workflow job for this annotation

GitHub Actions / lint

`omited` is a misspelling of `omitted` (misspell)
if 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 Down
24 changes: 24 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{}

gotRolePermission, _, 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(gotRolePermission, want) {
t.Errorf("Organizations.CreateCustomOrgRole with empty permission returned %+v, want %+v", gotRolePermission, want)
}
}

func TestOrganizationsService_UpdateCustomOrgRole(t *testing.T) {
Expand Down Expand Up @@ -334,6 +346,18 @@ func TestOrganizationsService_CreateCustomRepoRole(t *testing.T) {
}
return resp, err
})

opts.Permissions = []string{}

gotRolePermission, _, 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(gotRolePermission, want) {
t.Errorf("Organizations.CreateCustomRepoRole with empty permission returned %+v, want %+v", gotRolePermission, want)
}
}

func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) {
Expand Down

0 comments on commit 27c0bcf

Please sign in to comment.