diff --git a/github/github-accessors.go b/github/github-accessors.go index 724e67f0d3..d016a36bcb 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -3542,6 +3542,22 @@ func (c *CreateUserProjectOptions) GetBody() string { return *c.Body } +// GetID returns the ID field if it's non-nil, zero value otherwise. +func (c *CustomRepoRoles) GetID() int64 { + if c == nil || c.ID == nil { + return 0 + } + return *c.ID +} + +// GetName returns the Name field if it's non-nil, zero value otherwise. +func (c *CustomRepoRoles) GetName() string { + if c == nil || c.Name == nil { + return "" + } + return *c.Name +} + // GetInstallation returns the Installation field. func (d *DeleteEvent) GetInstallation() *Installation { if d == nil { @@ -9686,6 +9702,14 @@ func (o *Organization) GetURL() string { return *o.URL } +// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. +func (o *OrganizationCustomRepoRoles) GetTotalCount() int { + if o == nil || o.TotalCount == nil { + return 0 + } + return *o.TotalCount +} + // GetAction returns the Action field if it's non-nil, zero value otherwise. func (o *OrganizationEvent) GetAction() string { if o == nil || o.Action == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 5ac8d7b9a6..3e2108dfc3 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -4146,6 +4146,26 @@ func TestCreateUserProjectOptions_GetBody(tt *testing.T) { c.GetBody() } +func TestCustomRepoRoles_GetID(tt *testing.T) { + var zeroValue int64 + c := &CustomRepoRoles{ID: &zeroValue} + c.GetID() + c = &CustomRepoRoles{} + c.GetID() + c = nil + c.GetID() +} + +func TestCustomRepoRoles_GetName(tt *testing.T) { + var zeroValue string + c := &CustomRepoRoles{Name: &zeroValue} + c.GetName() + c = &CustomRepoRoles{} + c.GetName() + c = nil + c.GetName() +} + func TestDeleteEvent_GetInstallation(tt *testing.T) { d := &DeleteEvent{} d.GetInstallation() @@ -11376,6 +11396,16 @@ func TestOrganization_GetURL(tt *testing.T) { o.GetURL() } +func TestOrganizationCustomRepoRoles_GetTotalCount(tt *testing.T) { + var zeroValue int + o := &OrganizationCustomRepoRoles{TotalCount: &zeroValue} + o.GetTotalCount() + o = &OrganizationCustomRepoRoles{} + o.GetTotalCount() + o = nil + o.GetTotalCount() +} + func TestOrganizationEvent_GetAction(tt *testing.T) { var zeroValue string o := &OrganizationEvent{Action: &zeroValue} diff --git a/github/org_custom_roles.go b/github/org_custom_roles.go new file mode 100644 index 0000000000..7c60fea07b --- /dev/null +++ b/github/org_custom_roles.go @@ -0,0 +1,46 @@ +// Copyright 2022 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" +) + +// OrganizationCustomRepoRoles represents custom repository roles available in specified organization. +type OrganizationCustomRepoRoles struct { + TotalCount *int `json:"total_count,omitempty"` + CustomRepoRoles []*CustomRepoRoles `json:"custom_roles,omitempty"` +} + +// CustomRepoRoles represents custom repository roles for an organization. +// 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 +// for more information. +type CustomRepoRoles struct { + ID *int64 `json:"id,omitempty"` + Name *string `json:"name,omitempty"` +} + +// ListCustomRepoRoles lists the custom repository roles available in this organization. +// In order to see custom repository roles in an organization, the authenticated user must be an organization owner. +// +// GitHub API docs: https://docs.github.com/en/rest/orgs/custom-roles +func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrganizationCustomRepoRoles, *Response, error) { + u := fmt.Sprintf("organizations/%v/custom_roles", org) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + customRepoRoles := new(OrganizationCustomRepoRoles) + resp, err := s.client.Do(ctx, req, customRepoRoles) + if err != nil { + return nil, resp, err + } + + return customRepoRoles, resp, nil +} diff --git a/github/org_custom_roles_test.go b/github/org_custom_roles_test.go new file mode 100644 index 0000000000..cfcb02611a --- /dev/null +++ b/github/org_custom_roles_test.go @@ -0,0 +1,50 @@ +// Copyright 2022 The go-github AUTHORS. All rights reserved. +// +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) { + client, mux, _, teardown := setup() + defer teardown() + + mux.HandleFunc("/organizations/o/custom_roles", func(w http.ResponseWriter, r *http.Request) { + testMethod(t, r, "GET") + fmt.Fprint(w, `{"total_count": 1, "custom_roles": [{ "id": 1, "name": "Developer"}]}`) + }) + + ctx := context.Background() + apps, _, err := client.Organizations.ListCustomRepoRoles(ctx, "o") + if err != nil { + t.Errorf("Organizations.ListCustomRepoRoles returned error: %v", err) + } + + want := &OrganizationCustomRepoRoles{TotalCount: Int(1), CustomRepoRoles: []*CustomRepoRoles{{ID: Int64(1), Name: String("Developer")}}} + if !cmp.Equal(apps, want) { + t.Errorf("Organizations.ListCustomRepoRoles returned %+v, want %+v", apps, want) + } + + const methodName = "ListCustomRepoRoles" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.ListCustomRepoRoles(ctx, "\no") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.ListCustomRepoRoles(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +}