GetScopes performs a GitHub API request and returns the value of the X-Oauth-Scopes header.
(httpClient httpClient, hostname, authToken string)
| 33 | |
| 34 | // GetScopes performs a GitHub API request and returns the value of the X-Oauth-Scopes header. |
| 35 | func GetScopes(httpClient httpClient, hostname, authToken string) (string, error) { |
| 36 | apiEndpoint := ghinstance.RESTPrefix(hostname) |
| 37 | |
| 38 | req, err := http.NewRequest("GET", apiEndpoint, nil) |
| 39 | if err != nil { |
| 40 | return "", err |
| 41 | } |
| 42 | |
| 43 | req.Header.Set("Authorization", "token "+authToken) |
| 44 | |
| 45 | res, err := httpClient.Do(req) |
| 46 | if err != nil { |
| 47 | return "", err |
| 48 | } |
| 49 | |
| 50 | defer func() { |
| 51 | // Ensure the response body is fully read and closed |
| 52 | // before we reconnect, so that we reuse the same TCPconnection. |
| 53 | _, _ = io.Copy(io.Discard, res.Body) |
| 54 | res.Body.Close() |
| 55 | }() |
| 56 | |
| 57 | if res.StatusCode != 200 { |
| 58 | return "", api.HandleHTTPError(res) |
| 59 | } |
| 60 | |
| 61 | return res.Header.Get("X-Oauth-Scopes"), nil |
| 62 | } |
| 63 | |
| 64 | // HasMinimumScopes performs a GitHub API request and returns an error if the token used in the request |
| 65 | // lacks the minimum required scopes for performing API operations with gh. |
no test coverage detected