...

Source file src/github.com/google/go-github/v33/github/repos_collaborators.go

Documentation: github.com/google/go-github/v33/github

     1  // Copyright 2013 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  // ListCollaboratorsOptions specifies the optional parameters to the
    14  // RepositoriesService.ListCollaborators method.
    15  type ListCollaboratorsOptions struct {
    16  	// Affiliation specifies how collaborators should be filtered by their affiliation.
    17  	// Possible values are:
    18  	//     outside - All outside collaborators of an organization-owned repository
    19  	//     direct - All collaborators with permissions to an organization-owned repository,
    20  	//              regardless of organization membership status
    21  	//     all - All collaborators the authenticated user can see
    22  	//
    23  	// Default value is "all".
    24  	Affiliation string `url:"affiliation,omitempty"`
    25  
    26  	ListOptions
    27  }
    28  
    29  // CollaboratorInvitation represents an invitation created when adding a collaborator.
    30  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/collaborators/#response-when-a-new-invitation-is-created
    31  type CollaboratorInvitation struct {
    32  	ID          *int64      `json:"id,omitempty"`
    33  	Repo        *Repository `json:"repository,omitempty"`
    34  	Invitee     *User       `json:"invitee,omitempty"`
    35  	Inviter     *User       `json:"inviter,omitempty"`
    36  	Permissions *string     `json:"permissions,omitempty"`
    37  	CreatedAt   *Timestamp  `json:"created_at,omitempty"`
    38  	URL         *string     `json:"url,omitempty"`
    39  	HTMLURL     *string     `json:"html_url,omitempty"`
    40  }
    41  
    42  // ListCollaborators lists the GitHub users that have access to the repository.
    43  //
    44  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#list-repository-collaborators
    45  func (s *RepositoriesService) ListCollaborators(ctx context.Context, owner, repo string, opts *ListCollaboratorsOptions) ([]*User, *Response, error) {
    46  	u := fmt.Sprintf("repos/%v/%v/collaborators", owner, repo)
    47  	u, err := addOptions(u, opts)
    48  	if err != nil {
    49  		return nil, nil, err
    50  	}
    51  
    52  	req, err := s.client.NewRequest("GET", u, nil)
    53  	if err != nil {
    54  		return nil, nil, err
    55  	}
    56  
    57  	var users []*User
    58  	resp, err := s.client.Do(ctx, req, &users)
    59  	if err != nil {
    60  		return nil, resp, err
    61  	}
    62  
    63  	return users, resp, nil
    64  }
    65  
    66  // IsCollaborator checks whether the specified GitHub user has collaborator
    67  // access to the given repo.
    68  // Note: This will return false if the user is not a collaborator OR the user
    69  // is not a GitHub user.
    70  //
    71  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#check-if-a-user-is-a-repository-collaborator
    72  func (s *RepositoriesService) IsCollaborator(ctx context.Context, owner, repo, user string) (bool, *Response, error) {
    73  	u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
    74  	req, err := s.client.NewRequest("GET", u, nil)
    75  	if err != nil {
    76  		return false, nil, err
    77  	}
    78  
    79  	resp, err := s.client.Do(ctx, req, nil)
    80  	isCollab, err := parseBoolResponse(err)
    81  	return isCollab, resp, err
    82  }
    83  
    84  // RepositoryPermissionLevel represents the permission level an organization
    85  // member has for a given repository.
    86  type RepositoryPermissionLevel struct {
    87  	// Possible values: "admin", "write", "read", "none"
    88  	Permission *string `json:"permission,omitempty"`
    89  
    90  	User *User `json:"user,omitempty"`
    91  }
    92  
    93  // GetPermissionLevel retrieves the specific permission level a collaborator has for a given repository.
    94  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#get-repository-permissions-for-a-user
    95  func (s *RepositoriesService) GetPermissionLevel(ctx context.Context, owner, repo, user string) (*RepositoryPermissionLevel, *Response, error) {
    96  	u := fmt.Sprintf("repos/%v/%v/collaborators/%v/permission", owner, repo, user)
    97  	req, err := s.client.NewRequest("GET", u, nil)
    98  	if err != nil {
    99  		return nil, nil, err
   100  	}
   101  
   102  	rpl := new(RepositoryPermissionLevel)
   103  	resp, err := s.client.Do(ctx, req, rpl)
   104  	if err != nil {
   105  		return nil, resp, err
   106  	}
   107  	return rpl, resp, nil
   108  }
   109  
   110  // RepositoryAddCollaboratorOptions specifies the optional parameters to the
   111  // RepositoriesService.AddCollaborator method.
   112  type RepositoryAddCollaboratorOptions struct {
   113  	// Permission specifies the permission to grant the user on this repository.
   114  	// Possible values are:
   115  	//     pull - team members can pull, but not push to or administer this repository
   116  	//     push - team members can pull and push, but not administer this repository
   117  	//     admin - team members can pull, push and administer this repository
   118  	//     maintain - team members can manage the repository without access to sensitive or destructive actions.
   119  	//     triage - team members can proactively manage issues and pull requests without write access.
   120  	//
   121  	// Default value is "push". This option is only valid for organization-owned repositories.
   122  	Permission string `json:"permission,omitempty"`
   123  }
   124  
   125  // AddCollaborator sends an invitation to the specified GitHub user
   126  // to become a collaborator to the given repo.
   127  //
   128  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#add-a-repository-collaborator
   129  func (s *RepositoriesService) AddCollaborator(ctx context.Context, owner, repo, user string, opts *RepositoryAddCollaboratorOptions) (*CollaboratorInvitation, *Response, error) {
   130  	u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
   131  	req, err := s.client.NewRequest("PUT", u, opts)
   132  	if err != nil {
   133  		return nil, nil, err
   134  	}
   135  	acr := new(CollaboratorInvitation)
   136  	resp, err := s.client.Do(ctx, req, acr)
   137  	if err != nil {
   138  		return nil, resp, err
   139  	}
   140  	return acr, resp, nil
   141  }
   142  
   143  // RemoveCollaborator removes the specified GitHub user as collaborator from the given repo.
   144  // Note: Does not return error if a valid user that is not a collaborator is removed.
   145  //
   146  // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/repos/#remove-a-repository-collaborator
   147  func (s *RepositoriesService) RemoveCollaborator(ctx context.Context, owner, repo, user string) (*Response, error) {
   148  	u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
   149  	req, err := s.client.NewRequest("DELETE", u, nil)
   150  	if err != nil {
   151  		return nil, err
   152  	}
   153  	return s.client.Do(ctx, req, nil)
   154  }
   155  

View as plain text