github.com/google/go-github/v74@v74.0.0/github/secret_scanning.go (about) 1 // Copyright 2022 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 ) 12 13 // SecretScanningService handles communication with the secret scanning related 14 // methods of the GitHub API. 15 type SecretScanningService service 16 17 // SecretScanningAlert represents a GitHub secret scanning alert. 18 type SecretScanningAlert struct { 19 Number *int `json:"number,omitempty"` 20 CreatedAt *Timestamp `json:"created_at,omitempty"` 21 URL *string `json:"url,omitempty"` 22 HTMLURL *string `json:"html_url,omitempty"` 23 LocationsURL *string `json:"locations_url,omitempty"` 24 State *string `json:"state,omitempty"` 25 Resolution *string `json:"resolution,omitempty"` 26 ResolvedAt *Timestamp `json:"resolved_at,omitempty"` 27 ResolvedBy *User `json:"resolved_by,omitempty"` 28 SecretType *string `json:"secret_type,omitempty"` 29 SecretTypeDisplayName *string `json:"secret_type_display_name,omitempty"` 30 Secret *string `json:"secret,omitempty"` 31 Repository *Repository `json:"repository,omitempty"` 32 UpdatedAt *Timestamp `json:"updated_at,omitempty"` 33 IsBase64Encoded *bool `json:"is_base64_encoded,omitempty"` 34 MultiRepo *bool `json:"multi_repo,omitempty"` 35 PubliclyLeaked *bool `json:"publicly_leaked,omitempty"` 36 PushProtectionBypassed *bool `json:"push_protection_bypassed,omitempty"` 37 PushProtectionBypassedBy *User `json:"push_protection_bypassed_by,omitempty"` 38 PushProtectionBypassedAt *Timestamp `json:"push_protection_bypassed_at,omitempty"` 39 ResolutionComment *string `json:"resolution_comment,omitempty"` 40 PushProtectionBypassRequestComment *string `json:"push_protection_bypass_request_comment,omitempty"` 41 PushProtectionBypassRequestHTMLURL *string `json:"push_protection_bypass_request_html_url,omitempty"` 42 PushProtectionBypassRequestReviewer *User `json:"push_protection_bypass_request_reviewer,omitempty"` 43 PushProtectionBypassRequestReviewerComment *string `json:"push_protection_bypass_request_reviewer_comment,omitempty"` 44 Validity *string `json:"validity,omitempty"` 45 } 46 47 // SecretScanningAlertLocation represents the location for a secret scanning alert. 48 type SecretScanningAlertLocation struct { 49 Type *string `json:"type,omitempty"` 50 Details *SecretScanningAlertLocationDetails `json:"details,omitempty"` 51 } 52 53 // SecretScanningAlertLocationDetails represents the location details for a secret scanning alert. 54 type SecretScanningAlertLocationDetails struct { 55 Path *string `json:"path,omitempty"` 56 Startline *int `json:"start_line,omitempty"` 57 EndLine *int `json:"end_line,omitempty"` 58 StartColumn *int `json:"start_column,omitempty"` 59 EndColumn *int `json:"end_column,omitempty"` 60 BlobSHA *string `json:"blob_sha,omitempty"` 61 BlobURL *string `json:"blob_url,omitempty"` 62 CommitSHA *string `json:"commit_sha,omitempty"` 63 CommitURL *string `json:"commit_url,omitempty"` 64 PullRequestCommentURL *string `json:"pull_request_comment_url,omitempty"` 65 } 66 67 // SecretScanningAlertListOptions specifies optional parameters to the SecretScanningService.ListAlertsForEnterprise method. 68 type SecretScanningAlertListOptions struct { 69 // State of the secret scanning alerts to list. Set to open or resolved to only list secret scanning alerts in a specific state. 70 State string `url:"state,omitempty"` 71 72 // A comma-separated list of secret types to return. By default all secret types are returned. 73 SecretType string `url:"secret_type,omitempty"` 74 75 // A comma-separated list of resolutions. Only secret scanning alerts with one of these resolutions are listed. 76 // Valid resolutions are false_positive, wont_fix, revoked, pattern_edited, pattern_deleted or used_in_tests. 77 Resolution string `url:"resolution,omitempty"` 78 79 // A comma-separated list of validities that, when present, will return alerts that match the validities in this list. 80 // Valid options are active, inactive, and unknown. 81 Validity string `url:"validity,omitempty"` 82 83 // A boolean value representing whether or not to filter alerts by the publicly-leaked tag being present. Default: false. 84 IsPubliclyLeaked bool `url:"is_publicly_leaked,omitempty"` 85 86 // A boolean value representing whether or not to filter alerts by the multi-repo tag being present. Default: false. 87 IsMultiRepo bool `url:"is_multi_repo,omitempty"` 88 89 // The direction to sort the results by. Possible values are: asc, desc. Default: desc. 90 Direction string `url:"direction,omitempty"` 91 92 // The property by which to sort the results. Possible values are: created, updated. Default: created. 93 Sort string `url:"sort,omitempty"` 94 95 ListCursorOptions 96 97 // List options can vary on the Enterprise type. 98 // On Enterprise Cloud, Secret Scan alerts support requesting by page number 99 // along with providing a cursor for an "after" param. 100 // See: https://docs.github.com/enterprise-cloud@latest/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization 101 // Whereas on Enterprise Server, pagination is by index. 102 // See: https://docs.github.com/enterprise-server@3.6/rest/secret-scanning#list-secret-scanning-alerts-for-an-organization 103 ListOptions 104 } 105 106 // SecretScanningAlertUpdateOptions specifies optional parameters to the SecretScanningService.UpdateAlert method. 107 type SecretScanningAlertUpdateOptions struct { 108 // State is required and sets the state of the secret scanning alert. 109 // Can be either "open" or "resolved". 110 // You must provide resolution when you set the state to "resolved". 111 State string `json:"state"` 112 113 // Required when the state is "resolved" and represents the reason for resolving the alert. 114 // Can be one of: "false_positive", "wont_fix", "revoked", or "used_in_tests". 115 Resolution *string `json:"resolution,omitempty"` 116 117 // An optional comment when closing an alert. 118 ResolutionComment *string `json:"resolution_comment,omitempty"` 119 } 120 121 // ListAlertsForEnterprise lists secret scanning alerts for eligible repositories in an enterprise, from newest to oldest. 122 // 123 // To use this endpoint, you must be a member of the enterprise, and you must use an access token with the repo scope or 124 // security_events scope. Alerts are only returned for organizations in the enterprise for which you are an organization owner or a security manager. 125 // 126 // GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-secret-scanning-alerts-for-an-enterprise 127 // 128 //meta:operation GET /enterprises/{enterprise}/secret-scanning/alerts 129 func (s *SecretScanningService) ListAlertsForEnterprise(ctx context.Context, enterprise string, opts *SecretScanningAlertListOptions) ([]*SecretScanningAlert, *Response, error) { 130 u := fmt.Sprintf("enterprises/%v/secret-scanning/alerts", enterprise) 131 u, err := addOptions(u, opts) 132 if err != nil { 133 return nil, nil, err 134 } 135 136 req, err := s.client.NewRequest("GET", u, nil) 137 if err != nil { 138 return nil, nil, err 139 } 140 141 var alerts []*SecretScanningAlert 142 resp, err := s.client.Do(ctx, req, &alerts) 143 if err != nil { 144 return nil, resp, err 145 } 146 147 return alerts, resp, nil 148 } 149 150 // ListAlertsForOrg lists secret scanning alerts for eligible repositories in an organization, from newest to oldest. 151 // 152 // To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with 153 // the repo scope or security_events scope. 154 // 155 // GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-secret-scanning-alerts-for-an-organization 156 // 157 //meta:operation GET /orgs/{org}/secret-scanning/alerts 158 func (s *SecretScanningService) ListAlertsForOrg(ctx context.Context, org string, opts *SecretScanningAlertListOptions) ([]*SecretScanningAlert, *Response, error) { 159 u := fmt.Sprintf("orgs/%v/secret-scanning/alerts", org) 160 u, err := addOptions(u, opts) 161 if err != nil { 162 return nil, nil, err 163 } 164 165 req, err := s.client.NewRequest("GET", u, nil) 166 if err != nil { 167 return nil, nil, err 168 } 169 170 var alerts []*SecretScanningAlert 171 resp, err := s.client.Do(ctx, req, &alerts) 172 if err != nil { 173 return nil, resp, err 174 } 175 176 return alerts, resp, nil 177 } 178 179 // ListAlertsForRepo lists secret scanning alerts for a private repository, from newest to oldest. 180 // 181 // To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with 182 // the repo scope or security_events scope. 183 // 184 // GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-secret-scanning-alerts-for-a-repository 185 // 186 //meta:operation GET /repos/{owner}/{repo}/secret-scanning/alerts 187 func (s *SecretScanningService) ListAlertsForRepo(ctx context.Context, owner, repo string, opts *SecretScanningAlertListOptions) ([]*SecretScanningAlert, *Response, error) { 188 u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts", owner, repo) 189 u, err := addOptions(u, opts) 190 if err != nil { 191 return nil, nil, err 192 } 193 194 req, err := s.client.NewRequest("GET", u, nil) 195 if err != nil { 196 return nil, nil, err 197 } 198 199 var alerts []*SecretScanningAlert 200 resp, err := s.client.Do(ctx, req, &alerts) 201 if err != nil { 202 return nil, resp, err 203 } 204 205 return alerts, resp, nil 206 } 207 208 // GetAlert gets a single secret scanning alert detected in a private repository. 209 // 210 // To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with 211 // the repo scope or security_events scope. 212 // 213 // GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#get-a-secret-scanning-alert 214 // 215 //meta:operation GET /repos/{owner}/{repo}/secret-scanning/alerts/{alert_number} 216 func (s *SecretScanningService) GetAlert(ctx context.Context, owner, repo string, number int64) (*SecretScanningAlert, *Response, error) { 217 u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts/%v", owner, repo, number) 218 219 req, err := s.client.NewRequest("GET", u, nil) 220 if err != nil { 221 return nil, nil, err 222 } 223 224 var alert *SecretScanningAlert 225 resp, err := s.client.Do(ctx, req, &alert) 226 if err != nil { 227 return nil, resp, err 228 } 229 230 return alert, resp, nil 231 } 232 233 // UpdateAlert updates the status of a secret scanning alert in a private repository. 234 // 235 // To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with 236 // the repo scope or security_events scope. 237 // 238 // GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#update-a-secret-scanning-alert 239 // 240 //meta:operation PATCH /repos/{owner}/{repo}/secret-scanning/alerts/{alert_number} 241 func (s *SecretScanningService) UpdateAlert(ctx context.Context, owner, repo string, number int64, opts *SecretScanningAlertUpdateOptions) (*SecretScanningAlert, *Response, error) { 242 u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts/%v", owner, repo, number) 243 244 req, err := s.client.NewRequest("PATCH", u, opts) 245 if err != nil { 246 return nil, nil, err 247 } 248 249 var alert *SecretScanningAlert 250 resp, err := s.client.Do(ctx, req, &alert) 251 if err != nil { 252 return nil, resp, err 253 } 254 255 return alert, resp, nil 256 } 257 258 // ListLocationsForAlert lists all locations for a given secret scanning alert for a private repository. 259 // 260 // To use this endpoint, you must be an administrator for the repository or organization, and you must use an access token with 261 // the repo scope or security_events scope. 262 // 263 // GitHub API docs: https://docs.github.com/rest/secret-scanning/secret-scanning#list-locations-for-a-secret-scanning-alert 264 // 265 //meta:operation GET /repos/{owner}/{repo}/secret-scanning/alerts/{alert_number}/locations 266 func (s *SecretScanningService) ListLocationsForAlert(ctx context.Context, owner, repo string, number int64, opts *ListOptions) ([]*SecretScanningAlertLocation, *Response, error) { 267 u := fmt.Sprintf("repos/%v/%v/secret-scanning/alerts/%v/locations", owner, repo, number) 268 u, err := addOptions(u, opts) 269 if err != nil { 270 return nil, nil, err 271 } 272 273 req, err := s.client.NewRequest("GET", u, nil) 274 if err != nil { 275 return nil, nil, err 276 } 277 278 var locations []*SecretScanningAlertLocation 279 resp, err := s.client.Do(ctx, req, &locations) 280 if err != nil { 281 return nil, resp, err 282 } 283 284 return locations, resp, nil 285 }