Skip to content
TypeScript

fix(web): disable conflict checkout without required permissions (#23475)

Victor Vogel3a873e8fafd2
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.permissions.browser.test.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.permissions.browser.test.tsx
new file mode 100644
index 00000000000..ea544da84ec
--- /dev/null
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.permissions.browser.test.tsx
@@ -0,0 +1,164 @@
+import {
+ AuthOrchestrationOperateScope,
+ AuthSourceControlWriteScope,
+ EnvironmentId,
+ ProjectId,
+ SourceControlProviderKind,
+ type PullRequestDetailView,
+} from "@t3tools/contracts";
+import { expect, it, vi } from "vite-plus/test";
+import { DraftId, useComposerDraftStore } from "~/composerDraftStore";
+import { act, render } from "../../../test/render";
+
+const { permissions } = vi.hoisted(() => ({
+ permissions: { checkout: true, operate: true },
+}));
+
+vi.mock(import("~/state/session"), async (original) => ({
+ ...(await original()),
+ useEnvironmentScope: (_environmentId: unknown, scope: unknown) =>
+ scope === AuthOrchestrationOperateScope ? permissions.operate : permissions.checkout,
+ readEnvironmentScope: (_environmentId: unknown, scope: unknown) =>
+ scope === AuthOrchestrationOperateScope ? permissions.operate : permissions.checkout,
+}));
+vi.mock(import("~/state/sourceControlActions"), async (original) => ({
+ ...(await original()),
+ usePreparePullRequestThreadAction: (() => ({
+ isAllowed: permissions.checkout,
+ run: vi.fn(),
+ error: null,
+ })) as never,
+}));
+vi.mock(import("./usePullRequestDetail"), async (original) => ({
+ ...(await original()),
+ usePullRequestDetail: (() => ({
+ canWriteSourceControl: permissions.checkout,
+ detailQuery: { isPending: false, refresh: vi.fn() },
+ activityQuery: { isPending: false, refresh: vi.fn(), error: null },
+ activity: null,
+ sharedSummary: detail,
+ coreDetail: detail,
+ detail,
+ })) as never,
+}));
+vi.mock(import("~/state/usePullRequestStack"), async (original) => ({
+ ...(await original()),
+ usePullRequestStack: (() => ({
+ data: null,
+ isFresh: true,
+ isPending: false,
+ error: null,
+ refresh: vi.fn(),
+ })) as never,
+}));
+vi.mock(import("~/state/use-atom-command"), async (original) => ({
+ ...(await original()),
+ useAtomCommand: (() => vi.fn()) as never,
+}));
+
+import { PullRequestDetailPanel } from "./PullRequestDetailPanel";
+
+const detail: PullRequestDetailView = {
+ provider: SourceControlProviderKind.make("github"),
+ projectId: ProjectId.make("project"),
+ projectTitle: "Project",
+ workspaceRoot: "/workspace",
+ repository: "owner/repo",
+ number: 1,
+ title: "Test pull request",
+ body: "Original description",
+ url: "https://github.com/owner/repo/pull/1",
+ author: { login: "author", name: null, avatarUrl: null },
+ viewer: "author",
+ state: "open",
+ isDraft: false,
+ mergeability: "conflicting",
+ additions: 1,
+ deletions: 0,
+ changedFiles: 1,
+ headBranch: "feature",
+ baseBranch: "main",
+ createdAt: "2026-09-01T00:00:00Z",
+ updatedAt: "2026-09-01T00:00:00Z",
+ mergedAt: null,
+ closedAt: null,
+ reviewers: [],
+ labels: [],
+ checks: [{ name: "Unit tests", status: "success", description: null, url: null }],
+ comments: [],
+ commentCount: 0,
+ commentsTruncated: false,
+ reviewThreads: [],
+ commits: [],
+ mergeCapabilities: { merge: false, squash: false, rebase: false },
+ capabilities: {
+ diff: true,
+ comment: false,
+ search: true,
+ actions: [],
+ mergeMethods: [],
+ review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
+ reviewers: { request: false, listCandidates: false },
+ edit: { changeRequest: true, comment: false },
+ },
+ viewerPermissions: {
+ actions: [],
+ comment: false,
+ resolve: false,
+ verdicts: [],
+ requestReviewers: false,
+ },
+};
+
+const environmentId = EnvironmentId.make("environment");
+const draftId = DraftId.make("conflicts-draft");
+const panel = (attach = false) => (
+ <PullRequestDetailPanel
+ environmentId={environmentId}
+ reference={detail}
+ context="page"
+ shortcutsEnabled={false}
+ getShortcutContext={() => ({
+ terminalFocus: false,
+ terminalOpen: false,
+ previewFocus: false,
+ previewOpen: false,
+ isWeb: true,
+ isDesktop: false,
+ })}
+ {...(attach ? { composerDraftTarget: draftId } : {})}
+ />
+);
+
+function setup() {
+ permissions.checkout = true;
+ permissions.operate = true;
+ useComposerDraftStore.setState({ draftsByThreadKey: {} });
+}
+
+it.each([AuthSourceControlWriteScope, AuthOrchestrationOperateScope])(
+ "disables conflict checkout when the connection loses %s",
+ async (scope) => {
+ setup();
+ await using screen = await render(panel());
+ const resolve = screen.getByRole("button", { name: "Resolve conflicts" });
+ await expect.element(resolve).toBeEnabled();
+ if (scope === AuthSourceControlWriteScope) permissions.checkout = false;
+ else permissions.operate = false;
+ await screen.rerender(panel());
+ await expect.element(resolve).toBeDisabled();
+ },
+);
+
+it("can still add conflict resolution to an existing composer without checkout scopes", async () => {
+ setup();
+ permissions.checkout = false;
+ permissions.operate = false;
+ await using screen = await render(panel(true));
+ const resolve = screen.getByRole("button", { name: "Resolve conflicts" });
+ await expect.element(resolve).toBeEnabled();
+ await act(() => resolve.click());
+ const draft = useComposerDraftStore.getState().getComposerDraft(draftId);
+ expect(draft?.prompt).toContain("conflicts");
+ expect(draft?.prompt).toContain(detail.url);
+});
diff --git a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
index 6fd281aa145..3313bd86e45 100644
--- a/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
+++ b/apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx
@@ -1229,7 +1229,7 @@ export function PullRequestDetailPanel({
const resolveConflictsControl = (
<PullRequestResolveConflictsButton
handoff={handoff}
- unavailable={attachTarget === null && checkoutRoot === null}
+ unavailable={attachTarget === null && (checkoutRoot === null || !canPrepareWorktree)}
onResolve={startResolveConflicts}
/>
);