1
2
3
4
5
6 package github
7
8 import (
9 "context"
10 "fmt"
11 "strconv"
12 "strings"
13 )
14
15
16
17
18
19 type CodeScanningService service
20
21
22 type Rule struct {
23 ID *string `json:"id,omitempty"`
24 Severity *string `json:"severity,omitempty"`
25 Description *string `json:"description,omitempty"`
26 Name *string `json:"name,omitempty"`
27 SecuritySeverityLevel *string `json:"security_severity_level,omitempty"`
28 FullDescription *string `json:"full_description,omitempty"`
29 Tags []string `json:"tags,omitempty"`
30 Help *string `json:"help,omitempty"`
31 }
32
33
34 type Location struct {
35 Path *string `json:"path,omitempty"`
36 StartLine *int `json:"start_line,omitempty"`
37 EndLine *int `json:"end_line,omitempty"`
38 StartColumn *int `json:"start_column,omitempty"`
39 EndColumn *int `json:"end_column,omitempty"`
40 }
41
42
43 type Message struct {
44 Text *string `json:"text,omitempty"`
45 }
46
47
48 type MostRecentInstance struct {
49 Ref *string `json:"ref,omitempty"`
50 AnalysisKey *string `json:"analysis_key,omitempty"`
51 Category *string `json:"category,omitempty"`
52 Environment *string `json:"environment,omitempty"`
53 State *string `json:"state,omitempty"`
54 CommitSHA *string `json:"commit_sha,omitempty"`
55 Message *Message `json:"message,omitempty"`
56 Location *Location `json:"location,omitempty"`
57 HTMLURL *string `json:"html_url,omitempty"`
58 Classifications []string `json:"classifications,omitempty"`
59 }
60
61
62 type Tool struct {
63 Name *string `json:"name,omitempty"`
64 GUID *string `json:"guid,omitempty"`
65 Version *string `json:"version,omitempty"`
66 }
67
68
69
70
71 type Alert struct {
72 Number *int `json:"number,omitempty"`
73 Repository *Repository `json:"repository,omitempty"`
74 RuleID *string `json:"rule_id,omitempty"`
75 RuleSeverity *string `json:"rule_severity,omitempty"`
76 RuleDescription *string `json:"rule_description,omitempty"`
77 Rule *Rule `json:"rule,omitempty"`
78 Tool *Tool `json:"tool,omitempty"`
79 CreatedAt *Timestamp `json:"created_at,omitempty"`
80 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
81 FixedAt *Timestamp `json:"fixed_at,omitempty"`
82 State *string `json:"state,omitempty"`
83 ClosedBy *User `json:"closed_by,omitempty"`
84 ClosedAt *Timestamp `json:"closed_at,omitempty"`
85 URL *string `json:"url,omitempty"`
86 HTMLURL *string `json:"html_url,omitempty"`
87 MostRecentInstance *MostRecentInstance `json:"most_recent_instance,omitempty"`
88 Instances []*MostRecentInstance `json:"instances,omitempty"`
89 DismissedBy *User `json:"dismissed_by,omitempty"`
90 DismissedAt *Timestamp `json:"dismissed_at,omitempty"`
91 DismissedReason *string `json:"dismissed_reason,omitempty"`
92 DismissedComment *string `json:"dismissed_comment,omitempty"`
93 InstancesURL *string `json:"instances_url,omitempty"`
94 }
95
96
97 func (a *Alert) ID() int64 {
98 if a == nil {
99 return 0
100 }
101
102 s := a.GetHTMLURL()
103
104
105 if i := strings.LastIndex(s, "/"); i >= 0 {
106 s = s[i+1:]
107 }
108
109
110 id, err := strconv.ParseInt(s, 10, 64)
111 if err != nil {
112 return 0
113 }
114
115 return id
116 }
117
118
119 type AlertInstancesListOptions struct {
120
121
122 Ref string `url:"ref,omitempty"`
123
124 ListOptions
125 }
126
127
128 type AlertListOptions struct {
129
130 State string `url:"state,omitempty"`
131
132
133
134 Ref string `url:"ref,omitempty"`
135
136
137 Severity string `url:"severity,omitempty"`
138
139
140 ToolName string `url:"tool_name,omitempty"`
141
142 ListCursorOptions
143
144
145
146 ListOptions
147 }
148
149
150 type AnalysesListOptions struct {
151
152 SarifID *string `url:"sarif_id,omitempty"`
153
154
155
156 Ref *string `url:"ref,omitempty"`
157
158 ListOptions
159 }
160
161
162
163
164 type CodeQLDatabase struct {
165 ID *int64 `json:"id,omitempty"`
166 Name *string `json:"name,omitempty"`
167 Language *string `json:"language,omitempty"`
168 Uploader *User `json:"uploader,omitempty"`
169 ContentType *string `json:"content_type,omitempty"`
170 Size *int64 `json:"size,omitempty"`
171 CreatedAt *Timestamp `json:"created_at,omitempty"`
172 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
173 URL *string `json:"url,omitempty"`
174 }
175
176
177
178
179 type ScanningAnalysis struct {
180 ID *int64 `json:"id,omitempty"`
181 Ref *string `json:"ref,omitempty"`
182 CommitSHA *string `json:"commit_sha,omitempty"`
183 AnalysisKey *string `json:"analysis_key,omitempty"`
184 Environment *string `json:"environment,omitempty"`
185 Error *string `json:"error,omitempty"`
186 Category *string `json:"category,omitempty"`
187 CreatedAt *Timestamp `json:"created_at,omitempty"`
188 ResultsCount *int `json:"results_count,omitempty"`
189 RulesCount *int `json:"rules_count,omitempty"`
190 URL *string `json:"url,omitempty"`
191 SarifID *string `json:"sarif_id,omitempty"`
192 Tool *Tool `json:"tool,omitempty"`
193 Deletable *bool `json:"deletable,omitempty"`
194 Warning *string `json:"warning,omitempty"`
195 }
196
197
198
199
200 type SarifAnalysis struct {
201 CommitSHA *string `json:"commit_sha,omitempty"`
202 Ref *string `json:"ref,omitempty"`
203 Sarif *string `json:"sarif,omitempty"`
204 CheckoutURI *string `json:"checkout_uri,omitempty"`
205 StartedAt *Timestamp `json:"started_at,omitempty"`
206 ToolName *string `json:"tool_name,omitempty"`
207 }
208
209
210
211
212 type CodeScanningAlertState struct {
213
214
215
216 State string `json:"state"`
217
218
219
220 DismissedReason *string `json:"dismissed_reason,omitempty"`
221
222 DismissedComment *string `json:"dismissed_comment,omitempty"`
223 }
224
225
226
227
228 type SarifID struct {
229 ID *string `json:"id,omitempty"`
230 URL *string `json:"url,omitempty"`
231 }
232
233
234
235
236
237
238
239 func (s *CodeScanningService) ListAlertsForOrg(ctx context.Context, org string, opts *AlertListOptions) ([]*Alert, *Response, error) {
240 u := fmt.Sprintf("orgs/%v/code-scanning/alerts", org)
241 u, err := addOptions(u, opts)
242 if err != nil {
243 return nil, nil, err
244 }
245
246 req, err := s.client.NewRequest("GET", u, nil)
247 if err != nil {
248 return nil, nil, err
249 }
250
251 var alerts []*Alert
252 resp, err := s.client.Do(ctx, req, &alerts)
253 if err != nil {
254 return nil, resp, err
255 }
256
257 return alerts, resp, nil
258 }
259
260
261
262
263
264
265
266
267 func (s *CodeScanningService) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *AlertListOptions) ([]*Alert, *Response, error) {
268 u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts", owner, repo)
269 u, err := addOptions(u, opts)
270 if err != nil {
271 return nil, nil, err
272 }
273
274 req, err := s.client.NewRequest("GET", u, nil)
275 if err != nil {
276 return nil, nil, err
277 }
278
279 var alerts []*Alert
280 resp, err := s.client.Do(ctx, req, &alerts)
281 if err != nil {
282 return nil, resp, err
283 }
284
285 return alerts, resp, nil
286 }
287
288
289
290
291
292
293
294
295
296 func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, id int64) (*Alert, *Response, error) {
297 u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts/%v", owner, repo, id)
298
299 req, err := s.client.NewRequest("GET", u, nil)
300 if err != nil {
301 return nil, nil, err
302 }
303
304 a := new(Alert)
305 resp, err := s.client.Do(ctx, req, a)
306 if err != nil {
307 return nil, resp, err
308 }
309
310 return a, resp, nil
311 }
312
313
314
315
316
317
318
319
320
321 func (s *CodeScanningService) UpdateAlert(ctx context.Context, owner, repo string, id int64, stateInfo *CodeScanningAlertState) (*Alert, *Response, error) {
322 u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts/%v", owner, repo, id)
323
324 req, err := s.client.NewRequest("PATCH", u, stateInfo)
325 if err != nil {
326 return nil, nil, err
327 }
328
329 a := new(Alert)
330 resp, err := s.client.Do(ctx, req, a)
331 if err != nil {
332 return nil, resp, err
333 }
334
335 return a, resp, nil
336 }
337
338
339
340
341
342
343
344 func (s *CodeScanningService) ListAlertInstances(ctx context.Context, owner, repo string, id int64, opts *AlertInstancesListOptions) ([]*MostRecentInstance, *Response, error) {
345 u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts/%v/instances", owner, repo, id)
346 u, err := addOptions(u, opts)
347 if err != nil {
348 return nil, nil, err
349 }
350
351 req, err := s.client.NewRequest("GET", u, nil)
352 if err != nil {
353 return nil, nil, err
354 }
355
356 var alertInstances []*MostRecentInstance
357 resp, err := s.client.Do(ctx, req, &alertInstances)
358 if err != nil {
359 return nil, resp, err
360 }
361
362 return alertInstances, resp, nil
363 }
364
365
366
367
368
369
370
371
372 func (s *CodeScanningService) UploadSarif(ctx context.Context, owner, repo string, sarif *SarifAnalysis) (*SarifID, *Response, error) {
373 u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs", owner, repo)
374
375 req, err := s.client.NewRequest("POST", u, sarif)
376 if err != nil {
377 return nil, nil, err
378 }
379
380 sarifID := new(SarifID)
381 resp, err := s.client.Do(ctx, req, sarifID)
382 if err != nil {
383 return nil, resp, err
384 }
385
386 return sarifID, resp, nil
387 }
388
389
390 type SARIFUpload struct {
391
392
393 ProcessingStatus *string `json:"processing_status,omitempty"`
394
395 AnalysesURL *string `json:"analyses_url,omitempty"`
396 }
397
398
399
400
401
402
403
404 func (s *CodeScanningService) GetSARIF(ctx context.Context, owner, repo, sarifID string) (*SARIFUpload, *Response, error) {
405 u := fmt.Sprintf("repos/%v/%v/code-scanning/sarifs/%v", owner, repo, sarifID)
406
407 req, err := s.client.NewRequest("GET", u, nil)
408 if err != nil {
409 return nil, nil, err
410 }
411
412 sarifUpload := new(SARIFUpload)
413 resp, err := s.client.Do(ctx, req, sarifUpload)
414 if err != nil {
415 return nil, resp, err
416 }
417
418 return sarifUpload, resp, nil
419 }
420
421
422
423
424
425
426
427
428 func (s *CodeScanningService) ListAnalysesForRepo(ctx context.Context, owner, repo string, opts *AnalysesListOptions) ([]*ScanningAnalysis, *Response, error) {
429 u := fmt.Sprintf("repos/%v/%v/code-scanning/analyses", owner, repo)
430 u, err := addOptions(u, opts)
431 if err != nil {
432 return nil, nil, err
433 }
434
435 req, err := s.client.NewRequest("GET", u, nil)
436 if err != nil {
437 return nil, nil, err
438 }
439
440 var analyses []*ScanningAnalysis
441 resp, err := s.client.Do(ctx, req, &analyses)
442 if err != nil {
443 return nil, resp, err
444 }
445
446 return analyses, resp, nil
447 }
448
449
450
451
452
453
454
455
456
457 func (s *CodeScanningService) GetAnalysis(ctx context.Context, owner, repo string, id int64) (*ScanningAnalysis, *Response, error) {
458 u := fmt.Sprintf("repos/%v/%v/code-scanning/analyses/%v", owner, repo, id)
459
460 req, err := s.client.NewRequest("GET", u, nil)
461 if err != nil {
462 return nil, nil, err
463 }
464
465 analysis := new(ScanningAnalysis)
466 resp, err := s.client.Do(ctx, req, analysis)
467 if err != nil {
468 return nil, resp, err
469 }
470
471 return analysis, resp, nil
472 }
473
474
475 type DeleteAnalysis struct {
476
477 NextAnalysisURL *string `json:"next_analysis_url,omitempty"`
478
479 ConfirmDeleteURL *string `json:"confirm_delete_url,omitempty"`
480 }
481
482
483
484
485
486
487
488
489
490 func (s *CodeScanningService) DeleteAnalysis(ctx context.Context, owner, repo string, id int64) (*DeleteAnalysis, *Response, error) {
491 u := fmt.Sprintf("repos/%v/%v/code-scanning/analyses/%v", owner, repo, id)
492
493 req, err := s.client.NewRequest("DELETE", u, nil)
494 if err != nil {
495 return nil, nil, err
496 }
497
498 deleteAnalysis := new(DeleteAnalysis)
499 resp, err := s.client.Do(ctx, req, deleteAnalysis)
500 if err != nil {
501 return nil, resp, err
502 }
503
504 return deleteAnalysis, resp, nil
505 }
506
507
508
509
510
511
512
513 func (s *CodeScanningService) ListCodeQLDatabases(ctx context.Context, owner, repo string) ([]*CodeQLDatabase, *Response, error) {
514 u := fmt.Sprintf("repos/%v/%v/code-scanning/codeql/databases", owner, repo)
515
516 req, err := s.client.NewRequest("GET", u, nil)
517 if err != nil {
518 return nil, nil, err
519 }
520
521 var codeqlDatabases []*CodeQLDatabase
522 resp, err := s.client.Do(ctx, req, &codeqlDatabases)
523 if err != nil {
524 return nil, resp, err
525 }
526
527 return codeqlDatabases, resp, nil
528 }
529
530
531
532
533
534
535
536 func (s *CodeScanningService) GetCodeQLDatabase(ctx context.Context, owner, repo, language string) (*CodeQLDatabase, *Response, error) {
537 u := fmt.Sprintf("repos/%v/%v/code-scanning/codeql/databases/%v", owner, repo, language)
538
539 req, err := s.client.NewRequest("GET", u, nil)
540 if err != nil {
541 return nil, nil, err
542 }
543
544 codeqlDatabase := new(CodeQLDatabase)
545 resp, err := s.client.Do(ctx, req, codeqlDatabase)
546 if err != nil {
547 return nil, resp, err
548 }
549
550 return codeqlDatabase, resp, nil
551 }
552
553
554 type DefaultSetupConfiguration struct {
555 State *string `json:"state,omitempty"`
556 Languages []string `json:"languages,omitempty"`
557 QuerySuite *string `json:"query_suite,omitempty"`
558 UpdatedAt *Timestamp `json:"updated_at,omitempty"`
559 }
560
561
562
563
564
565
566
567
568 func (s *CodeScanningService) GetDefaultSetupConfiguration(ctx context.Context, owner, repo string) (*DefaultSetupConfiguration, *Response, error) {
569 u := fmt.Sprintf("repos/%s/%s/code-scanning/default-setup", owner, repo)
570
571 req, err := s.client.NewRequest("GET", u, nil)
572 if err != nil {
573 return nil, nil, err
574 }
575
576 cfg := new(DefaultSetupConfiguration)
577 resp, err := s.client.Do(ctx, req, cfg)
578 if err != nil {
579 return nil, resp, err
580 }
581
582 return cfg, resp, nil
583 }
584
585
586
587 type UpdateDefaultSetupConfigurationOptions struct {
588 State string `json:"state"`
589 QuerySuite *string `json:"query_suite,omitempty"`
590 Languages []string `json:"languages,omitempty"`
591 }
592
593
594 type UpdateDefaultSetupConfigurationResponse struct {
595 RunID *int64 `json:"run_id,omitempty"`
596 RunURL *string `json:"run_url,omitempty"`
597 }
598
599
600
601
602
603
604
605
606
607
608
609 func (s *CodeScanningService) UpdateDefaultSetupConfiguration(ctx context.Context, owner, repo string, options *UpdateDefaultSetupConfigurationOptions) (*UpdateDefaultSetupConfigurationResponse, *Response, error) {
610 u := fmt.Sprintf("repos/%s/%s/code-scanning/default-setup", owner, repo)
611
612 req, err := s.client.NewRequest("PATCH", u, options)
613 if err != nil {
614 return nil, nil, err
615 }
616
617 a := new(UpdateDefaultSetupConfigurationResponse)
618 resp, err := s.client.Do(ctx, req, a)
619 if err != nil {
620 return nil, resp, err
621 }
622
623 return a, resp, nil
624 }
625
View as plain text