1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
|
package api
import (
"context"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"github.com/cli/cli/v2/internal/ghinstance"
ghAPI "github.com/cli/go-gh/v2/pkg/api"
)
const (
accept = "Accept"
authorization = "Authorization"
cacheTTL = "X-GH-CACHE-TTL"
graphqlFeatures = "GraphQL-Features"
features = "merge_queue"
userAgent = "User-Agent"
)
var linkRE = regexp.MustCompile(`<([^>]+)>;\s*rel="([^"]+)"`)
func NewClientFromHTTP(httpClient *http.Client) *Client {
client := &Client{http: httpClient}
return client
}
type Client struct {
http *http.Client
}
func (c *Client) HTTP() *http.Client {
return c.http
}
type GraphQLError struct {
*ghAPI.GraphQLError
}
type HTTPError struct {
*ghAPI.HTTPError
scopesSuggestion string
}
func (err HTTPError) ScopesSuggestion() string {
return err.scopesSuggestion
}
// GraphQL performs a GraphQL request using the query string and parses the response into data receiver. If there are errors in the response,
// GraphQLError will be returned, but the receiver will also be partially populated.
func (c Client) GraphQL(hostname string, query string, variables map[string]interface{}, data interface{}) error {
opts := clientOptions(hostname, c.http.Transport)
opts.Headers[graphqlFeatures] = features
gqlClient, err := ghAPI.NewGraphQLClient(opts)
if err != nil {
return err
}
return handleResponse(gqlClient.Do(query, variables, data))
}
// Mutate performs a GraphQL mutation based on a struct and parses the response with the same struct as the receiver. If there are errors in the response,
// GraphQLError will be returned, but the receiver will also be partially populated.
func (c Client) Mutate(hostname, name string, mutation interface{}, variables map[string]interface{}) error {
opts := clientOptions(hostname, c.http.Transport)
opts.Headers[graphqlFeatures] = features
gqlClient, err := ghAPI.NewGraphQLClient(opts)
if err != nil {
return err
}
return handleResponse(gqlClient.Mutate(name, mutation, variables))
}
// Query performs a GraphQL query based on a struct and parses the response with the same struct as the receiver. If there are errors in the response,
// GraphQLError will be returned, but the receiver will also be partially populated.
func (c Client) Query(hostname, name string, query interface{}, variables map[string]interface{}) error {
opts := clientOptions(hostname, c.http.Transport)
opts.Headers[graphqlFeatures] = features
gqlClient, err := ghAPI.NewGraphQLClient(opts)
if err != nil {
return err
}
return handleResponse(gqlClient.Query(name, query, variables))
}
// QueryWithContext performs a GraphQL query based on a struct and parses the response with the same struct as the receiver. If there are errors in the response,
// GraphQLError will be returned, but the receiver will also be partially populated.
func (c Client) QueryWithContext(ctx context.Context, hostname, name string, query interface{}, variables map[string]interface{}) error {
opts := clientOptions(hostname, c.http.Transport)
opts.Headers[graphqlFeatures] = features
gqlClient, err := ghAPI.NewGraphQLClient(opts)
if err != nil {
return err
}
return handleResponse(gqlClient.QueryWithContext(ctx, name, query, variables))
}
// REST performs a REST request and parses the response.
func (c Client) REST(hostname string, method string, p string, body io.Reader, data interface{}) error {
opts := clientOptions(hostname, c.http.Transport)
restClient, err := ghAPI.NewRESTClient(opts)
if err != nil {
return err
}
return handleResponse(restClient.Do(method, p, body, data))
}
func (c Client) RESTWithNext(hostname string, method string, p string, body io.Reader, data interface{}) (string, error) {
opts := clientOptions(hostname, c.http.Transport)
restClient, err := ghAPI.NewRESTClient(opts)
if err != nil {
return "", err
}
resp, err := restClient.Request(method, p, body)
if err != nil {
return "", err
}
defer resp.Body.Close()
success := resp.StatusCode >= 200 && resp.StatusCode < 300
if !success {
return "", HandleHTTPError(resp)
}
if resp.StatusCode == http.StatusNoContent {
return "", nil
}
b, err := io.ReadAll(resp.Body)
if err != nil {
return "", err
}
err = json.Unmarshal(b, &data)
if err != nil {
return "", err
}
var next string
for _, m := range linkRE.FindAllStringSubmatch(resp.Header.Get("Link"), -1) {
if len(m) > 2 && m[2] == "next" {
next = m[1]
}
}
return next, nil
}
// HandleHTTPError parses a http.Response into a HTTPError.
func HandleHTTPError(resp *http.Response) error {
return handleResponse(ghAPI.HandleHTTPError(resp))
}
// handleResponse takes a ghAPI.HTTPError or ghAPI.GraphQLError and converts it into an
// HTTPError or GraphQLError respectively.
func handleResponse(err error) error {
if err == nil {
return nil
}
var restErr *ghAPI.HTTPError
if errors.As(err, &restErr) {
return HTTPError{
HTTPError: restErr,
scopesSuggestion: generateScopesSuggestion(restErr.StatusCode,
restErr.Headers.Get("X-Accepted-Oauth-Scopes"),
restErr.Headers.Get("X-Oauth-Scopes"),
restErr.RequestURL.Hostname()),
}
}
var gqlErr *ghAPI.GraphQLError
if errors.As(err, &gqlErr) {
return GraphQLError{
GraphQLError: gqlErr,
}
}
return err
}
// ScopesSuggestion is an error messaging utility that prints the suggestion to request additional OAuth
// scopes in case a server response indicates that there are missing scopes.
func ScopesSuggestion(resp *http.Response) string {
return generateScopesSuggestion(resp.StatusCode,
resp.Header.Get("X-Accepted-Oauth-Scopes"),
resp.Header.Get("X-Oauth-Scopes"),
resp.Request.URL.Hostname())
}
// EndpointNeedsScopes adds additional OAuth scopes to an HTTP response as if they were returned from the
// server endpoint. This improves HTTP 4xx error messaging for endpoints that don't explicitly list the
// OAuth scopes they need.
func EndpointNeedsScopes(resp *http.Response, s string) *http.Response {
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
oldScopes := resp.Header.Get("X-Accepted-Oauth-Scopes")
resp.Header.Set("X-Accepted-Oauth-Scopes", fmt.Sprintf("%s, %s", oldScopes, s))
}
return resp
}
func generateScopesSuggestion(statusCode int, endpointNeedsScopes, tokenHasScopes, hostname string) string {
if statusCode < 400 || statusCode > 499 || statusCode == 422 {
return ""
}
if tokenHasScopes == "" {
return ""
}
gotScopes := map[string]struct{}{}
for _, s := range strings.Split(tokenHasScopes, ",") {
s = strings.TrimSpace(s)
gotScopes[s] = struct{}{}
// Certain scopes may be grouped under a single "top-level" scope. The following branch
// statements include these grouped/implied scopes when the top-level scope is encountered.
// See https://docs.github.com/en/developers/apps/building-oauth-apps/scopes-for-oauth-apps.
if s == "repo" {
gotScopes["repo:status"] = struct{}{}
gotScopes["repo_deployment"] = struct{}{}
gotScopes["public_repo"] = struct{}{}
gotScopes["repo:invite"] = struct{}{}
gotScopes["security_events"] = struct{}{}
} else if s == "user" {
gotScopes["read:user"] = struct{}{}
gotScopes["user:email"] = struct{}{}
gotScopes["user:follow"] = struct{}{}
} else if s == "codespace" {
gotScopes["codespace:secrets"] = struct{}{}
} else if strings.HasPrefix(s, "admin:") {
gotScopes["read:"+strings.TrimPrefix(s, "admin:")] = struct{}{}
gotScopes["write:"+strings.TrimPrefix(s, "admin:")] = struct{}{}
} else if strings.HasPrefix(s, "write:") {
gotScopes["read:"+strings.TrimPrefix(s, "write:")] = struct{}{}
}
}
for _, s := range strings.Split(endpointNeedsScopes, ",") {
s = strings.TrimSpace(s)
if _, gotScope := gotScopes[s]; s == "" || gotScope {
continue
}
return fmt.Sprintf(
"This API operation needs the %[1]q scope. To request it, run: gh auth refresh -h %[2]s -s %[1]s",
s,
ghinstance.NormalizeHostname(hostname),
)
}
return ""
}
func clientOptions(hostname string, transport http.RoundTripper) ghAPI.ClientOptions {
// AuthToken, and Headers are being handled by transport,
// so let go-gh know that it does not need to resolve them.
opts := ghAPI.ClientOptions{
AuthToken: "none",
Headers: map[string]string{
authorization: "",
},
Host: hostname,
SkipDefaultHeaders: true,
Transport: transport,
LogIgnoreEnv: true,
}
return opts
}
|