Skip to content

Commit 27518ae

Browse files
committed
fix(workspace): address repository review feedback (#11251)
1 parent f479aae commit 27518ae

26 files changed

Lines changed: 575 additions & 125 deletions

‎apps/mobile/src/features/review/useReviewSections.ts‎

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { useFocusEffect } from "@react-navigation/native";
22
import { useWorkspaceRepositories } from "../../state/use-workspace-repositories";
33
import { useWorkspaceReviewSources } from "./useWorkspaceReviewSources";
4-
import { useCallback, useEffect, useMemo, useState } from "react";
4+
import { useCallback, useEffect, useMemo } from "react";
55

66
import type { EnvironmentId, OrchestrationCheckpointSummary, ThreadId } from "@t3tools/contracts";
77

@@ -40,11 +40,7 @@ export function useReviewSections(input: {
4040
if (enabled) refreshRepositories();
4141
}, [enabled, refreshRepositories]),
4242
);
43-
const [filter, setFilter] = useState<{ cwd: string | null; path: string | null }>({
44-
cwd: null,
45-
path: null,
46-
});
47-
const repositoryFilter = filter.cwd === selectedThreadCwd ? filter.path : null;
43+
const repositoryFilter = workspace.repositoryFilter;
4844
const repositories = useMemo(
4945
() =>
5046
workspace.repositories.length
@@ -238,7 +234,7 @@ export function useReviewSections(input: {
238234
return {
239235
repositories,
240236
repositoryFilter,
241-
selectRepositoryFilter: (path: string | null) => setFilter({ cwd: selectedThreadCwd, path }),
237+
selectRepositoryFilter: workspace.selectRepository,
242238
error:
243239
workspace.repositoryError ??
244240
diffPreview.error ??

‎apps/mobile/src/features/review/workspaceReviewDiff.test.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,19 @@ describe("workspace review diffs", () => {
3535
expect(parsed.files[0]?.path).toBe("src/index.ts");
3636
expect(buildWorkspaceReviewDiff([], "thread")).toEqual({ kind: "empty" });
3737
});
38+
it("retains every repository's complete patch when a member needs raw fallback", () => {
39+
const broken = "unrecognized patch format";
40+
const parsed = buildWorkspaceReviewDiff(
41+
[
42+
{ path: "projects/valid", diff },
43+
{ path: "projects/broken", diff: broken },
44+
],
45+
"thread",
46+
);
47+
expect(parsed.kind).toBe("raw");
48+
if (parsed.kind !== "raw") throw new Error("Expected raw fallback");
49+
expect(parsed.text).toContain(`Repository: projects/valid\n${diff}`);
50+
expect(parsed.text).toContain(`Repository: projects/broken\n${broken}`);
51+
expect(parsed.reason).toContain("projects/broken:");
52+
});
3853
});

‎apps/mobile/src/features/review/workspaceReviewDiff.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ export function buildWorkspaceReviewDiff(
1111
): ReviewParsedDiff {
1212
const files: Extract<ReviewParsedDiff, { kind: "files" }>["files"][number][] = [];
1313
const notices: string[] = [];
14+
let hasRawPatch = false;
1415
for (const entry of entries) {
1516
const parsed = buildReviewParsedDiff(entry.diff, `${cacheScope}:${entry.path}`);
1617
if (parsed.kind === "raw") {
18+
hasRawPatch = true;
1719
notices.push(`${entry.path}: ${parsed.reason}`);
1820
continue;
1921
}
@@ -29,6 +31,14 @@ export function buildWorkspaceReviewDiff(
2931
});
3032
}
3133
}
34+
if (hasRawPatch) {
35+
return {
36+
kind: "raw",
37+
text: entries.map((entry) => `Repository: ${entry.path}\n${entry.diff}`).join("\n\n"),
38+
reason: notices.join("\n"),
39+
notice: null,
40+
};
41+
}
3242
if (files.length === 0 && notices.length === 0) return { kind: "empty" };
3343
return {
3444
kind: "files",

‎apps/mobile/src/state/use-workspace-repositories.ts‎

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ import { projectEnvironment } from "./projects";
66
import { useEnvironmentQuery } from "./query";
77
import { useThreadSelection } from "./use-thread-selection";
88
import { useSelectedThreadWorktree } from "./use-selected-thread-worktree";
9-
import { resolveWorkspaceGitCwd } from "./workspace-repository-selection";
9+
import {
10+
resolveWorkspaceRepositoryFilter,
11+
updateWorkspaceRepositorySelection,
12+
type WorkspaceRepositorySelection,
13+
resolveWorkspaceGitCwd,
14+
} from "./workspace-repository-selection";
1015

1116
const EMPTY_REPOSITORIES = Object.freeze([]);
1217
const selections = Atom.family((_key: string) =>
13-
Atom.make<string | null>(null).pipe(Atom.keepAlive),
18+
Atom.make<WorkspaceRepositorySelection>({ path: null, diffPath: null }).pipe(Atom.keepAlive),
1419
);
1520

1621
export function useWorkspaceRepositories() {
@@ -31,9 +36,14 @@ export function useWorkspaceRepositories() {
3136
const selectionAtom = selections(
3237
JSON.stringify([selectedThread?.environmentId, selectedThread?.id, workspaceCwd]),
3338
);
34-
const selectedPath = useAtomValue(selectionAtom);
39+
const selection = useAtomValue(selectionAtom);
40+
const selectedPath = selection.path;
3541
const selectRepository = useCallback(
36-
(path: string) => appAtomRegistry.set(selectionAtom, path),
42+
(path: string | null) =>
43+
appAtomRegistry.set(
44+
selectionAtom,
45+
updateWorkspaceRepositorySelection(appAtomRegistry.get(selectionAtom), path),
46+
),
3747
[selectionAtom],
3848
);
3949
const repositories = query.data?.repositories ?? EMPTY_REPOSITORIES;
@@ -44,6 +54,7 @@ export function useWorkspaceRepositories() {
4454
);
4555
return {
4656
repositories,
57+
repositoryFilter: resolveWorkspaceRepositoryFilter(selection.diffPath, repositories),
4758
selectedPath: selectedPath ?? ".",
4859
selectRepository,
4960
selectedThreadCwd: query.error ? null : selectedThreadCwd,

‎apps/mobile/src/state/workspace-repository-selection.test.ts‎

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
import { describe, expect, it } from "vite-plus/test";
2-
import { resolveWorkspaceGitCwd } from "./workspace-repository-selection";
2+
import {
3+
resolveWorkspaceRepositoryFilter,
4+
updateWorkspaceRepositorySelection,
5+
resolveWorkspaceGitCwd,
6+
} from "./workspace-repository-selection";
37

48
describe("workspace Git target", () => {
59
const repositories = [
@@ -20,4 +24,35 @@ describe("workspace Git target", () => {
2024
]),
2125
).toBeNull();
2226
});
27+
it("keeps review and Git on a specific repository while All preserves the Git target", () => {
28+
const child = updateWorkspaceRepositorySelection(
29+
{ path: null, diffPath: null },
30+
"projects/api",
31+
);
32+
expect(resolveWorkspaceGitCwd("/workspace", child.path, repositories)).toBe(
33+
"/workspace/projects/api",
34+
);
35+
expect(resolveWorkspaceRepositoryFilter(child.diffPath, repositories)).toBe("projects/api");
36+
const all = updateWorkspaceRepositorySelection(child, null);
37+
expect(resolveWorkspaceRepositoryFilter(all.diffPath, repositories)).toBeNull();
38+
expect(resolveWorkspaceGitCwd("/workspace", all.path, repositories)).toBe(
39+
"/workspace/projects/api",
40+
);
41+
const root = updateWorkspaceRepositorySelection(all, ".");
42+
expect(resolveWorkspaceRepositoryFilter(root.diffPath, repositories)).toBe(".");
43+
expect(resolveWorkspaceGitCwd("/workspace", root.path, repositories)).toBe("/workspace");
44+
});
45+
it("falls back to All for unavailable review targets without redirecting Git actions", () => {
46+
const selected = updateWorkspaceRepositorySelection(
47+
{ path: null, diffPath: null },
48+
"projects/api",
49+
);
50+
for (const available of [
51+
repositories.slice(0, 1),
52+
repositories.map((repo) => ({ ...repo, available: repo.path === "." })),
53+
]) {
54+
expect(resolveWorkspaceRepositoryFilter(selected.diffPath, available)).toBeNull();
55+
expect(resolveWorkspaceGitCwd("/workspace", selected.path, available)).toBeNull();
56+
}
57+
});
2358
});

‎apps/mobile/src/state/workspace-repository-selection.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,24 @@ export function resolveWorkspaceGitCwd(
1111
?.cwd ?? null
1212
);
1313
}
14+
15+
export interface WorkspaceRepositorySelection {
16+
readonly path: string | null;
17+
readonly diffPath: string | null;
18+
}
19+
20+
export function updateWorkspaceRepositorySelection(
21+
current: WorkspaceRepositorySelection,
22+
path: string | null,
23+
): WorkspaceRepositorySelection {
24+
return { path: path ?? current.path, diffPath: path };
25+
}
26+
27+
export function resolveWorkspaceRepositoryFilter(
28+
selectedPath: string | null,
29+
repositories: readonly Pick<WorkspaceRepository, "path" | "available">[],
30+
): string | null {
31+
return repositories.some((repository) => repository.path === selectedPath && repository.available)
32+
? selectedPath
33+
: null;
34+
}

‎apps/server/src/pullRequest/PullRequestService.test.ts‎

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4667,6 +4667,7 @@ for (const invalid of ["thread", "path", "unavailable", "identity", "host"] as c
46674667
it.effect("keeps scoped summary, activity, diff and stats reads in their checkout", () =>
46684668
Effect.gen(function* () {
46694669
const reads: string[] = [];
4670+
const statsRequests: number[][] = [];
46704671
const service = yield* makeService({
46714672
projects: [project({ id: "p1", title: "wrapper", workspaceRoot: "/wrapper" })],
46724673
threads: [scopedThread, { ...scopedThread, id: "t2" as ThreadId, worktreePath: "/other" }],
@@ -4700,16 +4701,17 @@ it.effect("keeps scoped summary, activity, diff and stats reads in their checkou
47004701
reads.push(`diff:${cwd}`);
47014702
return Effect.succeed({ patch: cwd, truncated: false, nextCursor: null });
47024703
},
4703-
listChangeRequestStats: ({ cwd }) => {
4704+
listChangeRequestStats: ({ cwd, changeRequests }) => {
4705+
statsRequests.push(changeRequests.map((ref) => ref.number));
47044706
reads.push(`stats:${cwd}`);
4705-
return Effect.succeed([
4706-
{
4707+
return Effect.succeed(
4708+
changeRequests.map(({ number }) => ({
47074709
repository: "org/app",
4708-
number: 1,
4710+
number,
47094711
additions: cwd === "/task/projects/app" ? 10 : 20,
47104712
deletions: 0,
4711-
},
4712-
]);
4713+
})),
4714+
);
47134715
},
47144716
}),
47154717
],
@@ -4726,15 +4728,18 @@ it.effect("keeps scoped summary, activity, diff and stats reads in their checkou
47264728
yield* service.diff(ref);
47274729
yield* service.diff(ref);
47284730
}
4729-
const stats = yield* service.listStats({ refs });
4731+
const statsRefs = [...refs, { ...scopedRef, number: 2 }];
4732+
const stats = yield* service.listStats({ refs: statsRefs });
47304733
assert.deepStrictEqual(
47314734
stats.stats.map((stat) => [stat.workspace?.threadId, stat.additions]),
47324735
[
4736+
["t1" as ThreadId, 10],
47334737
["t1" as ThreadId, 10],
47344738
["t2" as ThreadId, 20],
47354739
],
47364740
);
4737-
yield* service.listStats({ refs });
4741+
yield* service.listStats({ refs: statsRefs });
4742+
assert.deepStrictEqual(statsRequests, [[1, 2], [1]]);
47384743
for (const kind of ["summary", "activity", "diff", "stats"])
47394744
assert.deepStrictEqual(
47404745
reads.filter((read) => read.startsWith(`${kind}:`)),

‎apps/server/src/pullRequest/PullRequestService.ts‎

Lines changed: 61 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -2161,33 +2161,69 @@ export const make = Effect.gen(function* () {
21612161
if (input.refs.length === 0) return { stats: [] };
21622162
const scoped = input.refs.filter((ref) => ref.workspace !== undefined);
21632163
if (scoped.length > 0) {
2164+
const byScope = new Map<string, typeof scoped>();
2165+
for (const ref of scoped) {
2166+
const key = [
2167+
ref.projectId,
2168+
ref.workspace?.threadId,
2169+
ref.workspace?.repositoryPath,
2170+
ref.host ?? "",
2171+
ref.repository.trim().toLowerCase(),
2172+
].join("\0");
2173+
const group = byScope.get(key);
2174+
if (group) group.push(ref);
2175+
else byScope.set(key, [ref]);
2176+
}
2177+
const resolved = yield* Effect.forEach(
2178+
[...byScope.values()],
2179+
Effect.fn(function* (refs) {
2180+
const project = yield* requireProject(refs[0]!).pipe(
2181+
Effect.orElseSucceed(() => undefined),
2182+
);
2183+
return project?.api.listChangeRequestStats ? [{ project, refs }] : [];
2184+
}),
2185+
{ concurrency: REPOSITORY_CONCURRENCY },
2186+
);
2187+
const byCheckout = new Map<string, (typeof resolved)[number]>();
2188+
for (const entry of resolved.flat()) {
2189+
const key = `${entry.project.host}\0${entry.project.project.workspaceRoot}`;
2190+
const group = byCheckout.get(key);
2191+
if (group) group.push(entry);
2192+
else byCheckout.set(key, [entry]);
2193+
}
21642194
const scopedStats = yield* Effect.forEach(
2165-
scoped,
2166-
Effect.fn(function* (ref) {
2167-
const project = yield* requireProject(ref).pipe(Effect.orElseSucceed(() => undefined));
2168-
if (!project?.api.listChangeRequestStats) return [];
2169-
return yield* project.api
2170-
.listChangeRequestStats({
2171-
cwd: project.project.workspaceRoot,
2172-
host: project.host,
2173-
changeRequests: [{ repository: project.repository, number: ref.number }],
2174-
})
2175-
.pipe(
2176-
Effect.map((stats) =>
2177-
stats
2178-
.filter(
2179-
(stat) =>
2180-
stat.number === ref.number &&
2181-
stat.repository.toLowerCase() === project.repository.toLowerCase(),
2182-
)
2183-
.map((stat) => ({
2184-
...stat,
2185-
projectId: ref.projectId,
2186-
workspace: ref.workspace,
2187-
})),
2195+
[...byCheckout.values()],
2196+
Effect.fn(function* (entries) {
2197+
const first = entries[0]!;
2198+
const readStats = first.project.api.listChangeRequestStats;
2199+
if (!readStats) return [];
2200+
return yield* readStats({
2201+
cwd: first.project.project.workspaceRoot,
2202+
host: first.project.host,
2203+
changeRequests: entries.flatMap(({ project, refs }) =>
2204+
refs.map((ref) => ({
2205+
repository: project.repository,
2206+
number: ref.number,
2207+
})),
2208+
),
2209+
}).pipe(
2210+
Effect.map((stats) =>
2211+
stats.flatMap((stat) =>
2212+
entries.flatMap(({ project, refs }) =>
2213+
stat.repository.toLowerCase() === project.repository.toLowerCase()
2214+
? refs
2215+
.filter((ref) => ref.number === stat.number)
2216+
.map((ref) => ({
2217+
...stat,
2218+
projectId: ref.projectId,
2219+
workspace: ref.workspace,
2220+
}))
2221+
: [],
2222+
),
21882223
),
2189-
Effect.orElseSucceed(() => []),
2190-
);
2224+
),
2225+
Effect.orElseSucceed(() => []),
2226+
);
21912227
}),
21922228
{ concurrency: REPOSITORY_CONCURRENCY },
21932229
);

0 commit comments

Comments
 (0)