Skip to content

Commit fe9a4c3

Browse files
committed
fix(pull-requests): preserve errors and bound fallback reads
1 parent 8daa1b8 commit fe9a4c3

13 files changed

Lines changed: 340 additions & 85 deletions

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1327,7 +1327,7 @@ it.effect("retains an unavailable host's summary when it is the only queried hos
13271327
configured,
13281328
projectIds: projectIds?.map(String),
13291329
})),
1330-
[{ host: "github.com", configured: false, projectIds: ["p1"] }],
1330+
[{ host: "github.com", configured: false, projectIds: undefined }],
13311331
);
13321332
assert.strictEqual(result.providers[0]?.detail?.includes("GitHub"), true);
13331333
assert.deepStrictEqual(
@@ -1381,7 +1381,7 @@ it.effect(
13811381
configured,
13821382
projectIds: projectIds?.map(String),
13831383
})),
1384-
[{ host: "bitbucket.org", configured: false, projectIds: ["bitbucket"] }],
1384+
[{ host: "bitbucket.org", configured: false, projectIds: undefined }],
13851385
);
13861386
assert.deepStrictEqual(result.entries, []);
13871387
assert.deepStrictEqual(
@@ -1480,7 +1480,7 @@ it.effect("reads a repository once when several worktrees share it", () =>
14801480

14811481
assert.strictEqual(calls, 1);
14821482
assert.strictEqual(result.entries.length, 1);
1483-
assert.deepStrictEqual(result.providers[0]?.projectIds?.map(String), ["p1", "p2"]);
1483+
assert.isUndefined(result.providers[0]?.projectIds);
14841484
}),
14851485
);
14861486

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

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ interface WorkspaceProjects {
318318
{ readonly kind: SourceControlProviderKind; readonly projectCount: number }
319319
>;
320320
readonly viewerRoots: ReadonlyMap<string, ReadonlyArray<string>>;
321-
readonly projectIdsByHost: ReadonlyMap<string, ReadonlyArray<ProjectId>>;
321+
readonly refinedProjectIdsByHost: ReadonlyMap<string, ReadonlyArray<ProjectId>>;
322322
}
323323

324324
interface RepositoryBatch {
@@ -706,7 +706,7 @@ export const make = Effect.gen(function* () {
706706
{ kind: SourceControlProviderKind; projectCount: number }
707707
>();
708708
const viewerRoots = new Map<string, string[]>();
709-
const projectIdsByHost = new Map<string, ProjectId[]>();
709+
const refinedProjectIdsByHost = new Map<string, ProjectId[]>();
710710
const seen = new Set<string>();
711711
for (const project of projects) {
712712
if (filter.projectId !== undefined && project.id !== filter.projectId) continue;
@@ -731,9 +731,13 @@ export const make = Effect.gen(function* () {
731731
if (filter.host !== undefined && host !== filter.host.toLowerCase()) {
732732
continue;
733733
}
734-
const projectIds = projectIdsByHost.get(host);
735-
if (projectIds === undefined) projectIdsByHost.set(host, [project.id]);
736-
else projectIds.push(project.id);
734+
if (
735+
host !== pullRequestHostOf(identity, identity.provider as SourceControlProviderKind)
736+
) {
737+
const projectIds = refinedProjectIdsByHost.get(host);
738+
if (projectIds === undefined) refinedProjectIdsByHost.set(host, [project.id]);
739+
else projectIds.push(project.id);
740+
}
737741
const api = registry.get(kind);
738742
if (api !== null) {
739743
const roots = viewerRoots.get(host);
@@ -762,7 +766,7 @@ export const make = Effect.gen(function* () {
762766
repositoryKey === null ? null : canonicalRepositoryKey(repositoryKey.toLowerCase()),
763767
});
764768
}
765-
return { supported, unimplemented, viewerRoots, projectIdsByHost };
769+
return { supported, unimplemented, viewerRoots, refinedProjectIdsByHost };
766770
}),
767771
);
768772

@@ -1088,7 +1092,7 @@ export const make = Effect.gen(function* () {
10881092
supported: projects,
10891093
unimplemented,
10901094
viewerRoots,
1091-
projectIdsByHost,
1095+
refinedProjectIdsByHost,
10921096
} = yield* listWorkspaceProjects(input);
10931097
const projectCounts = new Map<string, number>();
10941098
for (const { host } of projects) {
@@ -1111,7 +1115,7 @@ export const make = Effect.gen(function* () {
11111115
searchesOnHost: capabilities?.search ?? false,
11121116
actions: capabilities?.actions ?? [],
11131117
projectCount: projectCounts.get(result.host) ?? 1,
1114-
projectIds: projectIdsByHost.get(result.host) ?? [],
1118+
projectIds: refinedProjectIdsByHost.get(result.host),
11151119
configured: result.viewer !== null,
11161120
detail: result.error === null ? null : providerDetail(result.error),
11171121
};
@@ -1122,7 +1126,7 @@ export const make = Effect.gen(function* () {
11221126
searchesOnHost: false,
11231127
actions: [],
11241128
projectCount,
1125-
projectIds: projectIdsByHost.get(host) ?? [],
1129+
projectIds: refinedProjectIdsByHost.get(host),
11261130
configured: false,
11271131
detail: "This host cannot be browsed here yet.",
11281132
})),

‎apps/web/src/components/pullRequest/pullRequestList.logic.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1271,6 +1271,17 @@ describe("merging the environments' own listings", () => {
12711271
});
12721272
});
12731273

1274+
it("keeps continuations from every project batch on the same environment", () => {
1275+
const merged = mergePullRequestLists([
1276+
[ENV_1, answer({ truncated: true, nextCursors: { "github.com acme/first": "cursor-1" } })],
1277+
[ENV_1, answer({ truncated: true, nextCursors: { "github.com acme/second": "cursor-2" } })],
1278+
]);
1279+
expect(merged?.nextCursors).toEqual({
1280+
[ENV_1]: { "github.com acme/first": "cursor-1", "github.com acme/second": "cursor-2" },
1281+
});
1282+
expect(merged?.truncatedEnvironments).toEqual([ENV_1]);
1283+
});
1284+
12741285
it("reads the same key whichever order the environments connected in", () => {
12751286
expect(pullRequestEnvironmentSetKey(["env-2", "env-1"])).toBe(
12761287
pullRequestEnvironmentSetKey(["env-1", "env-2"]),

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -463,7 +463,7 @@ export function mergePullRequestLists(
463463
): MergedPullRequestList | null {
464464
if (answers.length === 0) return null;
465465
const viewers: Record<string, string> = {};
466-
const truncatedEnvironments: string[] = [];
466+
const truncatedEnvironments = new Set<string>();
467467
const providers = new Map<string, PullRequestListResult["providers"][number]>();
468468
const entries: EnvironmentPullRequestEntry[] = [];
469469
const errors: EnvironmentPullRequestError[] = [];
@@ -490,9 +490,9 @@ export function mergePullRequestLists(
490490
entries.push(...answer.entries.map((entry) => ({ ...entry, environmentId })));
491491
errors.push(...answer.errors.map((error) => ({ ...error, environmentId })));
492492
truncated ||= answer.truncated;
493-
if (answer.truncated) truncatedEnvironments.push(environmentId);
493+
if (answer.truncated) truncatedEnvironments.add(environmentId);
494494
if (Object.keys(answer.nextCursors).length > 0) {
495-
nextCursors[environmentId] = answer.nextCursors;
495+
nextCursors[environmentId] = { ...nextCursors[environmentId], ...answer.nextCursors };
496496
}
497497
}
498498
return {
@@ -502,7 +502,7 @@ export function mergePullRequestLists(
502502
errors,
503503
truncated,
504504
nextCursors,
505-
truncatedEnvironments,
505+
truncatedEnvironments: [...truncatedEnvironments],
506506
};
507507
}
508508

‎apps/web/src/components/pullRequest/pullRequestProjectAssignment.browser.test.tsx‎

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,12 @@ import { createRoot, type Root } from "react-dom/client";
66
import { afterEach, beforeEach, expect, it, vi } from "vite-plus/test";
77

88
const fixture = vi.hoisted(() => ({
9-
search: { state: "open", involvement: "all", sort: "updated" },
9+
search: {
10+
state: "open",
11+
involvement: "all",
12+
sort: "updated",
13+
environmentId: undefined as string | undefined,
14+
},
1015
primaryConfigured: false,
1116
remoteConfigured: true,
1217
revision: 1,
@@ -54,6 +59,9 @@ vi.mock(import("../../state/environments"), async (original) => ({
5459
}));
5560
vi.mock(import("../../state/pullRequests"), async (original) => {
5661
const actual = await original();
62+
const { AsyncResult } = await import("effect/reactivity");
63+
const Cause = await import("effect/Cause");
64+
const { PullRequestUnavailableError } = await import("@t3tools/contracts");
5765
const { mergePullRequestLists } = await import("./pullRequestList.logic");
5866
const cache = new Map<string, ReturnType<typeof actual.usePullRequestList>>();
5967
const refresh = vi.fn();
@@ -67,15 +75,33 @@ vi.mock(import("../../state/pullRequests"), async (original) => {
6775
const key = JSON.stringify([fixture.revision, targets]);
6876
const cached = cache.get(key);
6977
if (cached) return cached;
70-
const values = targets.map(({ environmentId }) => {
78+
const values: Array<readonly [EnvironmentId, PullRequestListResult]> = [];
79+
const observations: Array<readonly [EnvironmentId, PullRequestListResult, number]> = [];
80+
let error: string | null = null;
81+
for (const { environmentId } of targets) {
7182
fixture.targets.add(environmentId);
72-
return [environmentId, answer(environmentId)] as const;
73-
});
83+
const value = answer(environmentId);
84+
if (value.providers[0]!.configured) {
85+
values.push([environmentId, value]);
86+
observations.push([environmentId, value, Date.now()]);
87+
} else {
88+
const failure = new PullRequestUnavailableError({
89+
reason: "cli-unauthenticated",
90+
provider: "bitbucket" as never,
91+
listResult: value,
92+
});
93+
const observation = actual.observeFailedPullRequestList(
94+
AsyncResult.failure(Cause.fail(failure)),
95+
)!;
96+
observations.push([environmentId, observation.value, observation.observedAt]);
97+
error ??= failure.message;
98+
}
99+
}
74100
const result = {
75101
data: mergePullRequestLists(values),
76-
error: null,
102+
error,
77103
isPending: false,
78-
observations: values.map(([id, value]) => [id, value, fixture.revision] as const),
104+
observations,
79105
refresh,
80106
};
81107
cache.set(key, result);
@@ -116,7 +142,6 @@ function answer(environmentId: EnvironmentId): PullRequestListResult {
116142
kind: "bitbucket" as never,
117143
searchesOnHost: false,
118144
projectCount: 1,
119-
projectIds: [projectId],
120145
configured,
121146
detail: configured ? null : "Bitbucket rejected the configured credentials.",
122147
},
@@ -162,6 +187,8 @@ await View.preload?.();
162187

163188
beforeEach(() => {
164189
vi.stubGlobal("IS_REACT_ACT_ENVIRONMENT", true);
190+
fixture.search.environmentId = undefined;
191+
vi.spyOn(Date, "now").mockImplementation(() => fixture.revision * 1000);
165192
fixture.primaryConfigured = false;
166193
fixture.remoteConfigured = true;
167194
fixture.revision++;
@@ -175,6 +202,7 @@ beforeEach(() => {
175202
afterEach(async () => {
176203
await act(async () => root.unmount());
177204
container.remove();
205+
vi.restoreAllMocks();
178206
});
179207

180208
it("shows the shared repository from the server that can read it", async () => {
@@ -187,6 +215,7 @@ it("shows the shared repository from the server that can read it", async () => {
187215
it("stops falling back when neither server can read the host", async () => {
188216
fixture.remoteConfigured = false;
189217
await act(async () => root.render(<View />));
218+
expect(container.textContent).toContain("Bitbucket rejected");
190219
expect(container.textContent).not.toContain("Handle reconnects without dropping pending work");
191220
expect([...fixture.targets]).toEqual(["env-1", "env-2"]);
192221
});
@@ -204,3 +233,11 @@ it("retries the preferred server on refresh after its credentials are fixed", as
204233
expect(container.textContent).toContain("Local server");
205234
expect(container.textContent).not.toContain("Remote server");
206235
});
236+
237+
it("keeps the setup error visible when a failing server is explicitly selected", async () => {
238+
fixture.search.environmentId = "env-1";
239+
await act(async () => root.render(<View />));
240+
expect(container.textContent).toContain("Bitbucket rejected");
241+
expect(container.textContent).not.toContain("No pull requests");
242+
expect([...fixture.targets]).toEqual(["env-1"]);
243+
});

0 commit comments

Comments
 (0)