(statusCode int, endpointNeedsScopes, tokenHasScopes, hostname string)
| 359 | } |
| 360 | |
| 361 | func generateScopesSuggestion(statusCode int, endpointNeedsScopes, tokenHasScopes, hostname string) string { |
| 362 | if statusCode < 400 || statusCode > 499 || statusCode == 422 { |
| 363 | return "" |
| 364 | } |
| 365 | |
| 366 | if tokenHasScopes == "" { |
| 367 | return "" |
| 368 | } |
| 369 | |
| 370 | gotScopes := map[string]struct{}{} |
| 371 | for s := range strings.SplitSeq(tokenHasScopes, ",") { |
| 372 | s = strings.TrimSpace(s) |
| 373 | gotScopes[s] = struct{}{} |
| 374 | |
| 375 | // Certain scopes may be grouped under a single "top-level" scope. The following branch |
| 376 | // statements include these grouped/implied scopes when the top-level scope is encountered. |
| 377 | // See https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps. |
| 378 | if s == "repo" { |
| 379 | gotScopes["repo:status"] = struct{}{} |
| 380 | gotScopes["repo_deployment"] = struct{}{} |
| 381 | gotScopes["public_repo"] = struct{}{} |
| 382 | gotScopes["repo:invite"] = struct{}{} |
| 383 | gotScopes["security_events"] = struct{}{} |
| 384 | } else if s == "user" { |
| 385 | gotScopes["read:user"] = struct{}{} |
| 386 | gotScopes["user:email"] = struct{}{} |
| 387 | gotScopes["user:follow"] = struct{}{} |
| 388 | } else if s == "codespace" { |
| 389 | gotScopes["codespace:secrets"] = struct{}{} |
| 390 | } else if after, ok := strings.CutPrefix(s, "admin:"); ok { |
| 391 | gotScopes["read:"+after] = struct{}{} |
| 392 | gotScopes["write:"+strings.TrimPrefix(s, "admin:")] = struct{}{} |
| 393 | } else if after, ok := strings.CutPrefix(s, "write:"); ok { |
| 394 | gotScopes["read:"+after] = struct{}{} |
| 395 | } |
| 396 | } |
| 397 | |
| 398 | for s := range strings.SplitSeq(endpointNeedsScopes, ",") { |
| 399 | s = strings.TrimSpace(s) |
| 400 | if _, gotScope := gotScopes[s]; s == "" || gotScope { |
| 401 | continue |
| 402 | } |
| 403 | return fmt.Sprintf( |
| 404 | "This API operation needs the %[1]q scope. To request it, run: gh auth refresh -h %[2]s -s %[1]s", |
| 405 | s, |
| 406 | ghauth.NormalizeHostname(hostname), |
| 407 | ) |
| 408 | } |
| 409 | |
| 410 | return "" |
| 411 | } |
| 412 | |
| 413 | func clientOptions(hostname string, transport http.RoundTripper) ghAPI.ClientOptions { |
| 414 | // AuthToken, and Headers are being handled by transport, |
no outgoing calls
no test coverage detected