TypeScript
fix(server): published repos fall back to HTTPS when SSH is refused (#30142)
Alex Southwell6ea23bd4264e
diff --git a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts b/apps/server/src/sourceControl/SourceControlRepositoryService.test.tsindex dedf3636df4..326b7931da2 100644--- a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts+++ b/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts@@ -648,6 +648,52 @@ it.effect("explains a rejected SSH push after creating the repository", () =>}).pipe(Effect.provide(layer({ git: failingPush("authentication_failed") }))),);+// regression: an auto-selected SSH remote kept failing every later fetch after a refused push+it.effect("switches an auto-selected remote to HTTPS when the SSH push is refused", () => {+ const gitArgs: Array<ReadonlyArray<string>> = [];+ let pushAttempts = 0;+ return Effect.gen(function* () {+ const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;+ const result = yield* service.publishRepository({+ cwd: "/workspace",+ provider: SourceControlProviderKind.make("github"),+ repository: "octocat/t3code",+ visibility: "private",+ });++ assert.strictEqual(result.status, "pushed");+ assert.strictEqual(result.remoteUrl, CLONE_URLS.url);+ assert.strictEqual(pushAttempts, 2);+ assert.deepStrictEqual(gitArgs, [+ ["rev-parse", "--verify", "HEAD"],+ ["remote", "set-url", "origin", CLONE_URLS.url],+ ]);+ }).pipe(+ Effect.provide(+ layer({+ git: {+ execute: (input) =>+ Effect.sync(() => {+ gitArgs.push(input.args);+ return processOutput();+ }),+ pushCurrentBranch: (cwd) => {+ pushAttempts += 1;+ return pushAttempts === 1+ ? failingPush("authentication_failed").pushCurrentBranch(cwd)+ : Effect.succeed({+ status: "pushed" as const,+ branch: "main",+ upstreamBranch: "origin/main",+ setUpstream: true,+ });+ },+ },+ }),+ ),+ );+});+// regression: #19219 publishing did not explain why the first push failedit.effect("says the repository exists when an unclassified publish push fails", () =>Effect.gen(function* () {diff --git a/apps/server/src/sourceControl/SourceControlRepositoryService.ts b/apps/server/src/sourceControl/SourceControlRepositoryService.tsindex 1e9c48893da..64242b72d4d 100644--- a/apps/server/src/sourceControl/SourceControlRepositoryService.ts+++ b/apps/server/src/sourceControl/SourceControlRepositoryService.ts@@ -502,28 +502,50 @@ export const make = Effect.gen(function* () {};}- const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName }).pipe(- Effect.mapError(- (cause) =>- new SourceControlRepositoryError({- operation: "publishRepository",- provider: providerKind,- detail: publishPushFailureDetail({- urls,- remoteName,- remoteUrl,- reason: cause.reason,+ const push = (pushUrl: string) =>+ git.pushCurrentBranch(input.cwd, null, { remoteName }).pipe(+ Effect.map((result) => ({ branch: result.branch, remoteUrl: pushUrl })),+ Effect.mapError(+ (cause) =>+ new SourceControlRepositoryError({+ operation: "publishRepository",+ provider: providerKind,+ detail: publishPushFailureDetail({+ urls,+ remoteName,+ remoteUrl: pushUrl,+ reason: cause.reason,+ }),+ cause,}),- cause,- }),+ ),+ );++ // Like clone, an auto-selected SSH remote switches to HTTPS when SSH is refused;+ // otherwise the remote keeps failing every later fetch and push.+ const canSwitchToHttps = input.protocol !== "ssh" && remoteUrl === urls.sshUrl;+ const pushed = yield* push(remoteUrl).pipe(+ Effect.catchIf(+ (error) => canSwitchToHttps && isSshRejection(error),+ (sshError) =>+ git+ .execute({+ operation: "SourceControlRepositoryService.publishRepository.switchToHttps",+ cwd: input.cwd,+ args: ["remote", "set-url", remoteName, urls.url],+ })+ .pipe(+ Effect.mapError(() => sshError),+ Effect.andThen(push(urls.url)),+ ),),);return {repository: toRepositoryInfo(providerKind, urls),remoteName,- remoteUrl,- branch: pushResult.branch,+ remoteUrl: pushed.remoteUrl,+ branch: pushed.branch,status: "pushed" as const,};},