TypeScript
fix(web): show readable errors when a stack action fails (#24118)
Victor Vogel665351fc391d
diff --git a/apps/web/src/components/pullRequest/PullRequestStackMenu.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestStackMenu.browser.test.tsxnew file mode 100644index 00000000000..790cf493e82--- /dev/null+++ b/apps/web/src/components/pullRequest/PullRequestStackMenu.browser.test.tsx@@ -0,0 +1,75 @@+import { sourceControlClients } from "@t3tools/client-runtime/source-control-clients";+import { EnvironmentId, ProjectId } from "@t3tools/contracts";+import { expect, it, vi } from "vite-plus/test";+import { page } from "vite-plus/test/browser";++const state = vi.hoisted(() => ({ detail: "" }));++vi.mock(import("~/state/use-atom-command"), async () => {+ const { PullRequestOperationError } = await import("@t3tools/contracts");+ const { Cause } = await import("effect");+ const { AsyncResult } = await import("effect/reactivity");+ return {+ useAtomCommand: (() => async () =>+ AsyncResult.failure(+ Cause.fail(new PullRequestOperationError({ operation: "runAction", detail: state.detail })),+ )) as never,+ };+});++import "~/index.css";+import { render } from "../../../test/render";+import { ToastProvider } from "../ui/toast";+import { PullRequestStackMenu } from "./PullRequestStackMenu";++function setup(detail: string) {+ state.detail = detail;+}++const HEAD_MOVED = "The stack head moved. Refresh the stack before trying again.";++it.each([+ { label: "Merge stack", detail: HEAD_MOVED, description: HEAD_MOVED },+ { label: "Rebase stack", detail: HEAD_MOVED, description: HEAD_MOVED },+ {+ label: "Rebase stack",+ detail: "GitHub CLI command failed.",+ description:+ "The host refused the rebase. A rebase stops at the first commit that does not apply cleanly.",+ },+])("$label refused with $detail shows $description", async (row) => {+ setup(row.detail);+ await using screen = await render(+ <ToastProvider>+ <PullRequestStackMenu+ environmentId={EnvironmentId.make("environment")}+ reference={{+ projectId: ProjectId.make("project"),+ repository: "owner/repository",+ number: 2,+ }}+ hostLabel="GitHub"+ terminology={sourceControlClients.get("github").changeRequest}+ stack={{+ id: "stack",+ number: 1,+ url: "https://github.com/owner/repository/stack/1",+ base: "main",+ layers: [{ number: 2, headBranch: "feature", headSha: "abc123", state: "open" }],+ }}+ canMerge+ canRebase+ mergeMethod="squash"+ onActed={() => {}}+ />+ </ToastProvider>,+ );+ if (row.label === "Rebase stack") {+ await screen.getByRole("button", { name: "Stack 1, layer 1 of 1" }).click();+ await page.getByRole("menuitem", { name: row.label }).click();+ } else {+ await screen.getByRole("button", { name: row.label, exact: true }).click();+ }+ await page.getByRole("dialog").getByRole("button", { name: row.label, exact: true }).click();+ await expect.element(page.getByText(row.description, { exact: true })).toBeInTheDocument();+});diff --git a/apps/web/src/components/pullRequest/PullRequestStackMenu.tsx b/apps/web/src/components/pullRequest/PullRequestStackMenu.tsxindex 359fdb47d71..c870eebdd58 100644--- a/apps/web/src/components/pullRequest/PullRequestStackMenu.tsx+++ b/apps/web/src/components/pullRequest/PullRequestStackMenu.tsx@@ -27,6 +27,7 @@ import { PullRequestStackLayers } from "./PullRequestStackLayers";import { PullRequestStackHeader } from "./PullRequestStackHeader";import { PullRequestStackLayerContent } from "./PullRequestStackLayerContent";import { PullRequestGlyph } from "./pullRequestIcons";+import { readableFailure } from "./pullRequestDetail.logic";export function PullRequestStackMenu({hostLabel,@@ -114,7 +115,12 @@ export function PullRequestStackMenu({toastManager.update(toastId, {type: "error",title: action === "merge" ? "Could not merge the stack" : "Could not rebase the stack",- description: String(squashAtomCommandFailure(result)),+ description: readableFailure(+ squashAtomCommandFailure(result),+ action === "merge"+ ? "The host refused the merge. Check that you have write access, that the checks it requires have passed, and that the branch is not conflicting."+ : "The host refused the rebase. A rebase stops at the first commit that does not apply cleanly.",+ ),});} else {toastManager.update(toastId, {