MCPcopy Create free account
hub / github.com/cli/cli / generateScopesSuggestion

Function generateScopesSuggestion

api/client.go:362–412  ·  view source on GitHub ↗
(statusCode int, endpointNeedsScopes, tokenHasScopes, hostname string)

Source from the content-addressed store, hash-verified

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

Callers 2

handleResponseFunction · 0.85
ScopesSuggestionFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected