From 5685b9a443f0d32f200a3796a8cecb30e85e7aa0 Mon Sep 17 00:00:00 2001 From: "Babak K. Shandiz" Date: Mon, 13 Oct 2025 16:47:38 +0100 Subject: [PATCH] refactor(auth refresh): use `PlainHttpClient` instead of zero `http.Client` Signed-off-by: Babak K. Shandiz --- pkg/cmd/auth/refresh/refresh.go | 31 ++++++++++++++++------------ pkg/cmd/auth/refresh/refresh_test.go | 6 ++++-- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/pkg/cmd/auth/refresh/refresh.go b/pkg/cmd/auth/refresh/refresh.go index 6cd49a9f8..c025df465 100644 --- a/pkg/cmd/auth/refresh/refresh.go +++ b/pkg/cmd/auth/refresh/refresh.go @@ -21,11 +21,11 @@ type token string type username string type RefreshOptions struct { - IO *iostreams.IOStreams - Config func() (gh.Config, error) - HttpClient *http.Client - GitClient *git.Client - Prompter shared.Prompt + IO *iostreams.IOStreams + Config func() (gh.Config, error) + PlainHttpClient func() (*http.Client, error) + GitClient *git.Client + Prompter shared.Prompt MainExecutable string @@ -33,7 +33,7 @@ type RefreshOptions struct { Scopes []string RemoveScopes []string ResetScopes bool - AuthFlow func(*iostreams.IOStreams, string, []string, bool, bool) (token, username, error) + AuthFlow func(*http.Client, *iostreams.IOStreams, string, []string, bool, bool) (token, username, error) Interactive bool InsecureStorage bool @@ -44,13 +44,13 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra. opts := &RefreshOptions{ IO: f.IOStreams, Config: f.Config, - AuthFlow: func(io *iostreams.IOStreams, hostname string, scopes []string, interactive bool, clipboard bool) (token, username, error) { - t, u, err := authflow.AuthFlow(hostname, io, "", scopes, interactive, f.Browser, clipboard) + AuthFlow: func(httpClient *http.Client, io *iostreams.IOStreams, hostname string, scopes []string, interactive bool, clipboard bool) (token, username, error) { + t, u, err := authflow.AuthFlow(httpClient, hostname, io, "", scopes, interactive, f.Browser, clipboard) return token(t), username(u), err }, - HttpClient: &http.Client{}, - GitClient: f.GitClient, - Prompter: f.Prompter, + PlainHttpClient: f.PlainHttpClient, + GitClient: f.GitClient, + Prompter: f.Prompter, } cmd := &cobra.Command{ @@ -125,6 +125,11 @@ func NewCmdRefresh(f *cmdutil.Factory, runF func(*RefreshOptions) error) *cobra. } func refreshRun(opts *RefreshOptions) error { + plainHTTPClient, err := opts.PlainHttpClient() + if err != nil { + return err + } + cfg, err := opts.Config() if err != nil { return err @@ -171,7 +176,7 @@ func refreshRun(opts *RefreshOptions) error { if !opts.ResetScopes { if oldToken, _ := authCfg.ActiveToken(hostname); oldToken != "" { - if oldScopes, err := shared.GetScopes(opts.HttpClient, hostname, oldToken); err == nil { + if oldScopes, err := shared.GetScopes(plainHTTPClient, hostname, oldToken); err == nil { for _, s := range strings.Split(oldScopes, ",") { s = strings.TrimSpace(s) if s != "" { @@ -204,7 +209,7 @@ func refreshRun(opts *RefreshOptions) error { additionalScopes.RemoveValues(opts.RemoveScopes) - authedToken, authedUser, err := opts.AuthFlow(opts.IO, hostname, additionalScopes.ToSlice(), opts.Interactive, opts.Clipboard) + authedToken, authedUser, err := opts.AuthFlow(plainHTTPClient, opts.IO, hostname, additionalScopes.ToSlice(), opts.Interactive, opts.Clipboard) if err != nil { return err } diff --git a/pkg/cmd/auth/refresh/refresh_test.go b/pkg/cmd/auth/refresh/refresh_test.go index bcabef228..120f6efec 100644 --- a/pkg/cmd/auth/refresh/refresh_test.go +++ b/pkg/cmd/auth/refresh/refresh_test.go @@ -471,7 +471,7 @@ func Test_refreshRun(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { aa := authArgs{} - tt.opts.AuthFlow = func(_ *iostreams.IOStreams, hostname string, scopes []string, interactive bool, clipboard bool) (token, username, error) { + tt.opts.AuthFlow = func(_ *http.Client, _ *iostreams.IOStreams, hostname string, scopes []string, interactive bool, clipboard bool) (token, username, error) { aa.hostname = hostname aa.scopes = scopes aa.interactive = interactive @@ -514,7 +514,9 @@ func Test_refreshRun(t *testing.T) { }, nil }, ) - tt.opts.HttpClient = &http.Client{Transport: httpReg} + tt.opts.PlainHttpClient = func() (*http.Client, error) { + return &http.Client{Transport: httpReg}, nil + } pm := &prompter.PrompterMock{} if tt.prompterStubs != nil {