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
27
28
29
30
31 Permission string `url:"permission,omitempty"`
32
33 ListOptions
34 }
35
36
37
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
50
51
52 func (s *RepositoriesService) ListCollaborators(ctx context.Context, owner, repo string, opts *ListCollaboratorsOptions) ([]*User, *Response, error) {
53 u := fmt.Sprintf("repos/%v/%v/collaborators", owner, repo)
54 u, err := addOptions(u, opts)
55 if err != nil {
56 return nil, nil, err
57 }
58
59 req, err := s.client.NewRequest("GET", u, nil)
60 if err != nil {
61 return nil, nil, err
62 }
63
64 var users []*User
65 resp, err := s.client.Do(ctx, req, &users)
66 if err != nil {
67 return nil, resp, err
68 }
69
70 return users, resp, nil
71 }
72
73
74
75
76
77
78
79 func (s *RepositoriesService) IsCollaborator(ctx context.Context, owner, repo, user string) (bool, *Response, error) {
80 u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
81 req, err := s.client.NewRequest("GET", u, nil)
82 if err != nil {
83 return false, nil, err
84 }
85
86 resp, err := s.client.Do(ctx, req, nil)
87 isCollab, err := parseBoolResponse(err)
88 return isCollab, resp, err
89 }
90
91
92
93 type RepositoryPermissionLevel struct {
94
95 Permission *string `json:"permission,omitempty"`
96
97 User *User `json:"user,omitempty"`
98 }
99
100
101
102
103 func (s *RepositoriesService) GetPermissionLevel(ctx context.Context, owner, repo, user string) (*RepositoryPermissionLevel, *Response, error) {
104 u := fmt.Sprintf("repos/%v/%v/collaborators/%v/permission", owner, repo, user)
105 req, err := s.client.NewRequest("GET", u, nil)
106 if err != nil {
107 return nil, nil, err
108 }
109
110 rpl := new(RepositoryPermissionLevel)
111 resp, err := s.client.Do(ctx, req, rpl)
112 if err != nil {
113 return nil, resp, err
114 }
115
116 return rpl, resp, nil
117 }
118
119
120
121 type RepositoryAddCollaboratorOptions struct {
122
123
124
125
126
127
128
129
130
131 Permission string `json:"permission,omitempty"`
132 }
133
134
135
136
137
138 func (s *RepositoriesService) AddCollaborator(ctx context.Context, owner, repo, user string, opts *RepositoryAddCollaboratorOptions) (*CollaboratorInvitation, *Response, error) {
139 u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
140 req, err := s.client.NewRequest("PUT", u, opts)
141 if err != nil {
142 return nil, nil, err
143 }
144
145 acr := new(CollaboratorInvitation)
146 resp, err := s.client.Do(ctx, req, acr)
147 if err != nil {
148 return nil, resp, err
149 }
150
151 return acr, resp, nil
152 }
153
154
155
156
157
158 func (s *RepositoriesService) RemoveCollaborator(ctx context.Context, owner, repo, user string) (*Response, error) {
159 u := fmt.Sprintf("repos/%v/%v/collaborators/%v", owner, repo, user)
160 req, err := s.client.NewRequest("DELETE", u, nil)
161 if err != nil {
162 return nil, err
163 }
164
165 return s.client.Do(ctx, req, nil)
166 }
167
View as plain text