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)
| 33 | // the transport has nothing to attach. The transport only sets Authorization when it is |
| 34 | // absent, so the header set here wins. |
| 35 | func 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. |
no test coverage detected