Skip to content

Commit a73584d

Browse files
committed
Merge remote-tracking branch 'origin' into pr-commands-isolate
2 parents 7949d53 + 2ab06ab commit a73584d

File tree

32 files changed

+387
-293
lines changed

32 files changed

+387
-293
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
}
@@ -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

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: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package api
33
import (
44
"context"
55

6+
"github.com/cli/cli/internal/ghrepo"
67
"github.com/shurcooL/githubv4"
78
)
89

910
// OrganizationProjects fetches all open projects for an organization
10-
func OrganizationProjects(client *Client, owner string) ([]RepoProject, error) {
11+
func OrganizationProjects(client *Client, repo ghrepo.Interface) ([]RepoProject, error) {
1112
var query struct {
1213
Organization struct {
1314
Projects struct {
@@ -21,11 +22,11 @@ func OrganizationProjects(client *Client, owner string) ([]RepoProject, error) {
2122
}
2223

2324
variables := map[string]interface{}{
24-
"owner": githubv4.String(owner),
25+
"owner": githubv4.String(repo.RepoOwner()),
2526
"endCursor": (*githubv4.String)(nil),
2627
}
2728

28-
gql := graphQLClient(client.http)
29+
gql := graphQLClient(client.http, repo.RepoHost())
2930

3031
var projects []RepoProject
3132
for {
@@ -50,7 +51,7 @@ type OrgTeam struct {
5051
}
5152

5253
// OrganizationTeams fetches all the teams in an organization
53-
func OrganizationTeams(client *Client, owner string) ([]OrgTeam, error) {
54+
func OrganizationTeams(client *Client, repo ghrepo.Interface) ([]OrgTeam, error) {
5455
var query struct {
5556
Organization struct {
5657
Teams struct {
@@ -64,11 +65,11 @@ func OrganizationTeams(client *Client, owner string) ([]OrgTeam, error) {
6465
}
6566

6667
variables := map[string]interface{}{
67-
"owner": githubv4.String(owner),
68+
"owner": githubv4.String(repo.RepoOwner()),
6869
"endCursor": (*githubv4.String)(nil),
6970
}
7071

71-
gql := graphQLClient(client.http)
72+
gql := graphQLClient(client.http, repo.RepoHost())
7273

7374
var teams []OrgTeam
7475
for {

0 commit comments

Comments
 (0)