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

Function GetScopes

pkg/cmd/auth/shared/oauth_scopes.go:35–57  ·  view source on GitHub ↗

GetScopes performs a GitHub API request and returns the value of the X-Oauth-Scopes header. The token is passed explicitly because this runs before the token is stored in config, so the transport has nothing to attach. The transport only sets Authorization when it is absent, so the header set here

(httpClient *http.Client, hostname, authToken string)

Source from the content-addressed store, hash-verified

33// the transport has nothing to attach. The transport only sets Authorization when it is
34// absent, so the header set here wins.
35func GetScopes(httpClient *http.Client, hostname, authToken string) (string, error) {
36 // TODO(api-client-rollout)
37 // This line of code is part of a mechanical roll out of the api client.
38 // As a follow up, consider whether the api client can be injected to this call site, rather than constructed
39 res, err := api.NewClientFromHTTP(httpClient).Request(hostname, http.MethodGet, safeurl.NewImmutableSafeURL("").String(), nil,
40 api.WithHeader("Authorization", "token "+authToken))
41 if err != nil {
42 return "", err
43 }
44
45 defer func() {
46 // Ensure the response body is fully read and closed
47 // before we reconnect, so that we reuse the same TCPconnection.
48 _, _ = io.Copy(io.Discard, res.Body)
49 res.Body.Close()
50 }()
51
52 if res.StatusCode != 200 {
53 return "", api.UnexpectedStatusError(res)
54 }
55
56 return res.Header.Get("X-Oauth-Scopes"), nil
57}
58
59// HasMinimumScopes performs a GitHub API request and returns an error if the token used in the request
60// lacks the minimum required scopes for performing API operations with gh.

Callers 3

refreshRunFunction · 0.92
buildEntryFunction · 0.92
HasMinimumScopesFunction · 0.85

Calls 9

NewClientFromHTTPFunction · 0.92
NewImmutableSafeURLFunction · 0.92
WithHeaderFunction · 0.92
UnexpectedStatusErrorFunction · 0.92
RequestMethod · 0.80
StringMethod · 0.65
CloseMethod · 0.65
GetMethod · 0.65
CopyMethod · 0.45

Tested by

no test coverage detected