diff --git a/internal/worktree/worktree.go b/internal/worktree/worktree.go index b687a85bb..24f348ca1 100644 --- a/internal/worktree/worktree.go +++ b/internal/worktree/worktree.go @@ -251,6 +251,10 @@ func (m *Manager) Push(ctx context.Context, dir, branch string) error { if err != nil { return fmt.Errorf("open worktree: %w", err) } + if m.Token == "" && remoteUsesHTTP(r) { + return fmt.Errorf("push %s: no forge credential configured; "+ + "set [forge].token (or the identity's forge.token) so archied can authenticate", branch) + } ref := plumbing.NewBranchReferenceName(branch) spec := gitconfig.RefSpec(fmt.Sprintf("%s:%s", ref, ref)) if err := r.PushContext(ctx, &git.PushOptions{ @@ -280,6 +284,25 @@ func (m *Manager) Push(ctx context.Context, dir, branch string) error { return nil } +// remoteUsesHTTP reports whether the repository's origin remote is +// configured over HTTP(S), the only transport that requires a token. +func remoteUsesHTTP(r *git.Repository) bool { + cfg, err := r.Config() + if err != nil { + return false + } + remote, ok := cfg.Remotes[git.DefaultRemoteName] + if !ok { + return false + } + for _, u := range remote.URLs { + if strings.HasPrefix(u, "http://") || strings.HasPrefix(u, "https://") { + return true + } + } + return false +} + // patch computes the diff of the task branch against base, from their // merge base -- the three-dot semantics of `git diff origin/base...HEAD`. // Diffing against the remote tip instead would attribute every commit diff --git a/internal/worktree/worktree_test.go b/internal/worktree/worktree_test.go index 3b7f824e7..bb1e29770 100644 --- a/internal/worktree/worktree_test.go +++ b/internal/worktree/worktree_test.go @@ -418,6 +418,46 @@ func TestCloneURL(t *testing.T) { } } +// A missing forge credential over an HTTP(S) remote must fail with a +// message naming the missing token as the cause, not a bare git transport +// error discovered mid-push. Local (non-HTTP) remotes -- the test double +// used throughout this file -- are unaffected: they never need a token. +func TestPushWithoutTokenOverHTTPFailsWithClearMessage(t *testing.T) { + ctx := context.Background() + host := newLocalRemote(t, "acme", "todo") + seeder := newManager(t, host) + dir, branch, err := seeder.Prepare(ctx, "acme", "todo", testBase, 1, "t", "", "") + if err != nil { + t.Fatalf("Prepare() error = %v", err) + } + + // Point the already-cloned worktree's origin at an HTTPS remote, as if + // it had been cloned against a real forge rather than the local test + // double, then push with no token configured. + r, err := git.PlainOpen(dir) + if err != nil { + t.Fatalf("PlainOpen: %v", err) + } + if err := r.DeleteRemote(git.DefaultRemoteName); err != nil { + t.Fatalf("DeleteRemote: %v", err) + } + if _, err := r.CreateRemote(&gitconfig.RemoteConfig{ + Name: git.DefaultRemoteName, + URLs: []string{"https://forge.example.invalid/acme/todo.git"}, + }); err != nil { + t.Fatalf("CreateRemote: %v", err) + } + + m := &Manager{WorkDir: seeder.WorkDir, BotUser: "archie-bot"} + err = m.Push(ctx, dir, branch) + if err == nil { + t.Fatal("Push() error = nil, want a failure: no token is configured") + } + if !strings.Contains(err.Error(), "forge") && !strings.Contains(err.Error(), "token") { + t.Errorf("Push() error = %q, want it to name the missing forge credential as the cause", err.Error()) + } +} + func TestAuthOmittedWithoutToken(t *testing.T) { if opts := (&Manager{BotUser: "archie-bot"}).auth(); opts != nil { t.Errorf("auth() = %v with no token, want nil for an anonymous request", opts)