diff --git a/github/github.go b/github/github.go index 4e0d2f47e8f..58715b11c6f 100644 --- a/github/github.go +++ b/github/github.go @@ -348,6 +348,11 @@ func NewClient(httpClient *http.Client) *Client { return c } +// NewClientWithEnvProxy enhances NewClient with the HttpProxy env. +func NewClientWithEnvProxy() *Client { + return NewClient(&http.Client{Transport: &http.Transport{Proxy: http.ProxyFromEnvironment}}) +} + // NewTokenClient returns a new GitHub API client authenticated with the provided token. func NewTokenClient(ctx context.Context, token string) *Client { return NewClient(oauth2.NewClient(ctx, oauth2.StaticTokenSource(&oauth2.Token{AccessToken: token}))) diff --git a/github/github_test.go b/github/github_test.go index ea86e86cd54..21a5e4a7cf5 100644 --- a/github/github_test.go +++ b/github/github_test.go @@ -254,6 +254,13 @@ func TestNewClient(t *testing.T) { } } +func TestNewClientWithEnvProxy(t *testing.T) { + client := NewClientWithEnvProxy() + if got, want := client.BaseURL.String(), defaultBaseURL; got != want { + t.Errorf("NewClient BaseURL is %v, want %v", got, want) + } +} + func TestClient(t *testing.T) { c := NewClient(nil) c2 := c.Client()