Skip to content

Commit adaaae6

Browse files
authored
fix(server): publishing explains why the first push failed (pingdotgg#19219)
1 parent 442d5ca commit adaaae6

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
@@ -633,6 +633,63 @@ it.effect("publishes to the remote name returned by ensureRemote", () => {
633633
);
634634
});
635635

636+
function failingPush(reason?: GitCommandError["reason"]) {
637+
return {
638+
pushCurrentBranch: (cwd: string) =>
639+
Effect.fail(
640+
new GitCommandError({
641+
operation: "GitVcsDriver.pushCurrentBranch.pushWithRequestedRemote",
642+
command: "git",
643+
cwd,
644+
...(reason === undefined ? {} : { reason }),
645+
detail: "Git command exited with a non-zero status.",
646+
}),
647+
),
648+
} satisfies Partial<GitVcsDriver.GitVcsDriver["Service"]>;
649+
}
650+
651+
it.effect("explains a rejected SSH push after creating the repository", () =>
652+
Effect.gen(function* () {
653+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
654+
const error = yield* Effect.flip(
655+
service.publishRepository({
656+
cwd: "/workspace",
657+
provider: SourceControlProviderKind.make("github"),
658+
repository: "octocat/t3code",
659+
visibility: "private",
660+
protocol: "ssh",
661+
}),
662+
);
663+
664+
assert.strictEqual(error.operation, "publishRepository");
665+
assert.strictEqual(
666+
error.detail,
667+
'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.',
668+
);
669+
assert.instanceOf(error.cause, GitCommandError);
670+
}).pipe(Effect.provide(layer({ git: failingPush("authentication_failed") }))),
671+
);
672+
673+
it.effect("says the repository exists when an unclassified publish push fails", () =>
674+
Effect.gen(function* () {
675+
const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
676+
const error = yield* Effect.flip(
677+
service.publishRepository({
678+
cwd: "/workspace",
679+
provider: SourceControlProviderKind.make("github"),
680+
repository: "octocat/t3code",
681+
visibility: "private",
682+
protocol: "https",
683+
}),
684+
);
685+
686+
assert.strictEqual(
687+
error.detail,
688+
'Created octocat/t3code and added it as remote "origin", but the push failed. Fix the problem, then push again.',
689+
);
690+
}).pipe(Effect.provide(layer({ git: failingPush() }))),
691+
);
692+
636693
it.effect("publish succeeds with status remote_added when the local repo has no commits", () => {
637694
let pushCalls = 0;
638695
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,
@@ -141,6 +142,33 @@ function redactUrlCredentials(text: string): string {
141142
return text.replace(URL_WITH_USERINFO, "$1");
142143
}
143144

145+
/**
146+
* Explains a failed first push of a published repository. The repository
147+
* already exists by then, so publishing again cannot help; the user has to
148+
* fix access and push. Built from fixed text and the provider's URLs only,
149+
* since git's stderr can carry credentials.
150+
*/
151+
function publishPushFailureDetail(input: {
152+
readonly urls: SourceControlRepositoryCloneUrls;
153+
readonly remoteName: string;
154+
readonly remoteUrl: string;
155+
readonly reason: GitCommandFailureReason | undefined;
156+
}): string {
157+
const created = `Created ${input.urls.nameWithOwner} and added it as remote "${input.remoteName}", but the push failed`;
158+
const switchToHttps = `git remote set-url ${input.remoteName} ${input.urls.url}`;
159+
const usesSsh = input.remoteUrl === input.urls.sshUrl;
160+
switch (input.reason) {
161+
case "authentication_failed":
162+
return usesSsh
163+
? `${created} because SSH authentication was rejected. Add an SSH key to your account, or switch the remote to HTTPS with "${switchToHttps}", then push again.`
164+
: `${created} because Git could not authenticate. Check your Git credentials, then push again.`;
165+
case "host_key_unverified":
166+
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.`;
167+
default:
168+
return `${created}. Fix the problem, then push again.`;
169+
}
170+
}
171+
144172
function selectRemoteUrl(
145173
urls: SourceControlRepositoryCloneUrls,
146174
protocol: SourceControlCloneProtocol | undefined,
@@ -437,7 +465,22 @@ export const make = Effect.gen(function* () {
437465
};
438466
}
439467

440-
const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName });
468+
const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName }).pipe(
469+
Effect.mapError(
470+
(cause) =>
471+
new SourceControlRepositoryError({
472+
operation: "publishRepository",
473+
provider: providerKind,
474+
detail: publishPushFailureDetail({
475+
urls,
476+
remoteName,
477+
remoteUrl,
478+
reason: cause.reason,
479+
}),
480+
cause,
481+
}),
482+
),
483+
);
441484

442485
return {
443486
repository: toRepositoryInfo(providerKind, urls),

0 commit comments

Comments
 (0)