Skip to content

Commit a61d324

Browse files
committed
fix(server): publishing explains why the first push failed
1 parent dff750b commit a61d324

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -638,6 +638,63 @@ it.effect("publishes to the remote name returned by ensureRemote", () => {
638638
);
639639
});
640640

641+
function failingPush(reason?: GitCommandError["reason"]) {
642+
return {
643+
pushCurrentBranch: (cwd: string) =>
644+
Effect.fail(
645+
new GitCommandError({
646+
operation: "GitVcsDriver.pushCurrentBranch.pushWithRequestedRemote",
647+
command: "git",
648+
cwd,
649+
...(reason === undefined ? {} : { reason }),
650+
detail: "Git command exited with a non-zero status.",
651+
}),
652+
),
653+
} satisfies Partial<GitVcsDriver.GitVcsDriver["Service"]>;
654+
}
655+
656+
it.effect("explains a rejected SSH push after creating the repository", () =>
657+
Effect.gen(function* () {
658+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
659+
const error = yield* Effect.flip(
660+
service.publishRepository({
661+
cwd: "/workspace",
662+
provider: SourceControlProviderKind.make("github"),
663+
repository: "octocat/t3code",
664+
visibility: "private",
665+
protocol: "ssh",
666+
}),
667+
);
668+
669+
assert.strictEqual(error.operation, "publishRepository");
670+
assert.strictEqual(
671+
error.detail,
672+
'Created octocat/t3code and added it as remote "origin", but the push failed because SSH authentication was rejected. Add an SSH key to your account, or switch the remote to HTTPS with "git remote set-url origin https://github.com/octocat/t3code", then push again.',
673+
);
674+
assert.instanceOf(error.cause, GitCommandError);
675+
}).pipe(Effect.provide(layer({ git: failingPush("authentication_failed") }))),
676+
);
677+
678+
it.effect("says the repository exists when an unclassified publish push fails", () =>
679+
Effect.gen(function* () {
680+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
681+
const error = yield* Effect.flip(
682+
service.publishRepository({
683+
cwd: "/workspace",
684+
provider: SourceControlProviderKind.make("github"),
685+
repository: "octocat/t3code",
686+
visibility: "private",
687+
protocol: "https",
688+
}),
689+
);
690+
691+
assert.strictEqual(
692+
error.detail,
693+
'Created octocat/t3code and added it as remote "origin", but the push failed. Fix the problem, then push again.',
694+
);
695+
}).pipe(Effect.provide(layer({ git: failingPush() }))),
696+
);
697+
641698
it.effect("publish succeeds with status remote_added when the local repo has no commits", () => {
642699
let pushCalls = 0;
643700
return Effect.gen(function* () {

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

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import * as Schema from "effect/Schema";
99
import {
1010
SourceControlProviderError,
1111
SourceControlRepositoryError,
12+
type GitCommandFailureReason,
1213
type SourceControlCloneRepositoryInput,
1314
type SourceControlCloneRepositoryResult,
1415
type SourceControlCloneProtocol,
@@ -150,6 +151,33 @@ function redactUrlCredentials(text: string): string {
150151
return text.replace(URL_WITH_USERINFO, "$1");
151152
}
152153

154+
/**
155+
* Explains a failed first push of a published repository. The repository
156+
* already exists by then, so publishing again cannot help; the user has to
157+
* fix access and push. Built from fixed text and the provider's URLs only,
158+
* since git's stderr can carry credentials.
159+
*/
160+
function publishPushFailureDetail(input: {
161+
readonly urls: SourceControlRepositoryCloneUrls;
162+
readonly remoteName: string;
163+
readonly remoteUrl: string;
164+
readonly reason: GitCommandFailureReason | undefined;
165+
}): string {
166+
const created = `Created ${input.urls.nameWithOwner} and added it as remote "${input.remoteName}", but the push failed`;
167+
const switchToHttps = `git remote set-url ${input.remoteName} ${input.urls.url}`;
168+
const usesSsh = input.remoteUrl === input.urls.sshUrl;
169+
switch (input.reason) {
170+
case "authentication_failed":
171+
return usesSsh
172+
? `${created} because SSH authentication was rejected. Add an SSH key to your account, or switch the remote to HTTPS with "${switchToHttps}", then push again.`
173+
: `${created} because Git could not authenticate. Check your Git credentials, then push again.`;
174+
case "host_key_unverified":
175+
return `${created} because the host's SSH key is not trusted yet. Verify it by connecting once with ssh, or switch the remote to HTTPS with "${switchToHttps}", then push again.`;
176+
default:
177+
return `${created}. Fix the problem, then push again.`;
178+
}
179+
}
180+
153181
function selectRemoteUrl(
154182
urls: SourceControlRepositoryCloneUrls,
155183
protocol: SourceControlCloneProtocol | undefined,
@@ -446,7 +474,22 @@ export const make = Effect.gen(function* () {
446474
};
447475
}
448476

449-
const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName });
477+
const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName }).pipe(
478+
Effect.mapError(
479+
(cause) =>
480+
new SourceControlRepositoryError({
481+
operation: "publishRepository",
482+
provider: providerKind,
483+
detail: publishPushFailureDetail({
484+
urls,
485+
remoteName,
486+
remoteUrl,
487+
reason: cause.reason,
488+
}),
489+
cause,
490+
}),
491+
),
492+
);
450493

451494
return {
452495
repository: toRepositoryInfo(providerKind, urls),

0 commit comments

Comments
 (0)