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