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