From 38b73e3f85c885cc54335fd0bb8a7bc72c7764e8 Mon Sep 17 00:00:00 2001 From: Sam Coe Date: Thu, 2 Nov 2023 13:21:03 +0100 Subject: [PATCH] Set host level git_protocol on login --- internal/config/auth_config_test.go | 26 ++++++++++++++++++++------ internal/config/config.go | 4 ++++ pkg/cmd/auth/login/login_test.go | 7 ++++++- 3 files changed, 30 insertions(+), 7 deletions(-) diff --git a/internal/config/auth_config_test.go b/internal/config/auth_config_test.go index 5fe67e877..78d6072c8 100644 --- a/internal/config/auth_config_test.go +++ b/internal/config/auth_config_test.go @@ -298,12 +298,19 @@ func TestLoginSetsGitProtocolForProvidedHost(t *testing.T) { _, err := authCfg.Login("github.com", "test-user", "test-token", "ssh", false) require.NoError(t, err) - // When we get the git protocol - protocol, err := authCfg.cfg.Get([]string{hostsKey, "github.com", usersKey, "test-user", gitProtocolKey}) + // When we get the host git protocol + hostProtocol, err := authCfg.cfg.Get([]string{hostsKey, "github.com", gitProtocolKey}) require.NoError(t, err) // Then it returns the git protocol we provided on login - require.Equal(t, "ssh", protocol) + require.Equal(t, "ssh", hostProtocol) + + // When we get the users git protocol + userProtocol, err := authCfg.cfg.Get([]string{hostsKey, "github.com", usersKey, "test-user", gitProtocolKey}) + require.NoError(t, err) + + // Then it returns the git protocol we provided on login + require.Equal(t, "ssh", userProtocol) } func TestLoginAddsHostIfNotAlreadyAdded(t *testing.T) { @@ -535,12 +542,19 @@ func TestLoginPostMigrationSetsGitProtocol(t *testing.T) { _, err := authCfg.Login("github.com", "test-user", "test-token", "ssh", false) require.NoError(t, err) - // When we get the git protocol - gitProtocol, err := authCfg.cfg.Get([]string{hostsKey, "github.com", usersKey, "test-user", gitProtocolKey}) + // When we get the host git protocol + hostProtocol, err := authCfg.cfg.Get([]string{hostsKey, "github.com", gitProtocolKey}) require.NoError(t, err) // Then it returns the git protocol we provided on login - require.Equal(t, "ssh", gitProtocol) + require.Equal(t, "ssh", hostProtocol) + + // When we get the user git protocol + userProtocol, err := authCfg.cfg.Get([]string{hostsKey, "github.com", usersKey, "test-user", gitProtocolKey}) + require.NoError(t, err) + + // Then it returns the git protocol we provided on login + require.Equal(t, "ssh", userProtocol) } func TestLoginPostMigrationSetsUser(t *testing.T) { diff --git a/internal/config/config.go b/internal/config/config.go index 172e4a903..3f9e9d4f8 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -322,6 +322,10 @@ func (c *AuthConfig) Login(hostname, username, token, gitProtocol string, secure c.cfg.Set([]string{hostsKey, hostname, userKey}, username) if gitProtocol != "" { + // Set the git protocol + c.cfg.Set([]string{hostsKey, hostname, gitProtocolKey}, gitProtocol) + // And set the git protocol under the user to support later auth switch + // and logout switch without another migration. c.cfg.Set([]string{hostsKey, hostname, usersKey, username, gitProtocolKey}, gitProtocol) } diff --git a/pkg/cmd/auth/login/login_test.go b/pkg/cmd/auth/login/login_test.go index fae3b526b..8fa5df58e 100644 --- a/pkg/cmd/auth/login/login_test.go +++ b/pkg/cmd/auth/login/login_test.go @@ -294,7 +294,7 @@ func Test_loginRun_nontty(t *testing.T) { httpmock.GraphQL(`query UserCurrent\b`), httpmock.StringResponse(`{"data":{"viewer":{"login":"monalisa"}}}`)) }, - wantHosts: "github.com:\n oauth_token: abc123\n users:\n monalisa:\n oauth_token: abc123\n git_protocol: https\n user: monalisa\n", + wantHosts: "github.com:\n oauth_token: abc123\n users:\n monalisa:\n oauth_token: abc123\n git_protocol: https\n user: monalisa\n git_protocol: https\n", }, { name: "with token and non-default host", @@ -507,6 +507,7 @@ func Test_loginRun_Survey(t *testing.T) { oauth_token: def456 git_protocol: https user: jillv + git_protocol: https `), prompterStubs: func(pm *prompter.PrompterMock) { pm.SelectFunc = func(prompt, _ string, opts []string) (int, error) { @@ -541,6 +542,7 @@ func Test_loginRun_Survey(t *testing.T) { oauth_token: def456 git_protocol: https user: jillv + git_protocol: https `), opts: &LoginOptions{ Interactive: true, @@ -584,6 +586,7 @@ func Test_loginRun_Survey(t *testing.T) { oauth_token: def456 git_protocol: https user: jillv + git_protocol: https `), opts: &LoginOptions{ Interactive: true, @@ -618,6 +621,7 @@ func Test_loginRun_Survey(t *testing.T) { oauth_token: def456 git_protocol: ssh user: jillv + git_protocol: ssh `), opts: &LoginOptions{ Interactive: true, @@ -662,6 +666,7 @@ func Test_loginRun_Survey(t *testing.T) { wantHosts: heredoc.Doc(` github.com: user: jillv + git_protocol: https users: jillv: git_protocol: https