github.com/google/go-github/v68@v68.0.0/github/orgs_custom_repository_roles.go (about)

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