Repository: google/go-github
Pull request #2336 (2022-04-18 12:27:48)

Add support to list custom roles for organizations
Fixes: #2327 .

Thanks for this opportunity.

gmlewis: Please also make sure to run `go generate ./...` and push (not force-push) the results as described in our CONTRIBUTING.md document.
tamboliasir1: Hi @gmlewis I have made changes and pushed commit. Can you please review? Thank you so much.
codecov[bot]: # [Codecov](https://codecov.io/gh/google/go-github/pull/2336?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) Report
> Merging [#2336](https://codecov.io/gh/google/go-github/pull/2336?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (bdcc034) into [master](https://codecov.io/gh/google/go-github/commit/00e42332e405cdd73bc6cd45a4cc555a4cfd613e?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) (00e4233) will **increase** coverage by `0.00%`.
> The diff coverage is `100.00%`.

```diff
@@           Coverage Diff           @@
##           master    #2336   +/-   ##
=======================================
  Coverage   98.04%   98.05%           
=======================================
  Files         118      119    +1     
  Lines       10458    10471   +13     
=======================================
+ Hits        10254    10267   +13     
  Misses        140      140           
  Partials       64       64           
```


| [Impacted Files](https://codecov.io/gh/google/go-github/pull/2336?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google) | Coverage Δ | |
|---|---|---|
| [github/org\_custom\_roles.go](https://codecov.io/gh/google/go-github/pull/2336/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google#diff-Z2l0aHViL29yZ19jdXN0b21fcm9sZXMuZ28=) | `100.00% <100.00%> (ø)` | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/google/go-github/pull/2336?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/google/go-github/pull/2336?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Last update [00e4233...bdcc034](https://codecov.io/gh/google/go-github/pull/2336?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=google).

tamboliasir1: HI @reedloden @gmlewis Sorry for this silly typo I have resolved it please review. Please let me know if there is anything. Thank you.

Patch:
diff --git a/github/github-accessors.go b/github/github-accessors.go
index 724e67f0..d016a36b 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/org_custom_roles.go b/github/org_custom_roles.go
new file mode 100644
index 00000000..7c60fea0
--- /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
+}


Test patch:
diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go
index 5ac8d7b9..3e2108df 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_test.go b/github/org_custom_roles_test.go
new file mode 100644
index 00000000..cfcb0261
--- /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
+	})
+}