1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "time"
12 )
13
14
15
16
17
18 type AppsService service
19
20
21 type App struct {
22 ID *int64 `json:"id,omitempty"`
23 Slug *string `json:"slug,omitempty"`
24 NodeID *string `json:"node_id,omitempty"`
25 Owner *User `json:"owner,omitempty"`
26 Name *string `json:"name,omitempty"`
27 Description *string `json:"description,omitempty"`
28 ExternalURL *string `json:"external_url,omitempty"`
29 HTMLURL *string `json:"html_url,omitempty"`
30 CreatedAt *Timestamp `json:"created_at,omitempty"`
31 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
32 Permissions *InstallationPermissions `json:"permissions,omitempty"`
33 Events []string `json:"events,omitempty"`
34 }
35
36
37 type InstallationToken struct {
38 Token *string `json:"token,omitempty"`
39 ExpiresAt *time.Time `json:"expires_at,omitempty"`
40 Permissions *InstallationPermissions `json:"permissions,omitempty"`
41 Repositories []*Repository `json:"repositories,omitempty"`
42 }
43
44
45 type InstallationTokenOptions struct {
46
47
48 RepositoryIDs []int64 `json:"repository_ids,omitempty"`
49
50
51
52 Permissions *InstallationPermissions `json:"permissions,omitempty"`
53 }
54
55
56
57
58
59
60 type InstallationPermissions struct {
61 Administration *string `json:"administration,omitempty"`
62 Blocking *string `json:"blocking,omitempty"`
63 Checks *string `json:"checks,omitempty"`
64 Contents *string `json:"contents,omitempty"`
65 ContentReferences *string `json:"content_references,omitempty"`
66 Deployments *string `json:"deployments,omitempty"`
67 Emails *string `json:"emails,omitempty"`
68 Followers *string `json:"followers,omitempty"`
69 Issues *string `json:"issues,omitempty"`
70 Metadata *string `json:"metadata,omitempty"`
71 Members *string `json:"members,omitempty"`
72 OrganizationAdministration *string `json:"organization_administration,omitempty"`
73 OrganizationHooks *string `json:"organization_hooks,omitempty"`
74 OrganizationPlan *string `json:"organization_plan,omitempty"`
75 OrganizationPreReceiveHooks *string `json:"organization_pre_receive_hooks,omitempty"`
76 OrganizationProjects *string `json:"organization_projects,omitempty"`
77 OrganizationUserBlocking *string `json:"organization_user_blocking,omitempty"`
78 Packages *string `json:"packages,omitempty"`
79 Pages *string `json:"pages,omitempty"`
80 PullRequests *string `json:"pull_requests,omitempty"`
81 RepositoryHooks *string `json:"repository_hooks,omitempty"`
82 RepositoryProjects *string `json:"repository_projects,omitempty"`
83 RepositoryPreReceiveHooks *string `json:"repository_pre_receive_hooks,omitempty"`
84 SingleFile *string `json:"single_file,omitempty"`
85 Statuses *string `json:"statuses,omitempty"`
86 TeamDiscussions *string `json:"team_discussions,omitempty"`
87 VulnerabilityAlerts *string `json:"vulnerability_alerts,omitempty"`
88 }
89
90
91 type Installation struct {
92 ID *int64 `json:"id,omitempty"`
93 NodeID *string `json:"node_id,omitempty"`
94 AppID *int64 `json:"app_id,omitempty"`
95 TargetID *int64 `json:"target_id,omitempty"`
96 Account *User `json:"account,omitempty"`
97 AccessTokensURL *string `json:"access_tokens_url,omitempty"`
98 RepositoriesURL *string `json:"repositories_url,omitempty"`
99 HTMLURL *string `json:"html_url,omitempty"`
100 TargetType *string `json:"target_type,omitempty"`
101 SingleFileName *string `json:"single_file_name,omitempty"`
102 RepositorySelection *string `json:"repository_selection,omitempty"`
103 Events []string `json:"events,omitempty"`
104 Permissions *InstallationPermissions `json:"permissions,omitempty"`
105 CreatedAt *Timestamp `json:"created_at,omitempty"`
106 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
107 }
108
109
110 type Attachment struct {
111 ID *int64 `json:"id,omitempty"`
112 Title *string `json:"title,omitempty"`
113 Body *string `json:"body,omitempty"`
114 }
115
116
117 type ContentReference struct {
118 ID *int64 `json:"id,omitempty"`
119 NodeID *string `json:"node_id,omitempty"`
120 Reference *string `json:"reference,omitempty"`
121 }
122
123 func (i Installation) String() string {
124 return Stringify(i)
125 }
126
127
128
129
130
131
132
133
134
135
136 func (s *AppsService) Get(ctx context.Context, appSlug string) (*App, *Response, error) {
137 var u string
138 if appSlug != "" {
139 u = fmt.Sprintf("apps/%v", appSlug)
140 } else {
141 u = "app"
142 }
143
144 req, err := s.client.NewRequest("GET", u, nil)
145 if err != nil {
146 return nil, nil, err
147 }
148
149 app := new(App)
150 resp, err := s.client.Do(ctx, req, app)
151 if err != nil {
152 return nil, resp, err
153 }
154
155 return app, resp, nil
156 }
157
158
159
160
161 func (s *AppsService) ListInstallations(ctx context.Context, opts *ListOptions) ([]*Installation, *Response, error) {
162 u, err := addOptions("app/installations", opts)
163 if err != nil {
164 return nil, nil, err
165 }
166
167 req, err := s.client.NewRequest("GET", u, nil)
168 if err != nil {
169 return nil, nil, err
170 }
171
172 var i []*Installation
173 resp, err := s.client.Do(ctx, req, &i)
174 if err != nil {
175 return nil, resp, err
176 }
177
178 return i, resp, nil
179 }
180
181
182
183
184 func (s *AppsService) GetInstallation(ctx context.Context, id int64) (*Installation, *Response, error) {
185 return s.getInstallation(ctx, fmt.Sprintf("app/installations/%v", id))
186 }
187
188
189
190
191 func (s *AppsService) ListUserInstallations(ctx context.Context, opts *ListOptions) ([]*Installation, *Response, error) {
192 u, err := addOptions("user/installations", opts)
193 if err != nil {
194 return nil, nil, err
195 }
196
197 req, err := s.client.NewRequest("GET", u, nil)
198 if err != nil {
199 return nil, nil, err
200 }
201
202 var i struct {
203 Installations []*Installation `json:"installations"`
204 }
205 resp, err := s.client.Do(ctx, req, &i)
206 if err != nil {
207 return nil, resp, err
208 }
209
210 return i.Installations, resp, nil
211 }
212
213
214
215
216 func (s *AppsService) SuspendInstallation(ctx context.Context, id int64) (*Response, error) {
217 u := fmt.Sprintf("app/installations/%v/suspended", id)
218
219 req, err := s.client.NewRequest("PUT", u, nil)
220 if err != nil {
221 return nil, err
222 }
223
224 return s.client.Do(ctx, req, nil)
225 }
226
227
228
229
230 func (s *AppsService) UnsuspendInstallation(ctx context.Context, id int64) (*Response, error) {
231 u := fmt.Sprintf("app/installations/%v/suspended", id)
232
233 req, err := s.client.NewRequest("DELETE", u, nil)
234 if err != nil {
235 return nil, err
236 }
237
238 return s.client.Do(ctx, req, nil)
239 }
240
241
242
243
244 func (s *AppsService) DeleteInstallation(ctx context.Context, id int64) (*Response, error) {
245 u := fmt.Sprintf("app/installations/%v", id)
246
247 req, err := s.client.NewRequest("DELETE", u, nil)
248 if err != nil {
249 return nil, err
250 }
251
252 return s.client.Do(ctx, req, nil)
253 }
254
255
256
257
258 func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64, opts *InstallationTokenOptions) (*InstallationToken, *Response, error) {
259 u := fmt.Sprintf("app/installations/%v/access_tokens", id)
260
261 req, err := s.client.NewRequest("POST", u, opts)
262 if err != nil {
263 return nil, nil, err
264 }
265
266 t := new(InstallationToken)
267 resp, err := s.client.Do(ctx, req, t)
268 if err != nil {
269 return nil, resp, err
270 }
271
272 return t, resp, nil
273 }
274
275
276
277
278 func (s *AppsService) CreateAttachment(ctx context.Context, contentReferenceID int64, title, body string) (*Attachment, *Response, error) {
279 u := fmt.Sprintf("content_references/%v/attachments", contentReferenceID)
280 payload := &Attachment{Title: String(title), Body: String(body)}
281 req, err := s.client.NewRequest("POST", u, payload)
282 if err != nil {
283 return nil, nil, err
284 }
285
286
287 req.Header.Set("Accept", mediaTypeContentAttachmentsPreview)
288
289 m := &Attachment{}
290 resp, err := s.client.Do(ctx, req, m)
291 if err != nil {
292 return nil, resp, err
293 }
294
295 return m, resp, nil
296 }
297
298
299
300
301 func (s *AppsService) FindOrganizationInstallation(ctx context.Context, org string) (*Installation, *Response, error) {
302 return s.getInstallation(ctx, fmt.Sprintf("orgs/%v/installation", org))
303 }
304
305
306
307
308 func (s *AppsService) FindRepositoryInstallation(ctx context.Context, owner, repo string) (*Installation, *Response, error) {
309 return s.getInstallation(ctx, fmt.Sprintf("repos/%v/%v/installation", owner, repo))
310 }
311
312
313
314
315 func (s *AppsService) FindRepositoryInstallationByID(ctx context.Context, id int64) (*Installation, *Response, error) {
316 return s.getInstallation(ctx, fmt.Sprintf("repositories/%d/installation", id))
317 }
318
319
320
321
322 func (s *AppsService) FindUserInstallation(ctx context.Context, user string) (*Installation, *Response, error) {
323 return s.getInstallation(ctx, fmt.Sprintf("users/%v/installation", user))
324 }
325
326 func (s *AppsService) getInstallation(ctx context.Context, url string) (*Installation, *Response, error) {
327 req, err := s.client.NewRequest("GET", url, nil)
328 if err != nil {
329 return nil, nil, err
330 }
331
332 i := new(Installation)
333 resp, err := s.client.Do(ctx, req, i)
334 if err != nil {
335 return nil, resp, err
336 }
337
338 return i, resp, nil
339 }
340
View as plain text