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