| 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/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/enterprise-cloud@latest/rest/orgs/custom-roles#list-custom-repository-roles-in-an-organization |
| 34 |
// |
| 35 |
//meta:operation GET /orgs/{org}/custom-repository-roles |
| 36 |
func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrganizationCustomRepoRoles, *Response, error) { |
| 37 |
u := fmt.Sprintf("orgs/%v/custom-repository-roles", org) |
| 38 |
|
| 39 |
req, err := s.client.NewRequest("GET", u, nil) |
| 40 |
if err != nil { |
| 41 |
return nil, nil, err |
| 42 |
} |
| 43 |
|
| 44 |
customRepoRoles := new(OrganizationCustomRepoRoles) |
| 45 |
resp, err := s.client.Do(ctx, req, customRepoRoles) |
| 46 |
if err != nil { |
| 47 |
return nil, resp, err |
| 48 |
} |
| 49 |
|
| 50 |
return customRepoRoles, resp, nil |
| 51 |
} |
| 52 |
|
| 53 |
// CreateOrUpdateCustomRoleOptions represents options required to create or update a custom repository role. |
| 54 |
type CreateOrUpdateCustomRoleOptions struct { |
| 55 |
Name *string `json:"name,omitempty"` |
| 56 |
Description *string `json:"description,omitempty"` |
| 57 |
BaseRole *string `json:"base_role,omitempty"` |
| 58 |
Permissions []string `json:"permissions,omitempty"` |
| 59 |
} |
| 60 |
|
| 61 |
// CreateCustomRepoRole creates a custom repository role in this organization. |
| 62 |
// In order to create custom repository roles in an organization, the authenticated user must be an organization owner. |
| 63 |
// |
| 64 |
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#create-a-custom-repository-role |
| 65 |
// |
| 66 |
//meta:operation POST /orgs/{org}/custom-repository-roles |
| 67 |
func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRoleOptions) (*CustomRepoRoles, *Response, error) { |
| 68 |
u := fmt.Sprintf("orgs/%v/custom-repository-roles", org) |
| 69 |
|
| 70 |
req, err := s.client.NewRequest("POST", u, opts) |
| 71 |
if err != nil { |
| 72 |
return nil, nil, err |
| 73 |
} |
| 74 |
|
| 75 |
resultingRole := new(CustomRepoRoles) |
| 76 |
resp, err := s.client.Do(ctx, req, resultingRole) |
| 77 |
if err != nil { |
| 78 |
return nil, resp, err |
| 79 |
} |
| 80 |
|
| 81 |
return resultingRole, resp, err |
| 82 |
} |
| 83 |
|
| 84 |
// UpdateCustomRepoRole updates a custom repository role in this organization. |
| 85 |
// In order to update custom repository roles in an organization, the authenticated user must be an organization owner. |
| 86 |
// |
| 87 |
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#update-a-custom-repository-role |
| 88 |
// |
| 89 |
//meta:operation PATCH /orgs/{org}/custom-repository-roles/{role_id} |
| 90 |
func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org, roleID string, opts *CreateOrUpdateCustomRoleOptions) (*CustomRepoRoles, *Response, error) { |
| 91 |
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID) |
| 92 |
|
| 93 |
req, err := s.client.NewRequest("PATCH", u, opts) |
| 94 |
if err != nil { |
| 95 |
return nil, nil, err |
| 96 |
} |
| 97 |
|
| 98 |
resultingRole := new(CustomRepoRoles) |
| 99 |
resp, err := s.client.Do(ctx, req, resultingRole) |
| 100 |
if err != nil { |
| 101 |
return nil, resp, err |
| 102 |
} |
| 103 |
|
| 104 |
return resultingRole, resp, err |
| 105 |
} |
| 106 |
|
| 107 |
// DeleteCustomRepoRole deletes an existing custom repository role in this organization. |
| 108 |
// In order to delete custom repository roles in an organization, the authenticated user must be an organization owner. |
| 109 |
// |
| 110 |
// GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#delete-a-custom-repository-role |
| 111 |
// |
| 112 |
//meta:operation DELETE /orgs/{org}/custom-repository-roles/{role_id} |
| 113 |
func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org, roleID string) (*Response, error) { |
| 114 |
u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID) |
| 115 |
|
| 116 |
req, err := s.client.NewRequest("DELETE", u, nil) |
| 117 |
if err != nil { |
| 118 |
return nil, err |
| 119 |
} |
| 120 |
|
| 121 |
resultingRole := new(CustomRepoRoles) |
| 122 |
resp, err := s.client.Do(ctx, req, resultingRole) |
| 123 |
if err != nil { |
| 124 |
return resp, err |
| 125 |
} |
| 126 |
|
| 127 |
return resp, nil |
| 128 |
} |