github.com/google/go-github/v49@v49.1.0/github/repos_collaborators.go (about) 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/rest/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/rest/collaborators/collaborators#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/rest/collaborators/collaborators#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 // 95 // GitHub API docs: https://docs.github.com/en/rest/collaborators/collaborators#get-repository-permissions-for-a-user 96 func (s *RepositoriesService) GetPermissionLevel(ctx context.Context, owner, repo, user string) (*RepositoryPermissionLevel, *Response, error) { 97 u := fmt.Sprintf("repos/%v/%v/collaborators/%v/permission", owner, repo, user) 98 req, err := s.client.NewRequest("GET", u, nil) 99 if err != nil { 100 return nil, nil, err 101 } 102 103 rpl := new(RepositoryPermissionLevel) 104 resp, err := s.client.Do(ctx, req, rpl) 105 if err != nil { 106 return nil, resp, err 107 } 108 109 return rpl, resp, nil 110 } 111 112 // RepositoryAddCollaboratorOptions specifies the optional parameters to the 113 // RepositoriesService.AddCollaborator method. 114 type RepositoryAddCollaboratorOptions struct { 115 // Permission specifies the permission to grant the user on this repository. 116 // Possible values are: 117 // pull - team members can pull, but not push to or administer this repository 118 // push - team members can pull and push, but not administer this repository 119 // admin - team members can pull, push and administer this repository 120 // maintain - team members can manage the repository without access to sensitive or destructive actions. 121 // triage - team members can proactively manage issues and pull requests without write access. 122 // 123 // Default value is "push". This option is only valid for organization-owned repositories. 124 Permission string `json:"permission,omitempty"` 125 } 126 127 // AddCollaborator sends an invitation to the specified GitHub user 128 // to become a collaborator to the given repo. 129 // 130 // GitHub API docs: https://docs.github.com/en/rest/collaborators/collaborators#add-a-repository-collaborator 131 func (s *RepositoriesService) AddCollaborator(ctx context.Context, owner, repo, user string, opts *RepositoryAddCollaboratorOptions) (*CollaboratorInvitation, *Response, error) { 132 u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user) 133 req, err := s.client.NewRequest("PUT", u, opts) 134 if err != nil { 135 return nil, nil, err 136 } 137 138 acr := new(CollaboratorInvitation) 139 resp, err := s.client.Do(ctx, req, acr) 140 if err != nil { 141 return nil, resp, err 142 } 143 144 return acr, resp, nil 145 } 146 147 // RemoveCollaborator removes the specified GitHub user as collaborator from the given repo. 148 // Note: Does not return error if a valid user that is not a collaborator is removed. 149 // 150 // GitHub API docs: https://docs.github.com/en/rest/collaborators/collaborators#remove-a-repository-collaborator 151 func (s *RepositoriesService) RemoveCollaborator(ctx context.Context, owner, repo, user string) (*Response, error) { 152 u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user) 153 req, err := s.client.NewRequest("DELETE", u, nil) 154 if err != nil { 155 return nil, err 156 } 157 158 return s.client.Do(ctx, req, nil) 159 }