diff --git a/internal/app/alias_completion_test.go b/internal/app/alias_completion_test.go index 51fcd260..ca5ac10a 100644 --- a/internal/app/alias_completion_test.go +++ b/internal/app/alias_completion_test.go @@ -328,3 +328,14 @@ func TestCompletionInstallHelp(t *testing.T) { require.NoError(t, Execute(context.Background(), deps, []string{"completion", "bash", "--help"})) require.Contains(t, out.String(), "$ eval \"$(tea completion bash)\"\n") } + +func TestCompletionTargetAcceptsABareTokenHost(t *testing.T) { + deps, _, _ := testDeps(t) + instance := "http://127.0.0.1:3999" + require.NoError(t, testManager(t, deps).Register(instance, "alice", "file", "token", auth.HostOptions{}, auth.Credential{AccessToken: "stored-token"})) + deps.Getenv = envFunc(map[string]string{"TEA_HOST": "127.0.0.1:3999", "GITEA_TOKEN": "environment-token"}) + a := &application{deps: deps} + resolved, err := a.completionTarget(context.Background(), false) + require.NoError(t, err) + require.Equal(t, instance, resolved.Host) +} diff --git a/internal/app/completion.go b/internal/app/completion.go index 873c2c5f..b1bd0d58 100644 --- a/internal/app/completion.go +++ b/internal/app/completion.go @@ -303,7 +303,7 @@ func (a *application) completionTarget(ctx context.Context, requireRepo bool) (t var signer *auth.HTTPSign token := a.deps.Getenv("GITEA_TOKEN") if token != "" { - if instance, err := settings.NormalizeHost(a.tokenInstance()); err != nil || instance != t.Host { + if instance, err := settings.ResolveHost(a.tokenInstance(), t.hosts()); err != nil || instance != t.Host { return t, errors.New("GITEA_TOKEN belongs to another instance") } } else { diff --git a/internal/app/git_credentials.go b/internal/app/git_credentials.go index 53ef5697..51c1dcc7 100644 --- a/internal/app/git_credentials.go +++ b/internal/app/git_credentials.go @@ -40,6 +40,9 @@ func (a *application) gitCredentialCommand() *cobra.Command { return err } environmentHost := a.tokenInstance() + if resolved, err := settings.ResolveHost(environmentHost, snapshot.Hosts.Hosts); environmentHost != "" && err == nil { + environmentHost = resolved + } instance, err := auth.MatchGitInstance(request, snapshot.Hosts.Hosts, a.options.host, environmentHost) if err != nil { return clierr.Wrap(clierr.Usage, err) @@ -64,11 +67,7 @@ func (a *application) gitCredentialCommand() *cobra.Command { } credential := auth.Credential{} token := a.deps.Getenv("GITEA_TOKEN") - environmentMatches := false - if environmentHost != "" { - normalized, err := settings.NormalizeHost(environmentHost) - environmentMatches = err == nil && normalized == instance - } + environmentMatches := environmentHost != "" && environmentHost == instance if token != "" && environmentMatches { actual, err := services.CurrentAccount(cmd.Context(), a.deps.Client(instance, token, host.Insecure)) if err != nil { diff --git a/internal/app/git_credentials_test.go b/internal/app/git_credentials_test.go index 412d0e19..3ebd8549 100644 --- a/internal/app/git_credentials_test.go +++ b/internal/app/git_credentials_test.go @@ -84,3 +84,15 @@ func TestGitCredentialEnvironmentTokenIsScopedAndNeverPersisted(t *testing.T) { require.Empty(t, out.String()) } } + +func TestGitCredentialEnvironmentTokenAcceptsABareHost(t *testing.T) { + server := newAuthenticatedServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { t.Fatalf("unexpected request %s", r.URL.Path) })) + defer server.Close() + deps, out, _ := testDeps(t) + require.NoError(t, testManager(t, deps).Register(server.URL, "alice", "file", "token", auth.HostOptions{}, auth.Credential{AccessToken: "stored-token"})) + host := strings.TrimPrefix(server.URL, "http://") + deps.Getenv = envFunc(map[string]string{"TEA_HOST": host, "GITEA_TOKEN": "environment-token"}) + deps.Streams.In = strings.NewReader("protocol=http\nhost=" + host + "\npath=alice/project.git\n\n") + require.NoError(t, Execute(context.Background(), deps, []string{"auth", "git-credential", "get"})) + require.Equal(t, "username=alice\npassword=environment-token\n\n", out.String()) +}