github.com/google/go-github/v65@v65.0.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  // OrganizationCustomRoles represents custom organization roles available in specified organization.
    14  type OrganizationCustomRoles struct {
    15  	TotalCount      *int              `json:"total_count,omitempty"`
    16  	CustomRepoRoles []*CustomOrgRoles `json:"roles,omitempty"`
    17  }
    18  
    19  // CustomOrgRoles represents custom organization role available in specified organization.
    20  type CustomOrgRoles struct {
    21  	ID          *int64        `json:"id,omitempty"`
    22  	Name        *string       `json:"name,omitempty"`
    23  	Description *string       `json:"description,omitempty"`
    24  	Permissions []string      `json:"permissions,omitempty"`
    25  	Org         *Organization `json:"organization,omitempty"`
    26  	CreatedAt   *Timestamp    `json:"created_at,omitempty"`
    27  	UpdatedAt   *Timestamp    `json:"updated_at,omitempty"`
    28  	Source      *string       `json:"source,omitempty"`
    29  	BaseRole    *string       `json:"base_role,omitempty"`
    30  }
    31  
    32  // OrganizationCustomRepoRoles represents custom repository roles available in specified organization.
    33  type OrganizationCustomRepoRoles struct {
    34  	TotalCount      *int               `json:"total_count,omitempty"`
    35  	CustomRepoRoles []*CustomRepoRoles `json:"custom_roles,omitempty"`
    36  }
    37  
    38  // CustomRepoRoles represents custom repository roles for an organization.
    39  // See https://docs.github.com/enterprise-cloud@latest/organizations/managing-peoples-access-to-your-organization-with-roles/managing-custom-repository-roles-for-an-organization
    40  // for more information.
    41  type CustomRepoRoles struct {
    42  	ID          *int64        `json:"id,omitempty"`
    43  	Name        *string       `json:"name,omitempty"`
    44  	Description *string       `json:"description,omitempty"`
    45  	BaseRole    *string       `json:"base_role,omitempty"`
    46  	Permissions []string      `json:"permissions,omitempty"`
    47  	Org         *Organization `json:"organization,omitempty"`
    48  	CreatedAt   *Timestamp    `json:"created_at,omitempty"`
    49  	UpdatedAt   *Timestamp    `json:"updated_at,omitempty"`
    50  }
    51  
    52  // CreateOrUpdateOrgRoleOptions represents options required to create or update a custom organization role.
    53  type CreateOrUpdateOrgRoleOptions struct {
    54  	Name        *string  `json:"name,omitempty"`
    55  	Description *string  `json:"description,omitempty"`
    56  	Permissions []string `json:"permissions"`
    57  }
    58  
    59  // CreateOrUpdateCustomRepoRoleOptions represents options required to create or update a custom repository role.
    60  type CreateOrUpdateCustomRepoRoleOptions struct {
    61  	Name        *string  `json:"name,omitempty"`
    62  	Description *string  `json:"description,omitempty"`
    63  	BaseRole    *string  `json:"base_role,omitempty"`
    64  	Permissions []string `json:"permissions"`
    65  }
    66  
    67  // ListRoles lists the custom roles available in this organization.
    68  // In order to see custom roles in an organization, the authenticated user must be an organization owner.
    69  //
    70  // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#get-all-organization-roles-for-an-organization
    71  //
    72  //meta:operation GET /orgs/{org}/organization-roles
    73  func (s *OrganizationsService) ListRoles(ctx context.Context, org string) (*OrganizationCustomRoles, *Response, error) {
    74  	u := fmt.Sprintf("orgs/%v/organization-roles", org)
    75  
    76  	req, err := s.client.NewRequest("GET", u, nil)
    77  	if err != nil {
    78  		return nil, nil, err
    79  	}
    80  
    81  	customRepoRoles := new(OrganizationCustomRoles)
    82  	resp, err := s.client.Do(ctx, req, customRepoRoles)
    83  	if err != nil {
    84  		return nil, resp, err
    85  	}
    86  
    87  	return customRepoRoles, resp, nil
    88  }
    89  
    90  // CreateCustomOrgRole creates a custom role in this organization.
    91  // In order to create custom roles in an organization, the authenticated user must be an organization owner.
    92  //
    93  // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#create-a-custom-organization-role
    94  //
    95  //meta:operation POST /orgs/{org}/organization-roles
    96  func (s *OrganizationsService) CreateCustomOrgRole(ctx context.Context, org string, opts *CreateOrUpdateOrgRoleOptions) (*CustomOrgRoles, *Response, error) {
    97  	u := fmt.Sprintf("orgs/%v/organization-roles", org)
    98  
    99  	req, err := s.client.NewRequest("POST", u, opts)
   100  	if err != nil {
   101  		return nil, nil, err
   102  	}
   103  
   104  	resultingRole := new(CustomOrgRoles)
   105  	resp, err := s.client.Do(ctx, req, resultingRole)
   106  	if err != nil {
   107  		return nil, resp, err
   108  	}
   109  
   110  	return resultingRole, resp, err
   111  }
   112  
   113  // UpdateCustomOrgRole updates a custom role in this organization.
   114  // In order to update custom roles in an organization, the authenticated user must be an organization owner.
   115  //
   116  // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#update-a-custom-organization-role
   117  //
   118  //meta:operation PATCH /orgs/{org}/organization-roles/{role_id}
   119  func (s *OrganizationsService) UpdateCustomOrgRole(ctx context.Context, org string, roleID int64, opts *CreateOrUpdateOrgRoleOptions) (*CustomOrgRoles, *Response, error) {
   120  	u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID)
   121  
   122  	req, err := s.client.NewRequest("PATCH", u, opts)
   123  	if err != nil {
   124  		return nil, nil, err
   125  	}
   126  
   127  	resultingRole := new(CustomOrgRoles)
   128  	resp, err := s.client.Do(ctx, req, resultingRole)
   129  	if err != nil {
   130  		return nil, resp, err
   131  	}
   132  
   133  	return resultingRole, resp, err
   134  }
   135  
   136  // DeleteCustomOrgRole deletes an existing custom role in this organization.
   137  // In order to delete custom roles in an organization, the authenticated user must be an organization owner.
   138  //
   139  // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#delete-a-custom-organization-role
   140  //
   141  //meta:operation DELETE /orgs/{org}/organization-roles/{role_id}
   142  func (s *OrganizationsService) DeleteCustomOrgRole(ctx context.Context, org string, roleID int64) (*Response, error) {
   143  	u := fmt.Sprintf("orgs/%v/organization-roles/%v", org, roleID)
   144  
   145  	req, err := s.client.NewRequest("DELETE", u, nil)
   146  	if err != nil {
   147  		return nil, err
   148  	}
   149  
   150  	resultingRole := new(CustomOrgRoles)
   151  	resp, err := s.client.Do(ctx, req, resultingRole)
   152  	if err != nil {
   153  		return resp, err
   154  	}
   155  
   156  	return resp, nil
   157  }
   158  
   159  // ListCustomRepoRoles lists the custom repository roles available in this organization.
   160  // In order to see custom repository roles in an organization, the authenticated user must be an organization owner.
   161  //
   162  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#list-custom-repository-roles-in-an-organization
   163  //
   164  //meta:operation GET /orgs/{org}/custom-repository-roles
   165  func (s *OrganizationsService) ListCustomRepoRoles(ctx context.Context, org string) (*OrganizationCustomRepoRoles, *Response, error) {
   166  	u := fmt.Sprintf("orgs/%v/custom-repository-roles", org)
   167  
   168  	req, err := s.client.NewRequest("GET", u, nil)
   169  	if err != nil {
   170  		return nil, nil, err
   171  	}
   172  
   173  	customRepoRoles := new(OrganizationCustomRepoRoles)
   174  	resp, err := s.client.Do(ctx, req, customRepoRoles)
   175  	if err != nil {
   176  		return nil, resp, err
   177  	}
   178  
   179  	return customRepoRoles, resp, nil
   180  }
   181  
   182  // CreateCustomRepoRole creates a custom repository role in this organization.
   183  // In order to create custom repository roles in an organization, the authenticated user must be an organization owner.
   184  //
   185  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#create-a-custom-repository-role
   186  //
   187  //meta:operation POST /orgs/{org}/custom-repository-roles
   188  func (s *OrganizationsService) CreateCustomRepoRole(ctx context.Context, org string, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
   189  	u := fmt.Sprintf("orgs/%v/custom-repository-roles", org)
   190  
   191  	req, err := s.client.NewRequest("POST", u, opts)
   192  	if err != nil {
   193  		return nil, nil, err
   194  	}
   195  
   196  	resultingRole := new(CustomRepoRoles)
   197  	resp, err := s.client.Do(ctx, req, resultingRole)
   198  	if err != nil {
   199  		return nil, resp, err
   200  	}
   201  
   202  	return resultingRole, resp, err
   203  }
   204  
   205  // UpdateCustomRepoRole updates a custom repository role in this organization.
   206  // In order to update custom repository roles in an organization, the authenticated user must be an organization owner.
   207  //
   208  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#update-a-custom-repository-role
   209  //
   210  //meta:operation PATCH /orgs/{org}/custom-repository-roles/{role_id}
   211  func (s *OrganizationsService) UpdateCustomRepoRole(ctx context.Context, org string, roleID int64, opts *CreateOrUpdateCustomRepoRoleOptions) (*CustomRepoRoles, *Response, error) {
   212  	u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)
   213  
   214  	req, err := s.client.NewRequest("PATCH", u, opts)
   215  	if err != nil {
   216  		return nil, nil, err
   217  	}
   218  
   219  	resultingRole := new(CustomRepoRoles)
   220  	resp, err := s.client.Do(ctx, req, resultingRole)
   221  	if err != nil {
   222  		return nil, resp, err
   223  	}
   224  
   225  	return resultingRole, resp, err
   226  }
   227  
   228  // DeleteCustomRepoRole deletes an existing custom repository role in this organization.
   229  // In order to delete custom repository roles in an organization, the authenticated user must be an organization owner.
   230  //
   231  // GitHub API docs: https://docs.github.com/enterprise-cloud@latest/rest/orgs/custom-roles#delete-a-custom-repository-role
   232  //
   233  //meta:operation DELETE /orgs/{org}/custom-repository-roles/{role_id}
   234  func (s *OrganizationsService) DeleteCustomRepoRole(ctx context.Context, org string, roleID int64) (*Response, error) {
   235  	u := fmt.Sprintf("orgs/%v/custom-repository-roles/%v", org, roleID)
   236  
   237  	req, err := s.client.NewRequest("DELETE", u, nil)
   238  	if err != nil {
   239  		return nil, err
   240  	}
   241  
   242  	resultingRole := new(CustomRepoRoles)
   243  	resp, err := s.client.Do(ctx, req, resultingRole)
   244  	if err != nil {
   245  		return resp, err
   246  	}
   247  
   248  	return resp, nil
   249  }
   250  
   251  // ListTeamsAssignedToOrgRole returns all teams assigned to a specific organization role.
   252  // In order to list teams assigned to an organization role, the authenticated user must be an organization owner.
   253  //
   254  // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#list-teams-that-are-assigned-to-an-organization-role
   255  //
   256  //meta:operation GET /orgs/{org}/organization-roles/{role_id}/teams
   257  func (s *OrganizationsService) ListTeamsAssignedToOrgRole(ctx context.Context, org string, roleID int64, opts *ListOptions) ([]*Team, *Response, error) {
   258  	u := fmt.Sprintf("orgs/%v/organization-roles/%v/teams", org, roleID)
   259  	u, err := addOptions(u, opts)
   260  	if err != nil {
   261  		return nil, nil, err
   262  	}
   263  
   264  	req, err := s.client.NewRequest("GET", u, nil)
   265  	if err != nil {
   266  		return nil, nil, err
   267  	}
   268  
   269  	var teams []*Team
   270  	resp, err := s.client.Do(ctx, req, &teams)
   271  	if err != nil {
   272  		return nil, resp, err
   273  	}
   274  
   275  	return teams, resp, nil
   276  }
   277  
   278  // ListUsersAssignedToOrgRole returns all users assigned to a specific organization role.
   279  // In order to list users assigned to an organization role, the authenticated user must be an organization owner.
   280  //
   281  // GitHub API docs: https://docs.github.com/rest/orgs/organization-roles#list-users-that-are-assigned-to-an-organization-role
   282  //
   283  //meta:operation GET /orgs/{org}/organization-roles/{role_id}/users
   284  func (s *OrganizationsService) ListUsersAssignedToOrgRole(ctx context.Context, org string, roleID int64, opts *ListOptions) ([]*User, *Response, error) {
   285  	u := fmt.Sprintf("orgs/%v/organization-roles/%v/users", org, roleID)
   286  	u, err := addOptions(u, opts)
   287  	if err != nil {
   288  		return nil, nil, err
   289  	}
   290  
   291  	req, err := s.client.NewRequest("GET", u, nil)
   292  	if err != nil {
   293  		return nil, nil, err
   294  	}
   295  
   296  	var users []*User
   297  	resp, err := s.client.Do(ctx, req, &users)
   298  	if err != nil {
   299  		return nil, resp, err
   300  	}
   301  
   302  	return users, resp, nil
   303  }