Skip to content
TypeScript

fix(server): pushes the remote refuses say why instead of a generic failure (#22904)

Theo Brownefb7f08782b96
diff --git a/apps/server/src/vcs/GitVcsDriverCore.test.ts b/apps/server/src/vcs/GitVcsDriverCore.test.ts
index c0c5f261563..84861d6dd4a 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.test.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.test.ts
@@ -80,6 +80,13 @@ const makeSuccessfulHandle = (stdout: string) =>
getOutputFd: () => Stream.empty,
});
+const makeFailedHandle = (stderr: string) =>
+ ChildProcessSpawner.makeHandle({
+ ...makeNonRepositoryHandle(),
+ exitCode: Effect.succeed(ChildProcessSpawner.ExitCode(1)),
+ stderr: Stream.encodeText(Stream.make(stderr)),
+ });
+
const makeTmpDir = (
prefix = "git-vcs-driver-test-",
): Effect.Effect<string, PlatformError.PlatformError, FileSystem.FileSystem | Scope.Scope> =>
@@ -803,6 +810,88 @@ it.effect("retries a fetch that lost a ref update to another Git process", () =>
}).pipe(Effect.provide(layerServerConfig.pipe(Layer.provideMerge(NodeServices.layer)))),
);
+describe("rejected git refs", () => {
+ const remoteUrl = "https://user:[email protected]/org/secret-repo.git";
+ const failedPush = `error: failed to push some refs to '${remoteUrl}'`;
+ const leakedText = [
+ "token-123",
+ "secret-repo",
+ "git.example.invalid",
+ "private-branch",
+ "workflow scope",
+ ];
+
+ const failWith = (stderr: string) => {
+ const spawner = ChildProcessSpawner.make(() => Effect.succeed(makeFailedHandle(stderr)));
+ const layer = GitVcsDriver.layer.pipe(
+ Layer.provide(layerServerConfig),
+ Layer.provideMerge(
+ Layer.merge(
+ NodeServices.layer,
+ Layer.succeed(ChildProcessSpawner.ChildProcessSpawner, spawner),
+ ),
+ ),
+ );
+ return Effect.gen(function* () {
+ const driver = yield* GitVcsDriver.GitVcsDriver;
+ return yield* driver
+ .execute({
+ operation: "GitVcsDriver.test.rejectedRef",
+ cwd: "/repo",
+ args: ["push", "origin", "HEAD"],
+ })
+ .pipe(Effect.flip);
+ }).pipe(Effect.provide(layer));
+ };
+
+ it.effect.each([
+ {
+ label: "a ref the remote refused",
+ stderr: [
+ "remote: Unable to determine if workflow can be created; workflow scope may be required.",
+ `To ${remoteUrl}`,
+ " ! [remote rejected] private-branch -> private-branch (workflow scope may be required.)",
+ failedPush,
+ ],
+ reason: "push_rejected_by_remote" as const,
+ detail: "The remote rejected the push.",
+ },
+ {
+ label: "a non-fast-forward push",
+ stderr: [
+ `To ${remoteUrl}`,
+ " ! [rejected] private-branch -> private-branch (non-fast-forward)",
+ failedPush,
+ ],
+ reason: "push_rejected_non_fast_forward" as const,
+ detail: "Pull or rebase",
+ },
+ ])("names $label without quoting stderr", ({ stderr, reason, detail }) =>
+ Effect.gen(function* () {
+ const error = yield* failWith(stderr.join("\n"));
+
+ assert.deepInclude(error, { reason });
+ assert.include(error.detail, detail);
+ for (const text of leakedText) assert.notInclude(error.message, text);
+ }),
+ );
+
+ // A fetch into a diverged local branch prints the same rejected line, but
+ // never "failed to push some refs".
+ it.effect("leaves a rejected fetch unclassified", () =>
+ Effect.gen(function* () {
+ const error = yield* failWith(
+ [`From ${remoteUrl}`, " ! [rejected] main -> side (non-fast-forward)"].join(
+ "\n",
+ ),
+ );
+
+ assert.equal(error.reason, undefined);
+ assert.equal(error.detail, "Git command exited with a non-zero status.");
+ }),
+ );
+});
+
it.layer(layerTest)("GitVcsDriver core integration", (it) => {
describe("structured errors", () => {
it.effect("does not retain git arguments or stderr in command failures", () =>
@@ -1071,7 +1160,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
}),
);
- it.effect("does not read remote hook output as a git failure reason", () =>
+ it.effect("reads a hook-declined push from git's rejection line, not the hook's output", () =>
Effect.gen(function* () {
const parent = yield* makeTmpDir();
const pathService = yield* Path.Path;
@@ -1100,7 +1189,7 @@ it.layer(layerTest)("GitVcsDriver core integration", (it) => {
})
.pipe(Effect.flip);
- assert.notProperty(error, "reason");
+ assert.deepInclude(error, { reason: "push_rejected_by_remote" });
assert.notInclude(error.message, "(authentication_failed)");
}),
);
diff --git a/apps/server/src/vcs/GitVcsDriverCore.ts b/apps/server/src/vcs/GitVcsDriverCore.ts
index 0f3fe9a1e6b..62798374d2a 100644
--- a/apps/server/src/vcs/GitVcsDriverCore.ts
+++ b/apps/server/src/vcs/GitVcsDriverCore.ts
@@ -456,8 +456,32 @@ const GIT_FAILURE_REASON_PATTERNS: ReadonlyArray<readonly [RegExp, GitCommandFai
/^fatal: (?:invalid reference: |not a valid object name|needed a single revision|bad revision |'.+' is not a commit and a branch '.+' cannot be created from it)/im,
"ref_not_found",
],
+ // `[remote rejected]` is only printed for a push. The remote's explanation
+ // follows in parentheses; it is server text, so it is never quoted.
+ [/^[ \t]*![ \t]+\[remote rejected\]/im, "push_rejected_by_remote"],
+ // A fetch into a diverged local branch prints the same `[rejected]` line, but
+ // only a push goes on to report "failed to push some refs".
+ [
+ /^[ \t]*![ \t]+\[rejected\][^\n]*\((?:non-fast-forward|fetch first)\)[\s\S]*^error: failed to push some refs/im,
+ "push_rejected_non_fast_forward",
+ ],
];
+// Reasons the user can act on get a sentence in place of the generic detail.
+const GIT_FAILURE_REASON_DETAILS: Partial<Record<GitCommandFailureReason, string>> = {
+ push_rejected_by_remote:
+ "The remote rejected the push. Push this branch from a terminal to see the server's message.",
+ push_rejected_non_fast_forward:
+ "The remote has commits this branch doesn't have. Pull or rebase, then push again.",
+};
+
+function gitFailureDetail(reason: GitCommandFailureReason | null): string {
+ return (
+ (reason === null ? undefined : GIT_FAILURE_REASON_DETAILS[reason]) ??
+ "Git command exited with a non-zero status."
+ );
+}
+
// Existing non-commit objects share the missing-ref fatal diagnostic; Git
// distinguishes them with a separate error line.
const GIT_NOT_A_COMMIT_PATTERN = /^error: .+(?:, not a commit|expected commit type)/im;
@@ -1031,7 +1055,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
return yield* new GitCommandError({
...gitCommandContext(commandInput),
...(reason === null ? {} : { reason }),
- detail: "Git command exited with a non-zero status.",
+ detail: gitFailureDetail(reason),
exitCode,
stdoutLength: stdout.text.length,
stderrLength: stderr.text.length,
@@ -1123,7 +1147,7 @@ export const makeGitVcsDriverCore = Effect.fn("makeGitVcsDriverCore")(function*
new GitCommandError({
...gitCommandContext({ operation, cwd, args }),
...(reason === null ? {} : { reason }),
- detail: options.fallbackErrorDetail ?? "Git command exited with a non-zero status.",
+ detail: options.fallbackErrorDetail ?? gitFailureDetail(reason),
...(result.exitCode === null ? {} : { exitCode: result.exitCode }),
stdoutLength: result.stdout.length,
stderrLength: result.stderr.length,
diff --git a/packages/contracts/src/git.ts b/packages/contracts/src/git.ts
index 0976dcb07d5..cc5fcd01458 100644
--- a/packages/contracts/src/git.ts
+++ b/packages/contracts/src/git.ts
@@ -361,6 +361,8 @@ export const GitCommandFailureReason = Schema.Literals([
"host_key_unverified",
"not_a_repository",
"path_already_exists",
+ "push_rejected_by_remote",
+ "push_rejected_non_fast_forward",
"ref_not_found",
"remote_unreachable",
"tag_would_be_clobbered",