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 type Alert struct {
22 RuleID *string `json:"rule_id,omitempty"`
23 RuleSeverity *string `json:"rule_severity,omitempty"`
24 RuleDescription *string `json:"rule_description,omitempty"`
25 Tool *string `json:"tool,omitempty"`
26 CreatedAt *Timestamp `json:"created_at,omitempty"`
27 Open *bool `json:"open,omitempty"`
28 ClosedBy *User `json:"closed_by,omitempty"`
29 ClosedAt *Timestamp `json:"closed_at,omitempty"`
30 URL *string `json:"url,omitempty"`
31 HTMLURL *string `json:"html_url,omitempty"`
32 }
33
34
35 func (a *Alert) ID() int64 {
36 if a == nil {
37 return 0
38 }
39
40 s := a.GetHTMLURL()
41
42
43 if i := strings.LastIndex(s, "/"); i >= 0 {
44 s = s[i+1:]
45 }
46
47
48 id, err := strconv.ParseInt(s, 10, 64)
49 if err != nil {
50 return 0
51 }
52
53 return id
54 }
55
56
57
58 type AlertListOptions struct {
59
60 State string `url:"state,omitempty"`
61
62
63 Ref string `url:"ref,omitempty"`
64 }
65
66
67
68
69
70
71
72
73 func (s *CodeScanningService) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *AlertListOptions) ([]*Alert, *Response, error) {
74 u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts", owner, repo)
75 u, err := addOptions(u, opts)
76 if err != nil {
77 return nil, nil, err
78 }
79
80 req, err := s.client.NewRequest("GET", u, nil)
81 if err != nil {
82 return nil, nil, err
83 }
84
85 var alerts []*Alert
86 resp, err := s.client.Do(ctx, req, &alerts)
87 if err != nil {
88 return nil, resp, err
89 }
90
91 return alerts, resp, nil
92 }
93
94
95
96
97
98
99
100
101
102 func (s *CodeScanningService) GetAlert(ctx context.Context, owner, repo string, id int64) (*Alert, *Response, error) {
103 u := fmt.Sprintf("repos/%v/%v/code-scanning/alerts/%v", owner, repo, id)
104
105 req, err := s.client.NewRequest("GET", u, nil)
106 if err != nil {
107 return nil, nil, err
108 }
109
110 a := new(Alert)
111 resp, err := s.client.Do(ctx, req, a)
112 if err != nil {
113 return nil, resp, err
114 }
115
116 return a, resp, nil
117 }
118
View as plain text