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