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

Function generateScopesSuggestion

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

Source from the content-addressed store, hash-verified

202}
203
204func generateScopesSuggestion(statusCode int, endpointNeedsScopes, tokenHasScopes, hostname string) string {
205 if statusCode < 400 || statusCode > 499 || statusCode == 422 {
206 return ""
207 }
208
209 if tokenHasScopes == "" {
210 return ""
211 }
212
213 gotScopes := map[string]struct{}{}
214 for _, s := range strings.Split(tokenHasScopes, ",") {
215 s = strings.TrimSpace(s)
216 gotScopes[s] = struct{}{}
217
218 // Certain scopes may be grouped under a single "top-level" scope. The following branch
219 // statements include these grouped/implied scopes when the top-level scope is encountered.
220 // See https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps.
221 if s == "repo" {
222 gotScopes["repo:status"] = struct{}{}
223 gotScopes["repo_deployment"] = struct{}{}
224 gotScopes["public_repo"] = struct{}{}
225 gotScopes["repo:invite"] = struct{}{}
226 gotScopes["security_events"] = struct{}{}
227 } else if s == "user" {
228 gotScopes["read:user"] = struct{}{}
229 gotScopes["user:email"] = struct{}{}
230 gotScopes["user:follow"] = struct{}{}
231 } else if s == "codespace" {
232 gotScopes["codespace:secrets"] = struct{}{}
233 } else if strings.HasPrefix(s, "admin:") {
234 gotScopes["read:"+strings.TrimPrefix(s, "admin:")] = struct{}{}
235 gotScopes["write:"+strings.TrimPrefix(s, "admin:")] = struct{}{}
236 } else if strings.HasPrefix(s, "write:") {
237 gotScopes["read:"+strings.TrimPrefix(s, "write:")] = struct{}{}
238 }
239 }
240
241 for _, s := range strings.Split(endpointNeedsScopes, ",") {
242 s = strings.TrimSpace(s)
243 if _, gotScope := gotScopes[s]; s == "" || gotScope {
244 continue
245 }
246 return fmt.Sprintf(
247 "This API operation needs the %[1]q scope. To request it, run: gh auth refresh -h %[2]s -s %[1]s",
248 s,
249 ghauth.NormalizeHostname(hostname),
250 )
251 }
252
253 return ""
254}
255
256func clientOptions(hostname string, transport http.RoundTripper) ghAPI.ClientOptions {
257 // 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