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
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
113
114 type RepositoryAddCollaboratorOptions struct {
115
116
117
118
119
120
121
122
123
124 Permission string `json:"permission,omitempty"`
125 }
126
127
128
129
130
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
148
149
150
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 }
160
View as plain text