Skip to content

Commit be2d0b9

Browse files
saphidclaude
andcommitted
fix(server): published repos fall back to HTTPS when SSH is refused
Publishing picks the SSH remote by default. When the SSH key is rejected, the push failed and the remote stayed SSH, so every later fetch failed too, including worktree setup. Like clone, publish now switches an auto-selected SSH remote to HTTPS and pushes again. Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent cefb544 commit be2d0b9

2 files changed

Lines changed: 83 additions & 15 deletions

File tree

‎apps/server/src/sourceControl/SourceControlRepositoryService.test.ts‎

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,52 @@ it.effect("explains a rejected SSH push after creating the repository", () =>
648648
}).pipe(Effect.provide(layer({ git: failingPush("authentication_failed") }))),
649649
);
650650

651+
// regression: an auto-selected SSH remote kept failing every later fetch after a refused push
652+
it.effect("switches an auto-selected remote to HTTPS when the SSH push is refused", () => {
653+
const gitArgs: Array<ReadonlyArray<string>> = [];
654+
let pushAttempts = 0;
655+
return Effect.gen(function* () {
656+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
657+
const result = yield* service.publishRepository({
658+
cwd: "/workspace",
659+
provider: SourceControlProviderKind.make("github"),
660+
repository: "octocat/t3code",
661+
visibility: "private",
662+
});
663+
664+
assert.strictEqual(result.status, "pushed");
665+
assert.strictEqual(result.remoteUrl, CLONE_URLS.url);
666+
assert.strictEqual(pushAttempts, 2);
667+
assert.deepStrictEqual(gitArgs, [
668+
["rev-parse", "--verify", "HEAD"],
669+
["remote", "set-url", "origin", CLONE_URLS.url],
670+
]);
671+
}).pipe(
672+
Effect.provide(
673+
layer({
674+
git: {
675+
execute: (input) =>
676+
Effect.sync(() => {
677+
gitArgs.push(input.args);
678+
return processOutput();
679+
}),
680+
pushCurrentBranch: (cwd) => {
681+
pushAttempts += 1;
682+
return pushAttempts === 1
683+
? failingPush("authentication_failed").pushCurrentBranch(cwd)
684+
: Effect.succeed({
685+
status: "pushed" as const,
686+
branch: "main",
687+
upstreamBranch: "origin/main",
688+
setUpstream: true,
689+
});
690+
},
691+
},
692+
}),
693+
),
694+
);
695+
});
696+
651697
// regression: #19219 publishing did not explain why the first push failed
652698
it.effect("says the repository exists when an unclassified publish push fails", () =>
653699
Effect.gen(function* () {

‎apps/server/src/sourceControl/SourceControlRepositoryService.ts‎

Lines changed: 37 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -502,28 +502,50 @@ export const make = Effect.gen(function* () {
502502
};
503503
}
504504

505-
const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName }).pipe(
506-
Effect.mapError(
507-
(cause) =>
508-
new SourceControlRepositoryError({
509-
operation: "publishRepository",
510-
provider: providerKind,
511-
detail: publishPushFailureDetail({
512-
urls,
513-
remoteName,
514-
remoteUrl,
515-
reason: cause.reason,
505+
const push = (pushUrl: string) =>
506+
git.pushCurrentBranch(input.cwd, null, { remoteName }).pipe(
507+
Effect.map((result) => ({ branch: result.branch, remoteUrl: pushUrl })),
508+
Effect.mapError(
509+
(cause) =>
510+
new SourceControlRepositoryError({
511+
operation: "publishRepository",
512+
provider: providerKind,
513+
detail: publishPushFailureDetail({
514+
urls,
515+
remoteName,
516+
remoteUrl: pushUrl,
517+
reason: cause.reason,
518+
}),
519+
cause,
516520
}),
517-
cause,
518-
}),
521+
),
522+
);
523+
524+
// Like clone, an auto-selected SSH remote switches to HTTPS when SSH is refused;
525+
// otherwise the remote keeps failing every later fetch and push.
526+
const canSwitchToHttps = input.protocol !== "ssh" && remoteUrl === urls.sshUrl;
527+
const pushed = yield* push(remoteUrl).pipe(
528+
Effect.catchIf(
529+
(error) => canSwitchToHttps && isSshRejection(error),
530+
(sshError) =>
531+
git
532+
.execute({
533+
operation: "SourceControlRepositoryService.publishRepository.switchToHttps",
534+
cwd: input.cwd,
535+
args: ["remote", "set-url", remoteName, urls.url],
536+
})
537+
.pipe(
538+
Effect.mapError(() => sshError),
539+
Effect.andThen(push(urls.url)),
540+
),
519541
),
520542
);
521543

522544
return {
523545
repository: toRepositoryInfo(providerKind, urls),
524546
remoteName,
525-
remoteUrl,
526-
branch: pushResult.branch,
547+
remoteUrl: pushed.remoteUrl,
548+
branch: pushed.branch,
527549
status: "pushed" as const,
528550
};
529551
},

0 commit comments

Comments
 (0)