Skip to content

Commit c22ae81

Browse files
author
Andríyun
committed
Allow create/update custom repository/organization roles without permissions #3226
Signed-off-by: Andríyun <[email protected]>
1 parent f5d2850 commit c22ae81

File tree

2 files changed

+108
-4
lines changed

2 files changed

+108
-4
lines changed

github/orgs_custom_roles.go

+60-4
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,21 @@ func (s *OrganizationsService) ListRoles(ctx context.Context, org string) (*Orga
9696
func (s *OrganizationsService) CreateCustomOrgRole(ctx context.Context, org string, opts *CreateOrUpdateOrgRoleOptions) (*CustomOrgRoles, *Response, error) {
9797
u := fmt.Sprintf("orgs/%v/organization-roles", org)
9898

99-
req, err := s.client.NewRequest("POST", u, opts)
99+
var params interface{}
100+
params = opts
101+
102+
// For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON.
103+
if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 {
104+
params = struct {
105+
*CreateOrUpdateOrgRoleOptions
106+
Permissions []string `json:"permissions"`
107+
}{
108+
CreateOrUpdateOrgRoleOptions: opts,
109+
Permissions: opts.Permissions,
110+
}
111+
}
112+
113+
req, err := s.client.NewRequest("POST", u, params)
100114
if err != nil {
101115
return nil, nil, err
102116
}
@@ -119,7 +133,21 @@ func (s *OrganizationsService) CreateCustomOrgRole(ctx context.Context, org stri
119133
func (s *OrganizationsService) UpdateCustomOrgRole(ctx context.Context, org string, roleID int64, opts *CreateOrUpdateOrgRoleOptions) (*CustomOrgRoles, *Response, error) {
120134
u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID)
121135

122-
req, err := s.client.NewRequest("PATCH", u, opts)
136+
var params interface{}
137+
params = opts
138+
139+
// For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON.
140+
if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 {
141+
params = struct {
142+
*CreateOrUpdateOrgRoleOptions
143+
Permissions []string `json:"permissions"`
144+
}{
145+
CreateOrUpdateOrgRoleOptions: opts,
146+
Permissions: opts.Permissions,
147+
}
148+
}
149+
150+
req, err := s.client.NewRequest("PATCH", u, params)
123151
if err != nil {
124152
return nil, nil, err
125153
}
@@ -188,7 +216,21 @@ func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org stri
188216
func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
189217
u := fmt.Sprintf("orgs/%v/custom-repository-roles", org)
190218

191-
req, err := s.client.NewRequest("POST", u, opts)
219+
var params interface{}
220+
params = opts
221+
222+
// For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON.
223+
if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 {
224+
params = struct {
225+
*CreateOrUpdateCustomRepoRoleOptions
226+
Permissions []string `json:"permissions"`
227+
}{
228+
CreateOrUpdateCustomRepoRoleOptions: opts,
229+
Permissions: opts.Permissions,
230+
}
231+
}
232+
233+
req, err := s.client.NewRequest("POST", u, params)
192234
if err != nil {
193235
return nil, nil, err
194236
}
@@ -211,7 +253,21 @@ func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org str
211253
func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org string, roleID int64, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
212254
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)
213255

214-
req, err := s.client.NewRequest("PATCH", u, opts)
256+
var params interface{}
257+
params = opts
258+
259+
// For empty Permissions property change the type of the property, so it will not be omitted during coversion to JSON.
260+
if opts != nil && opts.Permissions != nil && len(opts.Permissions) == 0 {
261+
params = struct {
262+
*CreateOrUpdateCustomRepoRoleOptions
263+
Permissions []string `json:"permissions"`
264+
}{
265+
CreateOrUpdateCustomRepoRoleOptions: opts,
266+
Permissions: opts.Permissions,
267+
}
268+
}
269+
270+
req, err := s.client.NewRequest("PATCH", u, params)
215271
if err != nil {
216272
return nil, nil, err
217273
}

github/orgs_custom_roles_test.go

+48
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,18 @@ func TestOrganizationsService_CreateCustomOrgRole(t *testing.T) {
138138
}
139139
return resp, err
140140
})
141+
142+
opts.Permissions = []string{}
143+
144+
emptyPermissionRole, _, err := client.Organizations.CreateCustomOrgRole(ctx, "o", opts)
145+
if err != nil {
146+
t.Errorf("Organizations.CreateCustomOrgRole with empty permission returned error: %v", err)
147+
}
148+
want.Permissions = []string{}
149+
150+
if !cmp.Equal(emptyPermissionRole, want) {
151+
t.Errorf("Organizations.CreateCustomOrgRole with empty permission returned %+v, want %+v", emptyPermissionRole, want)
152+
}
141153
}
142154

143155
func TestOrganizationsService_UpdateCustomOrgRole(t *testing.T) {
@@ -179,6 +191,18 @@ func TestOrganizationsService_UpdateCustomOrgRole(t *testing.T) {
179191
}
180192
return resp, err
181193
})
194+
195+
opts.Permissions = []string{}
196+
197+
emptyPermissionRole, _, err := client.Organizations.UpdateCustomOrgRole(ctx, "o", 8030, opts)
198+
if err != nil {
199+
t.Errorf("Organizations.UpdateCustomOrgRole with empty permission returned error: %v", err)
200+
}
201+
want.Permissions = []string{}
202+
203+
if !cmp.Equal(emptyPermissionRole, want) {
204+
t.Errorf("Organizations.UpdateCustomOrgRole with empty permission returned %+v, want %+v", emptyPermissionRole, want)
205+
}
182206
}
183207

184208
func TestOrganizationsService_DeleteCustomOrgRole(t *testing.T) {
@@ -334,6 +358,18 @@ func TestOrganizationsService_CreateCustomRepoRole(t *testing.T) {
334358
}
335359
return resp, err
336360
})
361+
362+
opts.Permissions = []string{}
363+
364+
emptyPermissionRole, _, err := client.Organizations.CreateCustomRepoRole(ctx, "o", opts)
365+
if err != nil {
366+
t.Errorf("Organizations.CreateCustomRepoRole with empty permission returned error: %v", err)
367+
}
368+
want.Permissions = []string{}
369+
370+
if !cmp.Equal(emptyPermissionRole, want) {
371+
t.Errorf("Organizations.CreateCustomRepoRole with empty permission returned %+v, want %+v", emptyPermissionRole, want)
372+
}
337373
}
338374

339375
func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) {
@@ -375,6 +411,18 @@ func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) {
375411
}
376412
return resp, err
377413
})
414+
415+
opts.Permissions = []string{}
416+
417+
emptyPermissionRole, _, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, opts)
418+
if err != nil {
419+
t.Errorf("Organizations.UpdateCustomRepoRole with empty permission returned error: %v", err)
420+
}
421+
want.Permissions = []string{}
422+
423+
if !cmp.Equal(emptyPermissionRole, want) {
424+
t.Errorf("Organizations.UpdateCustomRepoRole with empty permission returned %+v, want %+v", emptyPermissionRole, want)
425+
}
378426
}
379427

380428
func TestOrganizationsService_DeleteCustomRepoRole(t *testing.T) {

0 commit comments

Comments
 (0)