github.com/google/go-github/v49@v49.1.0/github/orgs_custom_roles.go (about) 1 // Copyright 2022 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 ) 12 13 // OrganizationCustomRepoRoles represents custom repository roles available in specified organization. 14 type OrganizationCustomRepoRoles struct { 15 TotalCount *int `json:"total_count,omitempty"` 16 CustomRepoRoles []*CustomRepoRoles `json:"custom_roles,omitempty"` 17 } 18 19 // CustomRepoRoles represents custom repository roles for an organization. 20 // 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 21 // for more information. 22 type CustomRepoRoles struct { 23 ID *int64 `json:"id,omitempty"` 24 Name *string `json:"name,omitempty"` 25 Description *string `json:"description,omitempty"` 26 BaseRole *string `json:"base_role,omitempty"` 27 Permissions []string `json:"permissions,omitempty"` 28 } 29 30 // ListCustomRepoRoles lists the custom repository roles available in this organization. 31 // In order to see custom repository roles in an organization, the authenticated user must be an organization owner. 32 // 33 // GitHub API docs: https://docs.github.com/en/rest/orgs/custom-roles#list-custom-repository-roles-in-an-organization 34 func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrganizationCustomRepoRoles, *Response, error) { 35 u := fmt.Sprintf("orgs/%v/custom_roles", org) 36 37 req, err := s.client.NewRequest("GET", u, nil) 38 if err != nil { 39 return nil, nil, err 40 } 41 42 customRepoRoles := new(OrganizationCustomRepoRoles) 43 resp, err := s.client.Do(ctx, req, customRepoRoles) 44 if err != nil { 45 return nil, resp, err 46 } 47 48 return customRepoRoles, resp, nil 49 } 50 51 // CreateOrUpdateCustomRoleOptions represents options required to create or update a custom repository role. 52 type CreateOrUpdateCustomRoleOptions struct { 53 Name *string `json:"name,omitempty"` 54 Description *string `json:"description,omitempty"` 55 BaseRole *string `json:"base_role,omitempty"` 56 Permissions []string `json:"permissions,omitempty"` 57 } 58 59 // CreateCustomRepoRole creates a custom repository role in this organization. 60 // In order to create custom repository roles in an organization, the authenticated user must be an organization owner. 61 // 62 // GitHub API docs: https://docs.github.com/en/rest/orgs/custom-roles#create-a-custom-role 63 func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRoleOptions) (*CustomRepoRoles, *Response, error) { 64 u := fmt.Sprintf("orgs/%v/custom_roles", org) 65 66 req, err := s.client.NewRequest("POST", u, opts) 67 if err != nil { 68 return nil, nil, err 69 } 70 71 resultingRole := new(CustomRepoRoles) 72 resp, err := s.client.Do(ctx, req, resultingRole) 73 if err != nil { 74 return nil, resp, err 75 } 76 77 return resultingRole, resp, err 78 } 79 80 // UpdateCustomRepoRole updates a custom repository role in this organization. 81 // In order to update custom repository roles in an organization, the authenticated user must be an organization owner. 82 // 83 // GitHub API docs: https://docs.github.com/en/rest/orgs/custom-roles#update-a-custom-role 84 func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org, roleID string, opts *CreateOrUpdateCustomRoleOptions) (*CustomRepoRoles, *Response, error) { 85 u := fmt.Sprintf("orgs/%v/custom_roles/%v", org, roleID) 86 87 req, err := s.client.NewRequest("PATCH", u, opts) 88 if err != nil { 89 return nil, nil, err 90 } 91 92 resultingRole := new(CustomRepoRoles) 93 resp, err := s.client.Do(ctx, req, resultingRole) 94 if err != nil { 95 return nil, resp, err 96 } 97 98 return resultingRole, resp, err 99 } 100 101 // DeleteCustomRepoRole deletes an existing custom repository role in this organization. 102 // In order to delete custom repository roles in an organization, the authenticated user must be an organization owner. 103 // 104 // GitHub API docs: https://docs.github.com/en/rest/orgs/custom-roles#delete-a-custom-role 105 func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org, roleID string) (*Response, error) { 106 u := fmt.Sprintf("orgs/%v/custom_roles/%v", org, roleID) 107 108 req, err := s.client.NewRequest("DELETE", u, nil) 109 if err != nil { 110 return nil, err 111 } 112 113 resultingRole := new(CustomRepoRoles) 114 resp, err := s.client.Do(ctx, req, resultingRole) 115 if err != nil { 116 return resp, err 117 } 118 119 return resp, nil 120 }