Skip to content

Commit 894a15d

Browse files
committed
fix(mobile): use maintainer edit permission
1 parent 560960b commit 894a15d

3 files changed

Lines changed: 176 additions & 5 deletions

File tree

Lines changed: 168 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,168 @@
1+
import { RegistryContext } from "@effect/atom-react";
2+
import { canEditPullRequestChangeRequest } from "@t3tools/client-runtime/pull-request-editing";
3+
import {
4+
EnvironmentId,
5+
ProjectId,
6+
SourceControlProviderKind,
7+
type PullRequestDetail,
8+
} from "@t3tools/contracts";
9+
import { Atom, AtomRegistry } from "effect/reactivity";
10+
import { act, useLayoutEffect } from "react";
11+
import { createRoot } from "react-dom/client";
12+
import { expect, it, vi } from "vite-plus/test";
13+
14+
const mocks = vi.hoisted(() => ({
15+
connected: true,
16+
update: vi.fn(),
17+
permission: undefined as unknown as Atom.Writable<boolean>,
18+
detail: undefined as unknown as Atom.Writable<null>,
19+
}));
20+
vi.mock("../../state/pull-requests", () => {
21+
const command = { permissionAtom: () => mocks.permission };
22+
return {
23+
composerPullRequests: {
24+
runAction: command,
25+
comment: command,
26+
submitReview: command,
27+
replyToThread: command,
28+
setThreadResolution: command,
29+
update: command,
30+
updateComment: command,
31+
setReaction: command,
32+
detail: () => mocks.detail,
33+
},
34+
};
35+
});
36+
vi.mock("../../state/use-atom-command", () => ({ useAtomCommand: () => mocks.update }));
37+
vi.mock("../../components/ui/Dialog", () => ({ alert: vi.fn() }));
38+
vi.mock("../../state/workspace", () => ({
39+
useWorkspaceEnvironments: () => [
40+
{
41+
environmentId: "environment",
42+
connectionState: mocks.connected ? "connected" : "offline",
43+
},
44+
],
45+
}));
46+
47+
import { usePullRequestMaintainerEdits } from "./usePullRequestMaintainerEdits";
48+
49+
function setup() {
50+
(globalThis as { IS_REACT_ACT_ENVIRONMENT?: boolean }).IS_REACT_ACT_ENVIRONMENT = true;
51+
mocks.connected = true;
52+
mocks.update.mockReset().mockResolvedValue({ _tag: "Success" });
53+
mocks.permission = Atom.make(true);
54+
mocks.detail = Atom.make(null);
55+
const registry = AtomRegistry.make();
56+
const detail: PullRequestDetail = {
57+
provider: SourceControlProviderKind.make("github"),
58+
projectId: ProjectId.make("project"),
59+
projectTitle: "Project",
60+
workspaceRoot: "/repo",
61+
repository: "owner/repo",
62+
number: 7,
63+
title: "A change",
64+
body: "",
65+
url: "https://github.com/owner/repo/pull/7",
66+
author: { login: "author", name: null, avatarUrl: null },
67+
state: "open",
68+
isDraft: false,
69+
mergeability: "unknown",
70+
additions: 0,
71+
deletions: 0,
72+
changedFiles: 0,
73+
headBranch: "feature",
74+
baseBranch: "main",
75+
createdAt: "2026-01-01T00:00:00Z",
76+
updatedAt: "2026-01-01T00:00:00Z",
77+
mergedAt: null,
78+
closedAt: null,
79+
reviewers: [],
80+
labels: [],
81+
checks: [],
82+
mergeCapabilities: { merge: false, squash: false, rebase: false },
83+
capabilities: {
84+
diff: true,
85+
comment: true,
86+
search: true,
87+
actions: [],
88+
mergeMethods: [],
89+
review: { inlineComment: false, reply: false, resolve: false, verdicts: [] },
90+
reviewers: { request: false, listCandidates: false },
91+
edit: { changeRequest: true, comment: true, maintainerEdits: true },
92+
},
93+
viewerPermissions: {
94+
actions: [],
95+
comment: true,
96+
resolve: false,
97+
verdicts: [],
98+
requestReviewers: false,
99+
maintainerEdits: true,
100+
},
101+
maintainerCanModify: false,
102+
};
103+
const root = createRoot(document.createElement("div"));
104+
let current!: ReturnType<typeof usePullRequestMaintainerEdits>;
105+
function Probe() {
106+
const result = usePullRequestMaintainerEdits(
107+
{
108+
environmentId: "environment",
109+
projectId: "project",
110+
repository: "owner/repo",
111+
number: 7,
112+
},
113+
detail,
114+
);
115+
useLayoutEffect(() => {
116+
current = result;
117+
});
118+
return null;
119+
}
120+
const render = () =>
121+
act(() =>
122+
root.render(
123+
<RegistryContext.Provider value={registry}>
124+
<Probe />
125+
</RegistryContext.Provider>,
126+
),
127+
);
128+
render();
129+
return {
130+
detail,
131+
render,
132+
registry,
133+
get current() {
134+
return current;
135+
},
136+
[Symbol.dispose]() {
137+
act(() => root.unmount());
138+
registry.dispose();
139+
},
140+
};
141+
}
142+
143+
it("saves an explicit maintainer grant without a viewer login, while honoring local access", async () => {
144+
using screen = setup();
145+
expect(canEditPullRequestChangeRequest(screen.detail)).toBe(false);
146+
expect(screen.current.visible).toBe(true);
147+
expect(screen.current.disabled).toBe(false);
148+
await act(async () => screen.current.toggle());
149+
expect(screen.current.value).toBe(true);
150+
expect(mocks.update).toHaveBeenCalledExactlyOnceWith({
151+
environmentId: EnvironmentId.make("environment"),
152+
input: {
153+
projectId: ProjectId.make("project"),
154+
repository: "owner/repo",
155+
number: 7,
156+
maintainerCanModify: true,
157+
},
158+
});
159+
act(() => screen.registry.set(mocks.permission, false));
160+
expect(screen.current.disabled).toBe(true);
161+
await act(async () => screen.current.toggle());
162+
act(() => screen.registry.set(mocks.permission, true));
163+
mocks.connected = false;
164+
screen.render();
165+
expect(screen.current.disabled).toBe(true);
166+
await act(async () => screen.current.toggle());
167+
expect(mocks.update).toHaveBeenCalledTimes(1);
168+
});

‎apps/mobile/src/features/pull-requests/usePullRequestMaintainerEdits.ts‎

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { useAtomRefresh } from "@effect/atom-react";
1+
import { useAtomRefresh, useAtomValue } from "@effect/atom-react";
22
import { canEditPullRequestMaintainerEdits } from "@t3tools/client-runtime/pull-request-editing";
33
import type { PullRequestDetail } from "@t3tools/contracts";
44
import { AsyncResult } from "effect/reactivity";
@@ -7,14 +7,16 @@ import * as Dialog from "../../components/ui/Dialog";
77
import { composerPullRequests } from "../../state/pull-requests";
88
import { useAtomCommand } from "../../state/use-atom-command";
99
import { pullRequestTarget, type PullRequestParams } from "./usePullRequestDiff";
10-
import { usePullRequestPermissions } from "./usePullRequestPermissions";
10+
import { usePullRequestConnected } from "./usePullRequestConnected";
1111

1212
export function usePullRequestMaintainerEdits(
1313
params: PullRequestParams,
1414
pr: PullRequestDetail | null,
1515
) {
1616
const target = pullRequestTarget(params);
17-
const permissions = usePullRequestPermissions(params, pr);
17+
const connected = usePullRequestConnected(target.environmentId);
18+
const canUpdate = useAtomValue(composerPullRequests.update.permissionAtom(target.environmentId));
19+
const allowed = connected && canUpdate;
1820
const update = useAtomCommand(composerPullRequests.update, { reportFailure: false });
1921
const refresh = useAtomRefresh(composerPullRequests.detail(target));
2022
const key = `${pr?.url}:${pr?.maintainerCanModify}`;
@@ -27,7 +29,7 @@ export function usePullRequestMaintainerEdits(
2729
const inFlight = useRef(false);
2830
const visible = pr !== null && canEditPullRequestMaintainerEdits(pr);
2931
const save = async (next: boolean) => {
30-
if (!visible || !permissions.canEditChangeRequest || inFlight.current) return;
32+
if (!visible || !allowed || inFlight.current) return;
3133
const previous = value;
3234
inFlight.current = true;
3335
setSaving(true);
@@ -49,7 +51,7 @@ export function usePullRequestMaintainerEdits(
4951
return {
5052
visible,
5153
value,
52-
disabled: !permissions.canEditChangeRequest || saving,
54+
disabled: !allowed || saving,
5355
subtitle: saving
5456
? "Saving…"
5557
: "Maintainers can push to this branch and access fork workflow secrets.",

‎apps/mobile/vite.config.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const reactTests = [
1111
"src/features/threads/thread-inspector-navigation.test.tsx",
1212
"src/features/diffs/nativeReviewDiffSurface.test.tsx",
1313
"src/features/pull-requests/usePullRequestDiff.test.tsx",
14+
"src/features/pull-requests/usePullRequestMaintainerEdits.test.tsx",
1415
"src/components/ui/Dialog.android.test.tsx",
1516
"src/features/pull-requests/guide/useReviewGuide.test.tsx",
1617
"src/features/settings/SettingsServerControlsRouteScreen.test.tsx",

0 commit comments

Comments
 (0)