1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 )
12
13
14
15 type ListCollaboratorsOptions struct {
16
17
18
19
20
21
22
23
24 Affiliation string `url:"affiliation,omitempty"`
25
26 ListOptions
27 }
28
29
30
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
43
44
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
67
68
69
70
71
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
85
86 type RepositoryPermissionLevel struct {
87
88 Permission *string `json:"permission,omitempty"`
89
90 User *User `json:"user,omitempty"`
91 }
92
93
94
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
111
112 type RepositoryAddCollaboratorOptions struct {
113
114
115
116
117
118
119
120
121
122 Permission string `json:"permission,omitempty"`
123 }
124
125
126
127
128
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
144
145
146
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