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 Repositories []string `json:"repositories,omitempty"`
53
54
55
56 Permissions *InstallationPermissions `json:"permissions,omitempty"`
57 }
58
59
60
61
62
63
64 type InstallationPermissions struct {
65 Actions *string `json:"actions,omitempty"`
66 Administration *string `json:"administration,omitempty"`
67 Blocking *string `json:"blocking,omitempty"`
68 Checks *string `json:"checks,omitempty"`
69 Contents *string `json:"contents,omitempty"`
70 ContentReferences *string `json:"content_references,omitempty"`
71 Deployments *string `json:"deployments,omitempty"`
72 Emails *string `json:"emails,omitempty"`
73 Environments *string `json:"environments,omitempty"`
74 Followers *string `json:"followers,omitempty"`
75 Issues *string `json:"issues,omitempty"`
76 Metadata *string `json:"metadata,omitempty"`
77 Members *string `json:"members,omitempty"`
78 OrganizationAdministration *string `json:"organization_administration,omitempty"`
79 OrganizationHooks *string `json:"organization_hooks,omitempty"`
80 OrganizationPlan *string `json:"organization_plan,omitempty"`
81 OrganizationPreReceiveHooks *string `json:"organization_pre_receive_hooks,omitempty"`
82 OrganizationProjects *string `json:"organization_projects,omitempty"`
83 OrganizationSecrets *string `json:"organization_secrets,omitempty"`
84 OrganizationSelfHostedRunners *string `json:"organization_self_hosted_runners,omitempty"`
85 OrganizationUserBlocking *string `json:"organization_user_blocking,omitempty"`
86 Packages *string `json:"packages,omitempty"`
87 Pages *string `json:"pages,omitempty"`
88 PullRequests *string `json:"pull_requests,omitempty"`
89 RepositoryHooks *string `json:"repository_hooks,omitempty"`
90 RepositoryProjects *string `json:"repository_projects,omitempty"`
91 RepositoryPreReceiveHooks *string `json:"repository_pre_receive_hooks,omitempty"`
92 Secrets *string `json:"secrets,omitempty"`
93 SecretScanningAlerts *string `json:"secret_scanning_alerts,omitempty"`
94 SecurityEvents *string `json:"security_events,omitempty"`
95 SingleFile *string `json:"single_file,omitempty"`
96 Statuses *string `json:"statuses,omitempty"`
97 TeamDiscussions *string `json:"team_discussions,omitempty"`
98 VulnerabilityAlerts *string `json:"vulnerability_alerts,omitempty"`
99 Workflows *string `json:"workflows,omitempty"`
100 }
101
102
103 type Installation struct {
104 ID *int64 `json:"id,omitempty"`
105 NodeID *string `json:"node_id,omitempty"`
106 AppID *int64 `json:"app_id,omitempty"`
107 AppSlug *string `json:"app_slug,omitempty"`
108 TargetID *int64 `json:"target_id,omitempty"`
109 Account *User `json:"account,omitempty"`
110 AccessTokensURL *string `json:"access_tokens_url,omitempty"`
111 RepositoriesURL *string `json:"repositories_url,omitempty"`
112 HTMLURL *string `json:"html_url,omitempty"`
113 TargetType *string `json:"target_type,omitempty"`
114 SingleFileName *string `json:"single_file_name,omitempty"`
115 RepositorySelection *string `json:"repository_selection,omitempty"`
116 Events []string `json:"events,omitempty"`
117 SingleFilePaths []string `json:"single_file_paths,omitempty"`
118 Permissions *InstallationPermissions `json:"permissions,omitempty"`
119 CreatedAt *Timestamp `json:"created_at,omitempty"`
120 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
121 HasMultipleSingleFiles *bool `json:"has_multiple_single_files,omitempty"`
122 SuspendedBy *User `json:"suspended_by,omitempty"`
123 SuspendedAt *Timestamp `json:"suspended_at,omitempty"`
124 }
125
126
127 type Attachment struct {
128 ID *int64 `json:"id,omitempty"`
129 Title *string `json:"title,omitempty"`
130 Body *string `json:"body,omitempty"`
131 }
132
133
134 type ContentReference struct {
135 ID *int64 `json:"id,omitempty"`
136 NodeID *string `json:"node_id,omitempty"`
137 Reference *string `json:"reference,omitempty"`
138 }
139
140 func (i Installation) String() string {
141 return Stringify(i)
142 }
143
144
145
146
147
148
149
150
151
152
153 func (s *AppsService) Get(ctx context.Context, appSlug string) (*App, *Response, error) {
154 var u string
155 if appSlug != "" {
156 u = fmt.Sprintf("apps/%v", appSlug)
157 } else {
158 u = "app"
159 }
160
161 req, err := s.client.NewRequest("GET", u, nil)
162 if err != nil {
163 return nil, nil, err
164 }
165
166 app := new(App)
167 resp, err := s.client.Do(ctx, req, app)
168 if err != nil {
169 return nil, resp, err
170 }
171
172 return app, resp, nil
173 }
174
175
176
177
178 func (s *AppsService) ListInstallations(ctx context.Context, opts *ListOptions) ([]*Installation, *Response, error) {
179 u, err := addOptions("app/installations", opts)
180 if err != nil {
181 return nil, nil, err
182 }
183
184 req, err := s.client.NewRequest("GET", u, nil)
185 if err != nil {
186 return nil, nil, err
187 }
188
189 var i []*Installation
190 resp, err := s.client.Do(ctx, req, &i)
191 if err != nil {
192 return nil, resp, err
193 }
194
195 return i, resp, nil
196 }
197
198
199
200
201 func (s *AppsService) GetInstallation(ctx context.Context, id int64) (*Installation, *Response, error) {
202 return s.getInstallation(ctx, fmt.Sprintf("app/installations/%v", id))
203 }
204
205
206
207
208 func (s *AppsService) ListUserInstallations(ctx context.Context, opts *ListOptions) ([]*Installation, *Response, error) {
209 u, err := addOptions("user/installations", opts)
210 if err != nil {
211 return nil, nil, err
212 }
213
214 req, err := s.client.NewRequest("GET", u, nil)
215 if err != nil {
216 return nil, nil, err
217 }
218
219 var i struct {
220 Installations []*Installation `json:"installations"`
221 }
222 resp, err := s.client.Do(ctx, req, &i)
223 if err != nil {
224 return nil, resp, err
225 }
226
227 return i.Installations, resp, nil
228 }
229
230
231
232
233 func (s *AppsService) SuspendInstallation(ctx context.Context, id int64) (*Response, error) {
234 u := fmt.Sprintf("app/installations/%v/suspended", id)
235
236 req, err := s.client.NewRequest("PUT", u, nil)
237 if err != nil {
238 return nil, err
239 }
240
241 return s.client.Do(ctx, req, nil)
242 }
243
244
245
246
247 func (s *AppsService) UnsuspendInstallation(ctx context.Context, id int64) (*Response, error) {
248 u := fmt.Sprintf("app/installations/%v/suspended", id)
249
250 req, err := s.client.NewRequest("DELETE", u, nil)
251 if err != nil {
252 return nil, err
253 }
254
255 return s.client.Do(ctx, req, nil)
256 }
257
258
259
260
261 func (s *AppsService) DeleteInstallation(ctx context.Context, id int64) (*Response, error) {
262 u := fmt.Sprintf("app/installations/%v", id)
263
264 req, err := s.client.NewRequest("DELETE", u, nil)
265 if err != nil {
266 return nil, err
267 }
268
269 return s.client.Do(ctx, req, nil)
270 }
271
272
273
274
275 func (s *AppsService) CreateInstallationToken(ctx context.Context, id int64, opts *InstallationTokenOptions) (*InstallationToken, *Response, error) {
276 u := fmt.Sprintf("app/installations/%v/access_tokens", id)
277
278 req, err := s.client.NewRequest("POST", u, opts)
279 if err != nil {
280 return nil, nil, err
281 }
282
283 t := new(InstallationToken)
284 resp, err := s.client.Do(ctx, req, t)
285 if err != nil {
286 return nil, resp, err
287 }
288
289 return t, resp, nil
290 }
291
292
293
294
295 func (s *AppsService) CreateAttachment(ctx context.Context, contentReferenceID int64, title, body string) (*Attachment, *Response, error) {
296 u := fmt.Sprintf("content_references/%v/attachments", contentReferenceID)
297 payload := &Attachment{Title: String(title), Body: String(body)}
298 req, err := s.client.NewRequest("POST", u, payload)
299 if err != nil {
300 return nil, nil, err
301 }
302
303
304 req.Header.Set("Accept", mediaTypeContentAttachmentsPreview)
305
306 m := &Attachment{}
307 resp, err := s.client.Do(ctx, req, m)
308 if err != nil {
309 return nil, resp, err
310 }
311
312 return m, resp, nil
313 }
314
315
316
317
318 func (s *AppsService) FindOrganizationInstallation(ctx context.Context, org string) (*Installation, *Response, error) {
319 return s.getInstallation(ctx, fmt.Sprintf("orgs/%v/installation", org))
320 }
321
322
323
324
325 func (s *AppsService) FindRepositoryInstallation(ctx context.Context, owner, repo string) (*Installation, *Response, error) {
326 return s.getInstallation(ctx, fmt.Sprintf("repos/%v/%v/installation", owner, repo))
327 }
328
329
330
331
332 func (s *AppsService) FindRepositoryInstallationByID(ctx context.Context, id int64) (*Installation, *Response, error) {
333 return s.getInstallation(ctx, fmt.Sprintf("repositories/%d/installation", id))
334 }
335
336
337
338
339 func (s *AppsService) FindUserInstallation(ctx context.Context, user string) (*Installation, *Response, error) {
340 return s.getInstallation(ctx, fmt.Sprintf("users/%v/installation", user))
341 }
342
343 func (s *AppsService) getInstallation(ctx context.Context, url string) (*Installation, *Response, error) {
344 req, err := s.client.NewRequest("GET", url, nil)
345 if err != nil {
346 return nil, nil, err
347 }
348
349 i := new(Installation)
350 resp, err := s.client.Do(ctx, req, i)
351 if err != nil {
352 return nil, resp, err
353 }
354
355 return i, resp, nil
356 }
357
View as plain text