From e63bc700c1a20939493ed72b1bf3b849eaa97c57 Mon Sep 17 00:00:00 2001 From: Asir Tamboli Date: Mon, 18 Apr 2022 17:55:40 +0530 Subject: [PATCH 1/4] Added support to list custom roles for organizations --- github/org_custom_roles.go | 38 ++++++++++++++++++++++++++++ github/org_custom_roles_test.go | 45 +++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+) create mode 100644 github/org_custom_roles.go create mode 100644 github/org_custom_roles_test.go diff --git a/github/org_custom_roles.go b/github/org_custom_roles.go new file mode 100644 index 0000000000..5ac6ee6e10 --- /dev/null +++ b/github/org_custom_roles.go @@ -0,0 +1,38 @@ +package github + +import ( + "context" + "fmt" +) + +// OrginizationCustomRoles represents custom repository roles available in specified organization +type OrginizationCustomRoles struct { + TotalCount *int `json:"total_count,omitempty"` + CustomRoles []*CustomRoles `json:"custom_roles,omitempty"` +} + +// CustomRoles represents information of custom roles +type CustomRoles struct { + ID *int64 `json:"id,omitempty"` + Name *string `json:"name,omitempty"` +} + +// List Custome Roles in Org +// +// GitHub API docs: https://docs.github.com/en/rest/reference/orgs#custom-repository-roles +func (s *OrganizationsService) ListCustomRoles(ctx context.Context, organization_id string) (*OrginizationCustomRoles, *Response, error) { + u := fmt.Sprintf("organizations/%v/custom_roles", organization_id) + + req, err := s.client.NewRequest("GET", u, nil) + if err != nil { + return nil, nil, err + } + + custom_roles := new(OrginizationCustomRoles) + resp, err := s.client.Do(ctx, req, custom_roles) + if err != nil { + return nil, resp, err + } + + return custom_roles, resp, nil +} diff --git a/github/org_custom_roles_test.go b/github/org_custom_roles_test.go new file mode 100644 index 0000000000..4a093ecb8d --- /dev/null +++ b/github/org_custom_roles_test.go @@ -0,0 +1,45 @@ +package github + +import ( + "context" + "fmt" + "net/http" + "testing" + + "github.com/google/go-cmp/cmp" +) + +func TestOrganizationsService_ListCustomRoles(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.ListCustomRoles(ctx, "o") + if err != nil { + t.Errorf("Organizations.ListCustomRoles returned error: %v", err) + } + + want := &OrginizationCustomRoles{TotalCount: Int(1), CustomRoles: []*CustomRoles{{ID: Int64(1), Name: String("Developer")}}} + if !cmp.Equal(apps, want) { + t.Errorf("Organizations.ListCustomRoles returned %+v, want %+v", apps, want) + } + + const methodName = "ListCustomRoles" + testBadOptions(t, methodName, func() (err error) { + _, _, err = client.Organizations.ListCustomRoles(ctx, "\no") + return err + }) + + testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { + got, resp, err := client.Organizations.ListCustomRoles(ctx, "o") + if got != nil { + t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) + } + return resp, err + }) +} From bdcc034c0c3e2beedb9344caf0ef3a634f63d450 Mon Sep 17 00:00:00 2001 From: Asir Tamboli Date: Sun, 24 Apr 2022 15:50:20 +0530 Subject: [PATCH 2/4] Updated naming and comments for Organization Custom Repo Roles --- github/github-accessors.go | 24 ++++++++++++++++++++++++ github/github-accessors_test.go | 30 ++++++++++++++++++++++++++++++ github/org_custom_roles.go | 32 ++++++++++++++++++++------------ github/org_custom_roles_test.go | 21 +++++++++++++-------- 4 files changed, 87 insertions(+), 20 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index 724e67f0d3..c15b58604d 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 { @@ -9310,6 +9326,14 @@ func (o *OAuthAPP) GetURL() string { return *o.URL } +// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. +func (o *OrgainizationCustomRepoRoles) GetTotalCount() int { + if o == nil || o.TotalCount == nil { + return 0 + } + return *o.TotalCount +} + // GetAvatarURL returns the AvatarURL field if it's non-nil, zero value otherwise. func (o *Organization) GetAvatarURL() string { if o == nil || o.AvatarURL == nil { diff --git a/github/github-accessors_test.go b/github/github-accessors_test.go index 5ac8d7b9a6..bfeea82af3 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() @@ -10909,6 +10929,16 @@ func TestOAuthAPP_GetURL(tt *testing.T) { o.GetURL() } +func TestOrgainizationCustomRepoRoles_GetTotalCount(tt *testing.T) { + var zeroValue int + o := &OrgainizationCustomRepoRoles{TotalCount: &zeroValue} + o.GetTotalCount() + o = &OrgainizationCustomRepoRoles{} + o.GetTotalCount() + o = nil + o.GetTotalCount() +} + func TestOrganization_GetAvatarURL(tt *testing.T) { var zeroValue string o := &Organization{AvatarURL: &zeroValue} diff --git a/github/org_custom_roles.go b/github/org_custom_roles.go index 5ac6ee6e10..ba8d3eed19 100644 --- a/github/org_custom_roles.go +++ b/github/org_custom_roles.go @@ -1,3 +1,8 @@ +// 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 ( @@ -5,34 +10,37 @@ import ( "fmt" ) -// OrginizationCustomRoles represents custom repository roles available in specified organization -type OrginizationCustomRoles struct { - TotalCount *int `json:"total_count,omitempty"` - CustomRoles []*CustomRoles `json:"custom_roles,omitempty"` +// OrgainizationCustomRepoRoles represents custom repository roles available in specified organization. +type OrgainizationCustomRepoRoles struct { + TotalCount *int `json:"total_count,omitempty"` + CustomRepoRoles []*CustomRepoRoles `json:"custom_roles,omitempty"` } -// CustomRoles represents information of custom roles -type CustomRoles struct { +// 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"` } -// List Custome Roles in Org +// 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/reference/orgs#custom-repository-roles -func (s *OrganizationsService) ListCustomRoles(ctx context.Context, organization_id string) (*OrginizationCustomRoles, *Response, error) { - u := fmt.Sprintf("organizations/%v/custom_roles", organization_id) +func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrgainizationCustomRepoRoles, *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 } - custom_roles := new(OrginizationCustomRoles) - resp, err := s.client.Do(ctx, req, custom_roles) + customRepoRoles := new(OrgainizationCustomRepoRoles) + resp, err := s.client.Do(ctx, req, customRepoRoles) if err != nil { return nil, resp, err } - return custom_roles, resp, nil + return customRepoRoles, resp, nil } diff --git a/github/org_custom_roles_test.go b/github/org_custom_roles_test.go index 4a093ecb8d..4ae5274356 100644 --- a/github/org_custom_roles_test.go +++ b/github/org_custom_roles_test.go @@ -1,3 +1,8 @@ +// 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 ( @@ -9,7 +14,7 @@ import ( "github.com/google/go-cmp/cmp" ) -func TestOrganizationsService_ListCustomRoles(t *testing.T) { +func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) { client, mux, _, teardown := setup() defer teardown() @@ -19,24 +24,24 @@ func TestOrganizationsService_ListCustomRoles(t *testing.T) { }) ctx := context.Background() - apps, _, err := client.Organizations.ListCustomRoles(ctx, "o") + apps, _, err := client.Organizations.ListCustomRepoRoles(ctx, "o") if err != nil { - t.Errorf("Organizations.ListCustomRoles returned error: %v", err) + t.Errorf("Organizations.ListCustomRepoRoles returned error: %v", err) } - want := &OrginizationCustomRoles{TotalCount: Int(1), CustomRoles: []*CustomRoles{{ID: Int64(1), Name: String("Developer")}}} + want := &OrgainizationCustomRepoRoles{TotalCount: Int(1), CustomRepoRoles: []*CustomRepoRoles{{ID: Int64(1), Name: String("Developer")}}} if !cmp.Equal(apps, want) { - t.Errorf("Organizations.ListCustomRoles returned %+v, want %+v", apps, want) + t.Errorf("Organizations.ListCustomRepoRoles returned %+v, want %+v", apps, want) } - const methodName = "ListCustomRoles" + const methodName = "ListCustomRepoRoles" testBadOptions(t, methodName, func() (err error) { - _, _, err = client.Organizations.ListCustomRoles(ctx, "\no") + _, _, err = client.Organizations.ListCustomRepoRoles(ctx, "\no") return err }) testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) { - got, resp, err := client.Organizations.ListCustomRoles(ctx, "o") + got, resp, err := client.Organizations.ListCustomRepoRoles(ctx, "o") if got != nil { t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got) } From 75ab6ab8cac1f8de9751900d3f48284e317e948d Mon Sep 17 00:00:00 2001 From: Glenn Lewis <6598971+gmlewis@users.noreply.github.com> Date: Sun, 24 Apr 2022 06:57:29 -0400 Subject: [PATCH 3/4] Update github/org_custom_roles.go --- github/org_custom_roles.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/github/org_custom_roles.go b/github/org_custom_roles.go index ba8d3eed19..8e069c6d1d 100644 --- a/github/org_custom_roles.go +++ b/github/org_custom_roles.go @@ -27,7 +27,7 @@ type CustomRepoRoles struct { // 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/reference/orgs#custom-repository-roles +// GitHub API docs: https://docs.github.com/en/rest/orgs/custom-roles func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrgainizationCustomRepoRoles, *Response, error) { u := fmt.Sprintf("organizations/%v/custom_roles", org) From b01926cb2078e9b9a36275c9380c76b06c0cb6e4 Mon Sep 17 00:00:00 2001 From: Asir Tamboli Date: Sun, 24 Apr 2022 21:22:02 +0530 Subject: [PATCH 4/4] Updated typos in github/org_custom_roles.go --- github/github-accessors.go | 16 ++++++++-------- github/github-accessors_test.go | 20 ++++++++++---------- github/org_custom_roles.go | 8 ++++---- github/org_custom_roles_test.go | 2 +- 4 files changed, 23 insertions(+), 23 deletions(-) diff --git a/github/github-accessors.go b/github/github-accessors.go index c15b58604d..d016a36bcb 100644 --- a/github/github-accessors.go +++ b/github/github-accessors.go @@ -9326,14 +9326,6 @@ func (o *OAuthAPP) GetURL() string { return *o.URL } -// GetTotalCount returns the TotalCount field if it's non-nil, zero value otherwise. -func (o *OrgainizationCustomRepoRoles) GetTotalCount() int { - if o == nil || o.TotalCount == nil { - return 0 - } - return *o.TotalCount -} - // GetAvatarURL returns the AvatarURL field if it's non-nil, zero value otherwise. func (o *Organization) GetAvatarURL() string { if o == nil || o.AvatarURL == nil { @@ -9710,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 bfeea82af3..3e2108dfc3 100644 --- a/github/github-accessors_test.go +++ b/github/github-accessors_test.go @@ -10929,16 +10929,6 @@ func TestOAuthAPP_GetURL(tt *testing.T) { o.GetURL() } -func TestOrgainizationCustomRepoRoles_GetTotalCount(tt *testing.T) { - var zeroValue int - o := &OrgainizationCustomRepoRoles{TotalCount: &zeroValue} - o.GetTotalCount() - o = &OrgainizationCustomRepoRoles{} - o.GetTotalCount() - o = nil - o.GetTotalCount() -} - func TestOrganization_GetAvatarURL(tt *testing.T) { var zeroValue string o := &Organization{AvatarURL: &zeroValue} @@ -11406,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 index 8e069c6d1d..7c60fea07b 100644 --- a/github/org_custom_roles.go +++ b/github/org_custom_roles.go @@ -10,8 +10,8 @@ import ( "fmt" ) -// OrgainizationCustomRepoRoles represents custom repository roles available in specified organization. -type OrgainizationCustomRepoRoles struct { +// OrganizationCustomRepoRoles represents custom repository roles available in specified organization. +type OrganizationCustomRepoRoles struct { TotalCount *int `json:"total_count,omitempty"` CustomRepoRoles []*CustomRepoRoles `json:"custom_roles,omitempty"` } @@ -28,7 +28,7 @@ type CustomRepoRoles struct { // 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) (*OrgainizationCustomRepoRoles, *Response, error) { +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) @@ -36,7 +36,7 @@ func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org stri return nil, nil, err } - customRepoRoles := new(OrgainizationCustomRepoRoles) + customRepoRoles := new(OrganizationCustomRepoRoles) resp, err := s.client.Do(ctx, req, customRepoRoles) if err != nil { return nil, resp, err diff --git a/github/org_custom_roles_test.go b/github/org_custom_roles_test.go index 4ae5274356..cfcb02611a 100644 --- a/github/org_custom_roles_test.go +++ b/github/org_custom_roles_test.go @@ -29,7 +29,7 @@ func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) { t.Errorf("Organizations.ListCustomRepoRoles returned error: %v", err) } - want := &OrgainizationCustomRepoRoles{TotalCount: Int(1), CustomRepoRoles: []*CustomRepoRoles{{ID: Int64(1), Name: String("Developer")}}} + 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) }