@@ -11,6 +11,7 @@ import (
1111 "regexp"
1212 "strings"
1313
14+ "github.com/cli/cli/internal/ghinstance"
1415 "github.com/henvic/httpretty"
1516 "github.com/shurcooL/graphql"
1617)
@@ -43,25 +44,21 @@ func NewClientFromHTTP(httpClient *http.Client) *Client {
4344func AddHeader (name , value string ) ClientOption {
4445 return func (tr http.RoundTripper ) http.RoundTripper {
4546 return & funcTripper {roundTrip : func (req * http.Request ) (* http.Response , error ) {
46- // prevent the token from leaking to non-GitHub hosts
47- // TODO: GHE support
48- if ! strings .EqualFold (name , "Authorization" ) || strings .HasSuffix (req .URL .Hostname (), ".github.com" ) {
49- req .Header .Add (name , value )
50- }
47+ req .Header .Add (name , value )
5148 return tr .RoundTrip (req )
5249 }}
5350 }
5451}
5552
5653// AddHeaderFunc is an AddHeader that gets the string value from a function
57- func AddHeaderFunc (name string , value func () string ) ClientOption {
54+ func AddHeaderFunc (name string , getValue func (* http. Request ) ( string , error ) ) ClientOption {
5855 return func (tr http.RoundTripper ) http.RoundTripper {
5956 return & funcTripper {roundTrip : func (req * http.Request ) (* http.Response , error ) {
60- // prevent the token from leaking to non-GitHub hosts
61- // TODO: GHE support
62- if ! strings .EqualFold (name , "Authorization" ) || strings .HasSuffix (req .URL .Hostname (), ".github.com" ) {
63- req .Header .Add (name , value ())
57+ value , err := getValue (req )
58+ if err != nil {
59+ return nil , err
6460 }
61+ req .Header .Add (name , value )
6562 return tr .RoundTrip (req )
6663 }}
6764 }
@@ -244,14 +241,13 @@ func (c Client) HasScopes(wantedScopes ...string) (bool, string, error) {
244241}
245242
246243// GraphQL performs a GraphQL request and parses the response
247- func (c Client ) GraphQL (query string , variables map [string ]interface {}, data interface {}) error {
248- url := "https://api.github.com/graphql"
244+ func (c Client ) GraphQL (hostname string , query string , variables map [string ]interface {}, data interface {}) error {
249245 reqBody , err := json .Marshal (map [string ]interface {}{"query" : query , "variables" : variables })
250246 if err != nil {
251247 return err
252248 }
253249
254- req , err := http .NewRequest ("POST" , url , bytes .NewBuffer (reqBody ))
250+ req , err := http .NewRequest ("POST" , ghinstance . GraphQLEndpoint ( hostname ) , bytes .NewBuffer (reqBody ))
255251 if err != nil {
256252 return err
257253 }
@@ -267,13 +263,13 @@ func (c Client) GraphQL(query string, variables map[string]interface{}, data int
267263 return handleResponse (resp , data )
268264}
269265
270- func graphQLClient (h * http.Client ) * graphql.Client {
271- return graphql .NewClient ("https://api.github.com/graphql" , h )
266+ func graphQLClient (h * http.Client , hostname string ) * graphql.Client {
267+ return graphql .NewClient (ghinstance . GraphQLEndpoint ( hostname ) , h )
272268}
273269
274270// REST performs a REST request and parses the response.
275- func (c Client ) REST (method string , p string , body io.Reader , data interface {}) error {
276- url := "https://api.github.com/" + p
271+ func (c Client ) REST (hostname string , method string , p string , body io.Reader , data interface {}) error {
272+ url := ghinstance . RESTPrefix ( hostname ) + p
277273 req , err := http .NewRequest (method , url , body )
278274 if err != nil {
279275 return err
0 commit comments