Skip to content
TypeScript

fix(server): publishing explains why the first push failed (#19219)

Salloxyadaaae60c66b
diff --git a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts b/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts
index 7e6662f3e44..cbce130dcc8 100644
--- a/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts
+++ b/apps/server/src/sourceControl/SourceControlRepositoryService.test.ts
@@ -633,6 +633,63 @@ it.effect("publishes to the remote name returned by ensureRemote", () => {
);
});
+function failingPush(reason?: GitCommandError["reason"]) {
+ return {
+ pushCurrentBranch: (cwd: string) =>
+ Effect.fail(
+ new GitCommandError({
+ operation: "GitVcsDriver.pushCurrentBranch.pushWithRequestedRemote",
+ command: "git",
+ cwd,
+ ...(reason === undefined ? {} : { reason }),
+ detail: "Git command exited with a non-zero status.",
+ }),
+ ),
+ } satisfies Partial<GitVcsDriver.GitVcsDriver["Service"]>;
+}
+
+it.effect("explains a rejected SSH push after creating the repository", () =>
+ Effect.gen(function* () {
+ const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
+ const error = yield* Effect.flip(
+ service.publishRepository({
+ cwd: "/workspace",
+ provider: SourceControlProviderKind.make("github"),
+ repository: "octocat/t3code",
+ visibility: "private",
+ protocol: "ssh",
+ }),
+ );
+
+ assert.strictEqual(error.operation, "publishRepository");
+ assert.strictEqual(
+ error.detail,
+ '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.',
+ );
+ assert.instanceOf(error.cause, GitCommandError);
+ }).pipe(Effect.provide(layer({ git: failingPush("authentication_failed") }))),
+);
+
+it.effect("says the repository exists when an unclassified publish push fails", () =>
+ Effect.gen(function* () {
+ const service = yield* SourceControlRepositoryService.SourceControlRepositoryService;
+ const error = yield* Effect.flip(
+ service.publishRepository({
+ cwd: "/workspace",
+ provider: SourceControlProviderKind.make("github"),
+ repository: "octocat/t3code",
+ visibility: "private",
+ protocol: "https",
+ }),
+ );
+
+ assert.strictEqual(
+ error.detail,
+ 'Created octocat/t3code and added it as remote "origin", but the push failed. Fix the problem, then push again.',
+ );
+ }).pipe(Effect.provide(layer({ git: failingPush() }))),
+);
+
it.effect("publish succeeds with status remote_added when the local repo has no commits", () => {
let pushCalls = 0;
return Effect.gen(function* () {
diff --git a/apps/server/src/sourceControl/SourceControlRepositoryService.ts b/apps/server/src/sourceControl/SourceControlRepositoryService.ts
index f3b46816b23..fb59349749a 100644
--- a/apps/server/src/sourceControl/SourceControlRepositoryService.ts
+++ b/apps/server/src/sourceControl/SourceControlRepositoryService.ts
@@ -9,6 +9,7 @@ import * as Schema from "effect/Schema";
import {
SourceControlProviderError,
SourceControlRepositoryError,
+ type GitCommandFailureReason,
type SourceControlCloneRepositoryInput,
type SourceControlCloneRepositoryResult,
type SourceControlCloneProtocol,
@@ -141,6 +142,33 @@ function redactUrlCredentials(text: string): string {
return text.replace(URL_WITH_USERINFO, "$1");
}
+/**
+ * Explains a failed first push of a published repository. The repository
+ * already exists by then, so publishing again cannot help; the user has to
+ * fix access and push. Built from fixed text and the provider's URLs only,
+ * since git's stderr can carry credentials.
+ */
+function publishPushFailureDetail(input: {
+ readonly urls: SourceControlRepositoryCloneUrls;
+ readonly remoteName: string;
+ readonly remoteUrl: string;
+ readonly reason: GitCommandFailureReason | undefined;
+}): string {
+ const created = `Created ${input.urls.nameWithOwner} and added it as remote "${input.remoteName}", but the push failed`;
+ const switchToHttps = `git remote set-url ${input.remoteName} ${input.urls.url}`;
+ const usesSsh = input.remoteUrl === input.urls.sshUrl;
+ switch (input.reason) {
+ case "authentication_failed":
+ return usesSsh
+ ? `${created} because SSH authentication was rejected. Add an SSH key to your account, or switch the remote to HTTPS with "${switchToHttps}", then push again.`
+ : `${created} because Git could not authenticate. Check your Git credentials, then push again.`;
+ case "host_key_unverified":
+ 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.`;
+ default:
+ return `${created}. Fix the problem, then push again.`;
+ }
+}
+
function selectRemoteUrl(
urls: SourceControlRepositoryCloneUrls,
protocol: SourceControlCloneProtocol | undefined,
@@ -437,7 +465,22 @@ export const make = Effect.gen(function* () {
};
}
- const pushResult = yield* git.pushCurrentBranch(input.cwd, null, { remoteName });
+ 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,
+ }),
+ cause,
+ }),
+ ),
+ );
return {
repository: toRepositoryInfo(providerKind, urls),