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