Skip to content

Commit a35066d

Browse files
authored
fix(web): thread details PR merge uses the last selected merge method (pingdotgg#22399)
1 parent 1ee0241 commit a35066d

4 files changed

Lines changed: 55 additions & 13 deletions

File tree

‎apps/web/src/components/chat/ThreadDetailsPrRow.browser.test.tsx‎

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { expect, it, vi } from "vite-plus/test";
66
const state = vi.hoisted(() => ({
77
status: "success" as PullRequestCheck["status"],
88
extraStatus: null as PullRequestCheck["status"] | null,
9+
mergeMethods: ["merge"] as Array<"merge" | "squash" | "rebase">,
910
perform: vi.fn(),
1011
newThread: vi.fn(),
1112
prepareThread: vi.fn(),
@@ -45,9 +46,13 @@ const queryData = () => ({
4546
description: null,
4647
url: null,
4748
})),
48-
capabilities: { actions: ["merge"], mergeMethods: ["merge"] },
49+
capabilities: { actions: ["merge"], mergeMethods: state.mergeMethods },
4950
viewerPermissions: { actions: ["merge"] },
50-
mergeCapabilities: { merge: true, squash: false, rebase: false },
51+
mergeCapabilities: {
52+
merge: state.mergeMethods.includes("merge"),
53+
squash: state.mergeMethods.includes("squash"),
54+
rebase: state.mergeMethods.includes("rebase"),
55+
},
5156
});
5257
vi.mock(import("~/state/query"), async (importOriginal) => ({
5358
...(await importOriginal()),
@@ -96,8 +101,41 @@ vi.mock(import("../ui/toast"), async (original) => {
96101
});
97102

98103
import { act, render as renderUi } from "../../../test/render";
104+
import { useUiStateStore } from "~/uiStateStore";
99105
import { ThreadDetailsPrRow } from "./ThreadDetailsPrRow";
100106

107+
it("merges with the last selected merge method", async () => {
108+
await using screen = await setup();
109+
const { container } = screen;
110+
state.mergeMethods = ["merge", "squash"];
111+
useUiStateStore.getState().setPullRequestMergeMethod("squash");
112+
await act(async () => {
113+
await screen.rerender(
114+
<ThreadDetailsPrRow
115+
environmentId={EnvironmentId.make("environment")}
116+
pr={null}
117+
number={1}
118+
status={null}
119+
project={null}
120+
label="Test PR"
121+
openAriaLabel="Open PR"
122+
onOpen={vi.fn()}
123+
/>,
124+
);
125+
});
126+
const clickMerge = async (scope: ParentNode) =>
127+
await act(async () => {
128+
[...scope.querySelectorAll("button")]
129+
.find((button) => button.textContent?.trim() === "Merge")!
130+
.click();
131+
});
132+
await clickMerge(container);
133+
const dialog = document.querySelector('[role="alertdialog"]')!;
134+
expect(dialog.textContent).toContain("using squash");
135+
await clickMerge(dialog);
136+
expect(state.perform).toHaveBeenCalledWith("merge", "squash");
137+
});
138+
101139
it("requires a new merge click after passing checks become pending and pass again", async () => {
102140
await using screen = await setup();
103141
const { container } = screen;
@@ -152,5 +190,7 @@ async function setup() {
152190

153191
state.status = "success";
154192
state.extraStatus = null;
193+
state.mergeMethods = ["merge"];
194+
useUiStateStore.getState().setPullRequestMergeMethod("merge");
155195
return renderUi(null);
156196
}

‎apps/web/src/components/chat/ThreadDetailsPrRow.tsx‎

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import { usePullRequestChecksRefresh } from "~/hooks/usePullRequestChecksRefresh
2121
import { cn } from "~/lib/utils";
2222
import { useServerConfigs } from "~/state/entities";
2323
import { pullRequestEnvironment } from "~/state/pullRequests";
24+
import { useUiStateStore } from "~/uiStateStore";
2425
import {
2526
useEnvironmentQueryIsNewer,
2627
useEnvironmentQueryRefresh,
@@ -33,7 +34,7 @@ import {
3334
classifyPullRequestChecks,
3435
describePullRequestChecks,
3536
isPullRequestConflicting,
36-
resolveSelectedMergeMethod,
37+
resolvePullRequestMergeMethod,
3738
allowedPullRequestMergeMethods,
3839
resolveThreadPanelPullRequestAction,
3940
} from "../pullRequest/pullRequestDetail.logic";
@@ -46,6 +47,7 @@ import {
4647
} from "../pullRequest/pullRequestPresentation";
4748
import {
4849
usePullRequestActionRunner,
50+
usePullRequestDefaultMergeMethodResolver,
4951
usePullRequestHandoffs,
5052
} from "../pullRequest/usePullRequestActions";
5153
import {
@@ -166,6 +168,11 @@ export function ThreadDetailsPrRow({
166168
},
167169
});
168170
const { handoff, startHandoff } = usePullRequestHandoffs({ environmentId, detail });
171+
const lastSelectedMergeMethod = useUiStateStore((state) => state.pullRequestMergeMethod);
172+
const resolveProjectDefaultMergeMethod = usePullRequestDefaultMergeMethodResolver(
173+
environmentId,
174+
(project?.id as ProjectId | undefined) ?? null,
175+
);
169176
const [confirmingMerge, setConfirmingMerge] = useState(false);
170177

171178
const rowAction = resolveThreadPanelPullRequestAction(detail);
@@ -175,9 +182,11 @@ export function ThreadDetailsPrRow({
175182
const conflicting = isPullRequestConflicting(detail);
176183
const checksState = detail === null ? "none" : classifyPullRequestChecks(detail.checks);
177184
const checksRollup = detail === null ? null : pullRequestChecksState(detail.checks);
178-
const selectedMergeMethod = resolveSelectedMergeMethod(
185+
const selectedMergeMethod = resolvePullRequestMergeMethod(
179186
allowedPullRequestMergeMethods(detail),
180-
"merge",
187+
null,
188+
resolveProjectDefaultMergeMethod(),
189+
lastSelectedMergeMethod,
181190
);
182191

183192
const startResolveConflicts = () => {

‎apps/web/src/components/pullRequest/pullRequestDetail.logic.ts‎

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,6 @@ export function allowedPullRequestMergeMethods(
292292
: detail.capabilities.mergeMethods.filter((method) => detail.mergeCapabilities[method]);
293293
}
294294

295-
export function resolveSelectedMergeMethod(
296-
allowedMergeMethods: ReadonlyArray<PullRequestMergeMethod>,
297-
preferred: PullRequestMergeMethod,
298-
): PullRequestMergeMethod {
299-
return allowedMergeMethods.includes(preferred) ? preferred : (allowedMergeMethods[0] ?? "merge");
300-
}
301-
302295
function canPerformPullRequestAction(
303296
detail: Pick<PullRequestActionableDetail, "capabilities" | "viewerPermissions"> | null,
304297
action: PullRequestAction,

‎apps/web/src/components/pullRequest/usePullRequestActions.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ import { pullRequestEntryKey, type EnvironmentPullRequestEntry } from "./pullReq
4242

4343
export function usePullRequestDefaultMergeMethodResolver(
4444
environmentId: EnvironmentId,
45-
projectId: ProjectId,
45+
projectId: ProjectId | null,
4646
) {
4747
const projectDefault = useEnvironmentSettings(
4848
environmentId,

0 commit comments

Comments
 (0)