Skip to content

Commit 448e04d

Browse files
authored
Add support to list custom roles for organizations (#2336)
Fixes: #2327
1 parent 00e4233 commit 448e04d

File tree

4 files changed

+150
-0
lines changed

4 files changed

+150
-0
lines changed

github/github-accessors.go

Lines changed: 24 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/github-accessors_test.go

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

github/org_custom_roles.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// Copyright 2022 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/en/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+
}
26+
27+
// ListCustomRepoRoles lists the custom repository roles available in this organization.
28+
// In order to see custom repository roles in an organization, the authenticated user must be an organization owner.
29+
//
30+
// GitHub API docs: https://docs.github.com/en/rest/orgs/custom-roles
31+
func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrganizationCustomRepoRoles, *Response, error) {
32+
u := fmt.Sprintf("organizations/%v/custom_roles", org)
33+
34+
req, err := s.client.NewRequest("GET", u, nil)
35+
if err != nil {
36+
return nil, nil, err
37+
}
38+
39+
customRepoRoles := new(OrganizationCustomRepoRoles)
40+
resp, err := s.client.Do(ctx, req, customRepoRoles)
41+
if err != nil {
42+
return nil, resp, err
43+
}
44+
45+
return customRepoRoles, resp, nil
46+
}

github/org_custom_roles_test.go

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// Copyright 2022 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+
14+
"github.com/google/go-cmp/cmp"
15+
)
16+
17+
func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) {
18+
client, mux, _, teardown := setup()
19+
defer teardown()
20+
21+
mux.HandleFunc("/organizations/o/custom_roles", func(w http.ResponseWriter, r *http.Request) {
22+
testMethod(t, r, "GET")
23+
fmt.Fprint(w, `{"total_count": 1, "custom_roles": [{ "id": 1, "name": "Developer"}]}`)
24+
})
25+
26+
ctx := context.Background()
27+
apps, _, err := client.Organizations.ListCustomRepoRoles(ctx, "o")
28+
if err != nil {
29+
t.Errorf("Organizations.ListCustomRepoRoles returned error: %v", err)
30+
}
31+
32+
want := &OrganizationCustomRepoRoles{TotalCount: Int(1), CustomRepoRoles: []*CustomRepoRoles{{ID: Int64(1), Name: String("Developer")}}}
33+
if !cmp.Equal(apps, want) {
34+
t.Errorf("Organizations.ListCustomRepoRoles returned %+v, want %+v", apps, want)
35+
}
36+
37+
const methodName = "ListCustomRepoRoles"
38+
testBadOptions(t, methodName, func() (err error) {
39+
_, _, err = client.Organizations.ListCustomRepoRoles(ctx, "\no")
40+
return err
41+
})
42+
43+
testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
44+
got, resp, err := client.Organizations.ListCustomRepoRoles(ctx, "o")
45+
if got != nil {
46+
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
47+
}
48+
return resp, err
49+
})
50+
}

0 commit comments

Comments
 (0)