Skip to content

Commit 288d013

Browse files
committed
Respect the hostname of current repository in queries
1 parent ac1333a commit 288d013

File tree

23 files changed

+229
-276
lines changed

23 files changed

+229
-276
lines changed

api/client.go

Lines changed: 13 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -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 {
4344
func 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
}
@@ -238,14 +235,13 @@ func (c Client) HasScopes(wantedScopes ...string) (bool, string, error) {
238235
}
239236

240237
// GraphQL performs a GraphQL request and parses the response
241-
func (c Client) GraphQL(query string, variables map[string]interface{}, data interface{}) error {
242-
url := "https://api.github.com/graphql"
238+
func (c Client) GraphQL(hostname string, query string, variables map[string]interface{}, data interface{}) error {
243239
reqBody, err := json.Marshal(map[string]interface{}{"query": query, "variables": variables})
244240
if err != nil {
245241
return err
246242
}
247243

248-
req, err := http.NewRequest("POST", url, bytes.NewBuffer(reqBody))
244+
req, err := http.NewRequest("POST", ghinstance.GraphQLEndpoint(hostname), bytes.NewBuffer(reqBody))
249245
if err != nil {
250246
return err
251247
}
@@ -261,13 +257,13 @@ func (c Client) GraphQL(query string, variables map[string]interface{}, data int
261257
return handleResponse(resp, data)
262258
}
263259

264-
func graphQLClient(h *http.Client) *graphql.Client {
265-
return graphql.NewClient("https://api.github.com/graphql", h)
260+
func graphQLClient(h *http.Client, hostname string) *graphql.Client {
261+
return graphql.NewClient(ghinstance.GraphQLEndpoint(hostname), h)
266262
}
267263

268264
// REST performs a REST request and parses the response.
269-
func (c Client) REST(method string, p string, body io.Reader, data interface{}) error {
270-
url := "https://api.github.com/" + p
265+
func (c Client) REST(hostname string, method string, p string, body io.Reader, data interface{}) error {
266+
url := ghinstance.RESTPrefix(hostname) + p
271267
req, err := http.NewRequest(method, url, body)
272268
if err != nil {
273269
return err

api/client_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func TestGraphQL(t *testing.T) {
3333
}{}
3434

3535
http.StubResponse(200, bytes.NewBufferString(`{"data":{"viewer":{"login":"hubot"}}}`))
36-
err := client.GraphQL("QUERY", vars, &response)
36+
err := client.GraphQL("github.com", "QUERY", vars, &response)
3737
eq(t, err, nil)
3838
eq(t, response.Viewer.Login, "hubot")
3939

@@ -55,7 +55,7 @@ func TestGraphQLError(t *testing.T) {
5555
]
5656
}`))
5757

58-
err := client.GraphQL("", nil, &response)
58+
err := client.GraphQL("github.com", "", nil, &response)
5959
if err == nil || err.Error() != "GraphQL error: OH NO\nthis is fine" {
6060
t.Fatalf("got %q", err.Error())
6161
}
@@ -71,7 +71,7 @@ func TestRESTGetDelete(t *testing.T) {
7171
http.StubResponse(204, bytes.NewBuffer([]byte{}))
7272

7373
r := bytes.NewReader([]byte(`{}`))
74-
err := client.REST("DELETE", "applications/CLIENTID/grant", r, nil)
74+
err := client.REST("github.com", "DELETE", "applications/CLIENTID/grant", r, nil)
7575
eq(t, err, nil)
7676
}
7777

@@ -82,7 +82,7 @@ func TestRESTError(t *testing.T) {
8282
http.StubResponse(422, bytes.NewBufferString(`{"message": "OH NO"}`))
8383

8484
var httpErr HTTPError
85-
err := client.REST("DELETE", "repos/branch", nil, nil)
85+
err := client.REST("github.com", "DELETE", "repos/branch", nil, nil)
8686
if err == nil || !errors.As(err, &httpErr) {
8787
t.Fatalf("got %v", err)
8888
}

api/queries_gist.go

Lines changed: 0 additions & 52 deletions
This file was deleted.

api/queries_issue.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ func IssueCreate(client *Client, repo *Repository, params map[string]interface{}
112112
}
113113
}{}
114114

115-
err := client.GraphQL(query, variables, &result)
115+
err := client.GraphQL(repo.RepoHost(), query, variables, &result)
116116
if err != nil {
117117
return nil, err
118118
}
@@ -171,7 +171,7 @@ func IssueStatus(client *Client, repo ghrepo.Interface, currentUsername string)
171171
}
172172

173173
var resp response
174-
err := client.GraphQL(query, variables, &resp)
174+
err := client.GraphQL(repo.RepoHost(), query, variables, &resp)
175175
if err != nil {
176176
return nil, err
177177
}
@@ -270,7 +270,7 @@ func IssueList(client *Client, repo ghrepo.Interface, state string, labels []str
270270
loop:
271271
for {
272272
variables["limit"] = pageLimit
273-
err := client.GraphQL(query, variables, &response)
273+
err := client.GraphQL(repo.RepoHost(), query, variables, &response)
274274
if err != nil {
275275
return nil, err
276276
}
@@ -361,7 +361,7 @@ func IssueByNumber(client *Client, repo ghrepo.Interface, number int) (*Issue, e
361361
}
362362

363363
var resp response
364-
err := client.GraphQL(query, variables, &resp)
364+
err := client.GraphQL(repo.RepoHost(), query, variables, &resp)
365365
if err != nil {
366366
return nil, err
367367
}
@@ -389,7 +389,7 @@ func IssueClose(client *Client, repo ghrepo.Interface, issue Issue) error {
389389
},
390390
}
391391

392-
gql := graphQLClient(client.http)
392+
gql := graphQLClient(client.http, repo.RepoHost())
393393
err := gql.MutateNamed(context.Background(), "IssueClose", &mutation, variables)
394394

395395
if err != nil {
@@ -414,7 +414,7 @@ func IssueReopen(client *Client, repo ghrepo.Interface, issue Issue) error {
414414
},
415415
}
416416

417-
gql := graphQLClient(client.http)
417+
gql := graphQLClient(client.http, repo.RepoHost())
418418
err := gql.MutateNamed(context.Background(), "IssueReopen", &mutation, variables)
419419

420420
return err

api/queries_org.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import (
44
"context"
55
"fmt"
66

7+
"github.com/cli/cli/internal/ghinstance"
8+
"github.com/cli/cli/internal/ghrepo"
79
"github.com/shurcooL/githubv4"
810
)
911

@@ -12,7 +14,8 @@ func resolveOrganization(client *Client, orgName string) (string, error) {
1214
var response struct {
1315
NodeID string `json:"node_id"`
1416
}
15-
err := client.REST("GET", fmt.Sprintf("users/%s", orgName), nil, &response)
17+
// TODO: GHE support
18+
err := client.REST(ghinstance.Default(), "GET", fmt.Sprintf("users/%s", orgName), nil, &response)
1619
return response.NodeID, err
1720
}
1821

@@ -24,12 +27,13 @@ func resolveOrganizationTeam(client *Client, orgName, teamSlug string) (string,
2427
NodeID string `json:"node_id"`
2528
}
2629
}
27-
err := client.REST("GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
30+
// TODO: GHE support
31+
err := client.REST(ghinstance.Default(), "GET", fmt.Sprintf("orgs/%s/teams/%s", orgName, teamSlug), nil, &response)
2832
return response.Organization.NodeID, response.NodeID, err
2933
}
3034

3135
// OrganizationProjects fetches all open projects for an organization
32-
func OrganizationProjects(client *Client, owner string) ([]RepoProject, error) {
36+
func OrganizationProjects(client *Client, repo ghrepo.Interface) ([]RepoProject, error) {
3337
var query struct {
3438
Organization struct {
3539
Projects struct {
@@ -43,11 +47,11 @@ func OrganizationProjects(client *Client, owner string) ([]RepoProject, error) {
4347
}
4448

4549
variables := map[string]interface{}{
46-
"owner": githubv4.String(owner),
50+
"owner": githubv4.String(repo.RepoOwner()),
4751
"endCursor": (*githubv4.String)(nil),
4852
}
4953

50-
gql := graphQLClient(client.http)
54+
gql := graphQLClient(client.http, repo.RepoHost())
5155

5256
var projects []RepoProject
5357
for {
@@ -72,7 +76,7 @@ type OrgTeam struct {
7276
}
7377

7478
// OrganizationTeams fetches all the teams in an organization
75-
func OrganizationTeams(client *Client, owner string) ([]OrgTeam, error) {
79+
func OrganizationTeams(client *Client, repo ghrepo.Interface) ([]OrgTeam, error) {
7680
var query struct {
7781
Organization struct {
7882
Teams struct {
@@ -86,11 +90,11 @@ func OrganizationTeams(client *Client, owner string) ([]OrgTeam, error) {
8690
}
8791

8892
variables := map[string]interface{}{
89-
"owner": githubv4.String(owner),
93+
"owner": githubv4.String(repo.RepoOwner()),
9094
"endCursor": (*githubv4.String)(nil),
9195
}
9296

93-
gql := graphQLClient(client.http)
97+
gql := graphQLClient(client.http, repo.RepoHost())
9498

9599
var teams []OrgTeam
96100
for {

0 commit comments

Comments
 (0)