github.com/google/go-github/v33@v33.0.0/github/code-scanning.go (about) 1 // Copyright 2020 The go-github AUTHORS. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 package github 7 8 import ( 9 "context" 10 "fmt" 11 "strconv" 12 "strings" 13 ) 14 15 // CodeScanningService handles communication with the code scanning related 16 // methods of the GitHub API. 17 // 18 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/code-scanning/ 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 // ID returns the ID associated with an alert. It is the number at the end of the security alert's URL. 35 func (a *Alert) ID() int64 { 36 if a == nil { 37 return 0 38 } 39 40 s := a.GetHTMLURL() 41 42 // Check for an ID to parse at the end of the url 43 if i := strings.LastIndex(s, "/"); i >= 0 { 44 s = s[i+1:] 45 } 46 47 // Return the alert ID as a 64-bit integer. Unable to convert or out of range returns 0. 48 id, err := strconv.ParseInt(s, 10, 64) 49 if err != nil { 50 return 0 51 } 52 53 return id 54 } 55 56 // AlertListOptions specifies optional parameters to the CodeScanningService.ListAlerts 57 // method. 58 type AlertListOptions struct { 59 // State of the code scanning alerts to list. Set to closed to list only closed code scanning alerts. Default: open 60 State string `url:"state,omitempty"` 61 62 // Return code scanning alerts for a specific branch reference. The ref must be formatted as heads/<branch name>. 63 Ref string `url:"ref,omitempty"` 64 } 65 66 // ListAlertsForRepo lists code scanning alerts for a repository. 67 // 68 // Lists all open code scanning alerts for the default branch (usually master) and protected branches in a repository. 69 // You must use an access token with the security_events scope to use this endpoint. GitHub Apps must have the security_events 70 // read permission to use this endpoint. 71 // 72 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/code-scanning/#list-code-scanning-alerts-for-a-repository 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 // GetAlert gets a single code scanning alert for a repository. 95 // 96 // You must use an access token with the security_events scope to use this endpoint. 97 // GitHub Apps must have the security_events read permission to use this endpoint. 98 // 99 // The security alert_id is the number at the end of the security alert's URL. 100 // 101 // GitHub API docs: https://docs.github.com/en/free-pro-team@latest/rest/reference/code-scanning/#get-a-code-scanning-alert 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 }