Skip to content

Commit e10a8d7

Browse files
committed
feat: Added get for custom org repo role
Signed-off-by: Steve Hipwell <[email protected]>
1 parent f0bfdaf commit e10a8d7

File tree

2 files changed

+101
-0
lines changed

2 files changed

+101
-0
lines changed

github/orgs_custom_repository_roles.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,29 @@ func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org stri
6161
return customRepoRoles, resp, nil
6262
}
6363

64+
// GetCustomRepoRole gets a custom repository roles available in this organization.
65+
// In order to see custom repository roles in an organization, the authenticated user must be an organization owner.
66+
//
67+
// GitHub API docs: https://docs.github.com/en/enterprise-cloud@latest/rest/orgs/custom-roles#get-a-custom-repository-role
68+
//
69+
//meta:operation GET /orgs/{org}/custom-repository-roles/{role_id}
70+
func (s *OrganizationsService) GetCustomRepoRole(ctx context.Context, org string, roleID int64) (*CustomRepoRoles, *Response, error) {
71+
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)
72+
73+
req, err := s.client.NewRequest("GET", u, nil)
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, nil
85+
}
86+
6487
// CreateCustomRepoRole creates a custom repository role in this organization.
6588
// In order to create custom repository roles in an organization, the authenticated user must be an organization owner.
6689
//

github/orgs_custom_repository_roles_test.go

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,84 @@ func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) {
9696
})
9797
}
9898

99+
func TestOrganizationsService_GetCustomRepoRole(t *testing.T) {
100+
t.Parallel()
101+
client, mux, _ := setup(t)
102+
103+
mux.HandleFunc("/orgs/o/custom-repository-roles/1", func(w http.ResponseWriter, r *http.Request) {
104+
testMethod(t, r, "GET")
105+
fmt.Fprint(w, `{
106+
"id": 1,
107+
"name": "Developer",
108+
"base_role": "write",
109+
"permissions": ["delete_alerts_code_scanning"],
110+
"organization": {
111+
"login": "l",
112+
"id": 1,
113+
"node_id": "n",
114+
"avatar_url": "a",
115+
"html_url": "h",
116+
"name": "n",
117+
"company": "c",
118+
"blog": "b",
119+
"location": "l",
120+
"email": "e"
121+
},
122+
"created_at": "2024-07-21T19:33:08Z",
123+
"updated_at": "2024-07-21T19:33:08Z"
124+
}`)
125+
})
126+
127+
ctx := context.Background()
128+
role, _, err := client.Organizations.GetCustomRepoRole(ctx, "o", 1)
129+
if err != nil {
130+
t.Errorf("Organizations.GetCustomRepoRole returned error: %v", err)
131+
}
132+
133+
want := &CustomRepoRoles{
134+
ID: Int64(1),
135+
Name: String("Developer"),
136+
BaseRole: String("write"),
137+
Permissions: []string{"delete_alerts_code_scanning"},
138+
Org: &Organization{
139+
Login: String("l"),
140+
ID: Int64(1),
141+
NodeID: String("n"),
142+
AvatarURL: String("a"),
143+
HTMLURL: String("h"),
144+
Name: String("n"),
145+
Company: String("c"),
146+
Blog: String("b"),
147+
Location: String("l"),
148+
Email: String("e"),
149+
},
150+
CreatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)},
151+
UpdatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)},
152+
}
153+
if !cmp.Equal(role, want) {
154+
t.Errorf("Organizations.GetCustomRepoRole returned %+v, want %+v", role, want)
155+
}
156+
157+
const methodName = "GetCustomRepoRole"
158+
testBadOptions(t, methodName, func() (err error) {
159+
_, _, err = client.Organizations.GetCustomRepoRole(ctx, "\no", 1)
160+
return err
161+
})
162+
163+
testBadOptions(t, methodName, func() (err error) {
164+
_, _, err = client.Organizations.GetCustomRepoRole(ctx, "o", -1)
165+
return err
166+
})
167+
168+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
169+
got, resp, err := client.Organizations.GetCustomRepoRole(ctx, "o", 1)
170+
if got != nil {
171+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
172+
}
173+
return resp, err
174+
})
175+
}
176+
99177
func TestOrganizationsService_CreateCustomRepoRole(t *testing.T) {
100178
t.Parallel()
101179
client, mux, _ := setup(t)

0 commit comments

Comments
 (0)