fix(worktree): name the missing forge credential when push fails
Push previously handed an empty token straight to go-git and let the push fail with a bare transport/auth error. When a repo is configured while the forge is disabled (missing credential), that failure surfaced late -- at push time -- with a message that didn't explain the cause. Push now checks whether the worktree's origin remote is HTTP(S) (the only transport that needs a token) before attempting the push, and fails immediately with a message naming the missing forge.token configuration. Local/file transports (used by the test double) are unaffected -- they never needed a token. Refs archie-core-3fb
This commit is contained in:
1 parent
4a22fcd340
commit
efdc652488
2 files changed
+63
No files matched your search
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in new issue
Block a user