Skip to content

Commit 44acf10

Browse files
Merge branch 'master' into feat/revoke-org-invites
2 parents 2c4880f + c0f5841 commit 44acf10

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

github/orgs_custom_roles.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,29 @@ func (s *OrganizationsService) ListRoles(ctx context.Context, org string) (*Orga
8888
return customRepoRoles, resp, nil
8989
}
9090

91+
// GetOrgRole gets an organization role in this organization.
92+
// In order to get organization roles in an organization, the authenticated user must be an organization owner, or have access via an organization role.
93+
//
94+
// GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-an-organization-role
95+
//
96+
//meta:operation GET /orgs/{org}/organization-roles/{role_id}
97+
func (s *OrganizationsService) GetOrgRole(ctx context.Context, org string, roleID int64) (*CustomOrgRoles, *Response, error) {
98+
u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID)
99+
100+
req, err := s.client.NewRequest("GET", u, nil)
101+
if err != nil {
102+
return nil, nil, err
103+
}
104+
105+
resultingRole := new(CustomOrgRoles)
106+
resp, err := s.client.Do(ctx, req, resultingRole)
107+
if err != nil {
108+
return nil, resp, err
109+
}
110+
111+
return resultingRole, resp, err
112+
}
113+
91114
// CreateCustomOrgRole creates a custom role in this organization.
92115
// In order to create custom roles in an organization, the authenticated user must be an organization owner.
93116
//

github/orgs_custom_roles_test.go

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,105 @@ func TestOrganizationsService_ListRoles(t *testing.T) {
9898
})
9999
}
100100

101+
func TestOrganizationsService_GetOrgRole(t *testing.T) {
102+
client, mux, _, teardown := setup()
103+
defer teardown()
104+
105+
// Test built-in org role
106+
mux.HandleFunc("/orgs/o/organization-roles/8132", func(w http.ResponseWriter, r *http.Request) {
107+
testMethod(t, r, "GET")
108+
fmt.Fprint(w, `{
109+
"id": 8132,
110+
"name": "all_repo_read",
111+
"description": "Grants read access to all repositories in the organization.",
112+
"permissions": [],
113+
"created_at": `+referenceTimeStr+`,
114+
"updated_at": `+referenceTimeStr+`,
115+
"source": "Predefined",
116+
"base_role": "read"
117+
}`)
118+
})
119+
120+
ctx := context.Background()
121+
122+
gotBuiltInRole, _, err := client.Organizations.GetOrgRole(ctx, "o", 8132)
123+
if err != nil {
124+
t.Errorf("Organizations.GetOrgRole returned error: %v", err)
125+
}
126+
127+
wantBuiltInRole := &CustomOrgRoles{
128+
ID: Int64(8132),
129+
Name: String("all_repo_read"),
130+
Description: String("Grants read access to all repositories in the organization."),
131+
Permissions: []string{},
132+
CreatedAt: &Timestamp{referenceTime},
133+
UpdatedAt: &Timestamp{referenceTime},
134+
Source: String("Predefined"),
135+
BaseRole: String("read"),
136+
}
137+
138+
if !cmp.Equal(gotBuiltInRole, wantBuiltInRole) {
139+
t.Errorf("Organizations.GetOrgRole returned %+v, want %+v", gotBuiltInRole, wantBuiltInRole)
140+
}
141+
142+
// Test custom org role
143+
mux.HandleFunc("/orgs/o/organization-roles/123456", func(w http.ResponseWriter, r *http.Request) {
144+
testMethod(t, r, "GET")
145+
fmt.Fprint(w, `{
146+
"id": 123456,
147+
"name": "test-role",
148+
"description": "test-role",
149+
"permissions": [
150+
"read_organization_custom_org_role",
151+
"read_organization_custom_repo_role",
152+
"write_organization_custom_org_role"
153+
],
154+
"created_at": `+referenceTimeStr+`,
155+
"updated_at": `+referenceTimeStr+`,
156+
"source": "Organization",
157+
"base_role": null
158+
}`)
159+
})
160+
161+
gotCustomRole, _, err := client.Organizations.GetOrgRole(ctx, "o", 123456)
162+
if err != nil {
163+
t.Errorf("Organizations.GetOrgRole returned error: %v", err)
164+
}
165+
166+
wantCustomRole := &CustomOrgRoles{
167+
ID: Int64(123456),
168+
Name: String("test-role"),
169+
Description: String("test-role"),
170+
Permissions: []string{
171+
"read_organization_custom_org_role",
172+
"read_organization_custom_repo_role",
173+
"write_organization_custom_org_role",
174+
},
175+
CreatedAt: &Timestamp{referenceTime},
176+
UpdatedAt: &Timestamp{referenceTime},
177+
Source: String("Organization"),
178+
BaseRole: nil,
179+
}
180+
181+
if !cmp.Equal(gotCustomRole, wantCustomRole) {
182+
t.Errorf("Organizations.GetOrgRole returned %+v, want %+v", gotCustomRole, wantCustomRole)
183+
}
184+
185+
const methodName = "GetOrgRole"
186+
testBadOptions(t, methodName, func() (err error) {
187+
_, _, err = client.Organizations.GetOrgRole(ctx, "\no", -8132)
188+
return err
189+
})
190+
191+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
192+
got, resp, err := client.Organizations.GetOrgRole(ctx, "o", 8132)
193+
if got != nil {
194+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
195+
}
196+
return resp, err
197+
})
198+
}
199+
101200
func TestOrganizationsService_CreateCustomOrgRole(t *testing.T) {
102201
client, mux, _, teardown := setup()
103202
defer teardown()

0 commit comments

Comments
 (0)