Skip to content

chore: refactor org_custom_roles.go strucutre into multiple files #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
131 changes: 131 additions & 0 deletions github/orgs_custom_repository_roles.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2024 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/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"`
Description *string `json:"description,omitempty"`
BaseRole *string `json:"base_role,omitempty"`
Permissions []string `json:"permissions,omitempty"`
Org *Organization `json:"organization,omitempty"`
CreatedAt *Timestamp `json:"created_at,omitempty"`
UpdatedAt *Timestamp `json:"updated_at,omitempty"`
}

// CreateOrUpdateCustomRepoRoleOptions represents options required to create or update a custom repository role.
type CreateOrUpdateCustomRepoRoleOptions struct {
Name *string `json:"name,omitempty"`
Description *string `json:"description,omitempty"`
BaseRole *string `json:"base_role,omitempty"`
Permissions []string `json:"permissions"`
}

// 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/enterprise-cloud@latest/rest/orgs/custom-roles#list-custom-repository-roles-in-an-organization
//
//meta:operation GET /orgs/{org}/custom-repository-roles
func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrganizationCustomRepoRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/custom-repository-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
}

// CreateCustomRepoRole creates a custom repository role in this organization.
// In order to create custom repository roles in an organization, the authenticated user must be an organization owner.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#create-a-custom-repository-role
//
//meta:operation POST /orgs/{org}/custom-repository-roles
func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/custom-repository-roles", org)

req, err := s.client.NewRequest("POST", u, opts)
if err != nil {
return nil, nil, err
}

resultingRole := new(CustomRepoRoles)
resp, err := s.client.Do(ctx, req, resultingRole)
if err != nil {
return nil, resp, err
}

return resultingRole, resp, err
}

// UpdateCustomRepoRole updates a custom repository role in this organization.
// In order to update custom repository roles in an organization, the authenticated user must be an organization owner.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#update-a-custom-repository-role
//
//meta:operation PATCH /orgs/{org}/custom-repository-roles/{role_id}
func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org string, roleID int64, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)

req, err := s.client.NewRequest("PATCH", u, opts)
if err != nil {
return nil, nil, err
}

resultingRole := new(CustomRepoRoles)
resp, err := s.client.Do(ctx, req, resultingRole)
if err != nil {
return nil, resp, err
}

return resultingRole, resp, err
}

// DeleteCustomRepoRole deletes an existing custom repository role in this organization.
// In order to delete custom repository roles in an organization, the authenticated user must be an organization owner.
//
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#delete-a-custom-repository-role
//
//meta:operation DELETE /orgs/{org}/custom-repository-roles/{role_id}
func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org string, roleID int64) (*Response, error) {
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)

req, err := s.client.NewRequest("DELETE", u, nil)
if err != nil {
return nil, err
}

resultingRole := new(CustomRepoRoles)
resp, err := s.client.Do(ctx, req, resultingRole)
if err != nil {
return resp, err
}

return resp, nil
}
208 changes: 208 additions & 0 deletions github/orgs_custom_repository_roles_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
// Copyright 2024 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"
"time"

"github.com/google/go-cmp/cmp"
)

func TestOrganizationsService_ListCustomRepoRoles(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "GET")
fmt.Fprint(w, `{"total_count": 1, "custom_roles": [
{
"id": 1,
"name": "Developer",
"base_role": "write",
"permissions": ["delete_alerts_code_scanning"],
"organization": {
"login": "l",
"id": 1,
"node_id": "n",
"avatar_url": "a",
"html_url": "h",
"name": "n",
"company": "c",
"blog": "b",
"location": "l",
"email": "e"
},
"created_at": "2024-07-21T19:33:08Z",
"updated_at": "2024-07-21T19:33:08Z"
}
]
}`)
})

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"),
BaseRole: String("write"),
Permissions: []string{"delete_alerts_code_scanning"},
Org: &Organization{
Login: String("l"),
ID: Int64(1),
NodeID: String("n"),
AvatarURL: String("a"),
HTMLURL: String("h"),
Name: String("n"),
Company: String("c"),
Blog: String("b"),
Location: String("l"),
Email: String("e"),
},
CreatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)},
UpdatedAt: &Timestamp{time.Date(2024, time.July, 21, 19, 33, 8, 0, time.UTC)},
},
},
}
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
})
}

func TestOrganizationsService_CreateCustomRepoRole(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/custom-repository-roles", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "POST")
fmt.Fprint(w, `{"id":8030,"name":"Labeler","description":"A role for issue and PR labelers","base_role":"read","permissions":["add_label"]}`)
})

ctx := context.Background()

opts := &CreateOrUpdateCustomRepoRoleOptions{
Name: String("Labeler"),
Description: String("A role for issue and PR labelers"),
BaseRole: String("read"),
Permissions: []string{"add_label"},
}
apps, _, err := client.Organizations.CreateCustomRepoRole(ctx, "o", opts)
if err != nil {
t.Errorf("Organizations.CreateCustomRepoRole returned error: %v", err)
}

want := &CustomRepoRoles{ID: Int64(8030), Name: String("Labeler"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("A role for issue and PR labelers")}

if !cmp.Equal(apps, want) {
t.Errorf("Organizations.CreateCustomRepoRole returned %+v, want %+v", apps, want)
}

const methodName = "CreateCustomRepoRole"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Organizations.CreateCustomRepoRole(ctx, "\no", nil)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.CreateCustomRepoRole(ctx, "o", nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestOrganizationsService_UpdateCustomRepoRole(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "PATCH")
fmt.Fprint(w, `{"id":8030,"name":"Updated Name","description":"Updated Description","base_role":"read","permissions":["add_label"]}`)
})

ctx := context.Background()

opts := &CreateOrUpdateCustomRepoRoleOptions{
Name: String("Updated Name"),
Description: String("Updated Description"),
}
apps, _, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, opts)
if err != nil {
t.Errorf("Organizations.UpdateCustomRepoRole returned error: %v", err)
}

want := &CustomRepoRoles{ID: Int64(8030), Name: String("Updated Name"), BaseRole: String("read"), Permissions: []string{"add_label"}, Description: String("Updated Description")}

if !cmp.Equal(apps, want) {
t.Errorf("Organizations.UpdateCustomRepoRole returned %+v, want %+v", apps, want)
}

const methodName = "UpdateCustomRepoRole"
testBadOptions(t, methodName, func() (err error) {
_, _, err = client.Organizations.UpdateCustomRepoRole(ctx, "\no", 8030, nil)
return err
})

testNewRequestAndDoFailure(t, methodName, client, func() (*Response, error) {
got, resp, err := client.Organizations.UpdateCustomRepoRole(ctx, "o", 8030, nil)
if got != nil {
t.Errorf("testNewRequestAndDoFailure %v = %#v, want nil", methodName, got)
}
return resp, err
})
}

func TestOrganizationsService_DeleteCustomRepoRole(t *testing.T) {
client, mux, _, teardown := setup()
defer teardown()

mux.HandleFunc("/orgs/o/custom-repository-roles/8030", func(w http.ResponseWriter, r *http.Request) {
testMethod(t, r, "DELETE")
w.WriteHeader(http.StatusNoContent)
})

ctx := context.Background()

resp, err := client.Organizations.DeleteCustomRepoRole(ctx, "o", 8030)
if err != nil {
t.Errorf("Organizations.DeleteCustomRepoRole returned error: %v", err)
}

if !cmp.Equal(resp.StatusCode, 204) {
t.Errorf("Organizations.DeleteCustomRepoRole returned status code %+v, want %+v", resp.StatusCode, "204")
}

const methodName = "DeleteCustomRepoRole"
testBadOptions(t, methodName, func() (err error) {
_, err = client.Organizations.DeleteCustomRepoRole(ctx, "\no", 8030)
return err
})
}
Loading
Loading