Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
38 changes: 38 additions & 0 deletions github/org_custom_roles.go
Original file line number Diff line number Diff line change
@@ -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
}
45 changes: 45 additions & 0 deletions github/org_custom_roles_test.go
Original file line number Diff line number Diff line change
@@ -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
})
}