Skip to content

Commit cd59fac

Browse files
authored
refactor: Refactor org_custom_roles.go into multiple files (#3291)
1 parent 8faa07c commit cd59fac

File tree

4 files changed

+339
-312
lines changed

4 files changed

+339
-312
lines changed
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// Copyright 2024 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
)
12+
13+
// OrganizationCustomRepoRoles represents custom repository roles available in specified organization.
14+
type OrganizationCustomRepoRoles struct {
15+
TotalCount *int `json:"total_count,omitempty"`
16+
CustomRepoRoles []*CustomRepoRoles `json:"custom_roles,omitempty"`
17+
}
18+
19+
// CustomRepoRoles represents custom repository roles for an organization.
20+
// See https://docs.github.com/enterprise-cloud@latest/organizations/managing-peoples-access-to-your-organization-with-roles/managing-custom-repository-roles-for-an-organization
21+
// for more information.
22+
type CustomRepoRoles struct {
23+
ID *int64 `json:"id,omitempty"`
24+
Name *string `json:"name,omitempty"`
25+
Description *string `json:"description,omitempty"`
26+
BaseRole *string `json:"base_role,omitempty"`
27+
Permissions []string `json:"permissions,omitempty"`
28+
Org *Organization `json:"organization,omitempty"`
29+
CreatedAt *Timestamp `json:"created_at,omitempty"`
30+
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
31+
}
32+
33+
// CreateOrUpdateCustomRepoRoleOptions represents options required to create or update a custom repository role.
34+
type CreateOrUpdateCustomRepoRoleOptions struct {
35+
Name *string `json:"name,omitempty"`
36+
Description *string `json:"description,omitempty"`
37+
BaseRole *string `json:"base_role,omitempty"`
38+
Permissions []string `json:"permissions"`
39+
}
40+
41+
// ListCustomRepoRoles lists the custom repository roles available in this organization.
42+
// In order to see custom repository roles in an organization, the authenticated user must be an organization owner.
43+
//
44+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#list-custom-repository-roles-in-an-organization
45+
//
46+
//meta:operation GET /orgs/{org}/custom-repository-roles
47+
func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrganizationCustomRepoRoles, *Response, error) {
48+
u := fmt.Sprintf("orgs/%v/custom-repository-roles", org)
49+
50+
req, err := s.client.NewRequest("GET", u, nil)
51+
if err != nil {
52+
return nil, nil, err
53+
}
54+
55+
customRepoRoles := new(OrganizationCustomRepoRoles)
56+
resp, err := s.client.Do(ctx, req, customRepoRoles)
57+
if err != nil {
58+
return nil, resp, err
59+
}
60+
61+
return customRepoRoles, resp, nil
62+
}
63+
64+
// CreateCustomRepoRole creates a custom repository role in this organization.
65+
// In order to create custom repository roles in an organization, the authenticated user must be an organization owner.
66+
//
67+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#create-a-custom-repository-role
68+
//
69+
//meta:operation POST /orgs/{org}/custom-repository-roles
70+
func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
71+
u := fmt.Sprintf("orgs/%v/custom-repository-roles", org)
72+
73+
req, err := s.client.NewRequest("POST", u, opts)
74+
if err != nil {
75+
return nil, nil, err
76+
}
77+
78+
resultingRole := new(CustomRepoRoles)
79+
resp, err := s.client.Do(ctx, req, resultingRole)
80+
if err != nil {
81+
return nil, resp, err
82+
}
83+
84+
return resultingRole, resp, err
85+
}
86+
87+
// UpdateCustomRepoRole updates a custom repository role in this organization.
88+
// In order to update custom repository roles in an organization, the authenticated user must be an organization owner.
89+
//
90+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#update-a-custom-repository-role
91+
//
92+
//meta:operation PATCH /orgs/{org}/custom-repository-roles/{role_id}
93+
func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org string, roleID int64, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
94+
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)
95+
96+
req, err := s.client.NewRequest("PATCH", u, opts)
97+
if err != nil {
98+
return nil, nil, err
99+
}
100+
101+
resultingRole := new(CustomRepoRoles)
102+
resp, err := s.client.Do(ctx, req, resultingRole)
103+
if err != nil {
104+
return nil, resp, err
105+
}
106+
107+
return resultingRole, resp, err
108+
}
109+
110+
// DeleteCustomRepoRole deletes an existing custom repository role in this organization.
111+
// In order to delete custom repository roles in an organization, the authenticated user must be an organization owner.
112+
//
113+
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#delete-a-custom-repository-role
114+
//
115+
//meta:operation DELETE /orgs/{org}/custom-repository-roles/{role_id}
116+
func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org string, roleID int64) (*Response, error) {
117+
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)
118+
119+
req, err := s.client.NewRequest("DELETE", u, nil)
120+
if err != nil {
121+
return nil, err
122+
}
123+
124+
resultingRole := new(CustomRepoRoles)
125+
resp, err := s.client.Do(ctx, req, resultingRole)
126+
if err != nil {
127+
return resp, err
128+
}
129+
130+
return resp, nil
131+
}
Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
// Copyright 2024 The go-github AUTHORS. All rights reserved.
2+
//
3+
// Use of this source code is governed by a BSD-style
4+
// license that can be found in the LICENSE file.
5+
6+
package github
7+
8+
import (
9+
"context"
10+
"fmt"
11+
"net/http"
12+
"testing"
13+
"time"
14+
15+
"github.com/google/go-cmp/cmp"
16+
)
17+
18+
func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) {
19+
client, mux, _, teardown := setup()
20+
defer teardown()
21+
22+
mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) {
23+
testMethod(t, r, "GET")
24+
fmt.Fprint(w, `{"total_count": 1, "custom_roles": [
25+
{
26+
"id": 1,
27+
"name": "Developer",
28+
"base_role": "write",
29+
"permissions": ["delete_alerts_code_scanning"],
30+
"organization": {
31+
"login": "l",
32+
"id": 1,
33+
"node_id": "n",
34+
"avatar_url": "a",
35+
"html_url": "h",
36+
"name": "n",
37+
"company": "c",
38+
"blog": "b",
39+
"location": "l",
40+
"email": "e"
41+
},
42+
"created_at": "2024-07-21T19:33:08Z",
43+
"updated_at": "2024-07-21T19:33:08Z"
44+
}
45+
]
46+
}`)
47+
})
48+
49+
ctx := context.Background()
50+
apps, _, err := client.Organizations.ListCustomRepoRoles(ctx, "o")
51+
if err != nil {
52+
t.Errorf("Organizations.ListCustomRepoRoles returned error: %v", err)
53+
}
54+
55+
want := &OrganizationCustomRepoRoles{
56+
TotalCount: Int(1),
57+
CustomRepoRoles: []*CustomRepoRoles{
58+
{
59+
ID: Int64(1),
60+
Name: String("Developer"),
61+
BaseRole: String("write"),
62+
Permissions: []string{"delete_alerts_code_scanning"},
63+
Org: &Organization{
64+
Login: String("l"),
65+
ID: Int64(1),
66+
NodeID: String("n"),
67+
AvatarURL: String("a"),
68+
HTMLURL: String("h"),
69+
Name: String("n"),
70+
Company: String("c"),
71+
Blog: String("b"),
72+
Location: String("l"),
73+
Email: String("e"),
74+
},
75+
CreatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)},
76+
UpdatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)},
77+
},
78+
},
79+
}
80+
if !cmp.Equal(apps, want) {
81+
t.Errorf("Organizations.ListCustomRepoRoles returned %+v, want %+v", apps, want)
82+
}
83+
84+
const methodName = "ListCustomRepoRoles"
85+
testBadOptions(t, methodName, func() (err error) {
86+
_, _, err = client.Organizations.ListCustomRepoRoles(ctx, "\no")
87+
return err
88+
})
89+
90+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
91+
got, resp, err := client.Organizations.ListCustomRepoRoles(ctx, "o")
92+
if got != nil {
93+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
94+
}
95+
return resp, err
96+
})
97+
}
98+
99+
func TestOrganizationsService_CreateCustomRepoRole(t *testing.T) {
100+
client, mux, _, teardown := setup()
101+
defer teardown()
102+
103+
mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) {
104+
testMethod(t, r, "POST")
105+
fmt.Fprint(w, `{"id":8030,"name":"Labeler","description":"A role for issue and PR labelers","base_role":"read","permissions":["add_label"]}`)
106+
})
107+
108+
ctx := context.Background()
109+
110+
opts := &CreateOrUpdateCustomRepoRoleOptions{
111+
Name: String("Labeler"),
112+
Description: String("A role for issue and PR labelers"),
113+
BaseRole: String("read"),
114+
Permissions: []string{"add_label"},
115+
}
116+
apps, _, err := client.Organizations.CreateCustomRepoRole(ctx, "o", opts)
117+
if err != nil {
118+
t.Errorf("Organizations.CreateCustomRepoRole returned error: %v", err)
119+
}
120+
121+
want := &CustomRepoRoles{ID: Int64(8030), Name: String("Labeler"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("A role for issue and PR labelers")}
122+
123+
if !cmp.Equal(apps, want) {
124+
t.Errorf("Organizations.CreateCustomRepoRole returned %+v, want %+v", apps, want)
125+
}
126+
127+
const methodName = "CreateCustomRepoRole"
128+
testBadOptions(t, methodName, func() (err error) {
129+
_, _, err = client.Organizations.CreateCustomRepoRole(ctx, "\no", nil)
130+
return err
131+
})
132+
133+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
134+
got, resp, err := client.Organizations.CreateCustomRepoRole(ctx, "o", nil)
135+
if got != nil {
136+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
137+
}
138+
return resp, err
139+
})
140+
}
141+
142+
func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) {
143+
client, mux, _, teardown := setup()
144+
defer teardown()
145+
146+
mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) {
147+
testMethod(t, r, "PATCH")
148+
fmt.Fprint(w, `{"id":8030,"name":"Updated Name","description":"Updated Description","base_role":"read","permissions":["add_label"]}`)
149+
})
150+
151+
ctx := context.Background()
152+
153+
opts := &CreateOrUpdateCustomRepoRoleOptions{
154+
Name: String("Updated Name"),
155+
Description: String("Updated Description"),
156+
}
157+
apps, _, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, opts)
158+
if err != nil {
159+
t.Errorf("Organizations.UpdateCustomRepoRole returned error: %v", err)
160+
}
161+
162+
want := &CustomRepoRoles{ID: Int64(8030), Name: String("Updated Name"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("Updated Description")}
163+
164+
if !cmp.Equal(apps, want) {
165+
t.Errorf("Organizations.UpdateCustomRepoRole returned %+v, want %+v", apps, want)
166+
}
167+
168+
const methodName = "UpdateCustomRepoRole"
169+
testBadOptions(t, methodName, func() (err error) {
170+
_, _, err = client.Organizations.UpdateCustomRepoRole(ctx, "\no", 8030, nil)
171+
return err
172+
})
173+
174+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
175+
got, resp, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, nil)
176+
if got != nil {
177+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
178+
}
179+
return resp, err
180+
})
181+
}
182+
183+
func TestOrganizationsService_DeleteCustomRepoRole(t *testing.T) {
184+
client, mux, _, teardown := setup()
185+
defer teardown()
186+
187+
mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) {
188+
testMethod(t, r, "DELETE")
189+
w.WriteHeader(http.StatusNoContent)
190+
})
191+
192+
ctx := context.Background()
193+
194+
resp, err := client.Organizations.DeleteCustomRepoRole(ctx, "o", 8030)
195+
if err != nil {
196+
t.Errorf("Organizations.DeleteCustomRepoRole returned error: %v", err)
197+
}
198+
199+
if !cmp.Equal(resp.StatusCode, 204) {
200+
t.Errorf("Organizations.DeleteCustomRepoRole returned status code %+v, want %+v", resp.StatusCode, "204")
201+
}
202+
203+
const methodName = "DeleteCustomRepoRole"
204+
testBadOptions(t, methodName, func() (err error) {
205+
_, err = client.Organizations.DeleteCustomRepoRole(ctx, "\no", 8030)
206+
return err
207+
})
208+
}

0 commit comments

Comments
 (0)