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