Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions apps/mobile/src/features/pull-requests/PullRequestsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,15 @@ export function PullRequestsScreen(
onPress={() => void result.loadMore(true)}
/>
</View>
) : result.hasMore ? (
// A filtered page can be too short to scroll, which leaves nothing to reach the end of.
<View className="items-center py-4">
<Button
label="Load more"
accessibilityLabel="Load more pull requests"
onPress={() => void result.loadMore()}
/>
</View>
) : undefined
}
ListEmptyComponent={
Expand Down
85 changes: 85 additions & 0 deletions apps/server/src/pullRequest/PullRequestService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3047,6 +3047,91 @@ it.effect("refuses a way of updating a branch that the host or the viewer does n
}),
);

it.effect("keeps reading a host's thinned-out slices until a filtered page is full", () =>
Effect.gen(function* () {
const reads: Array<number> = [];
const rows = Array.from({ length: 40 }, (_, index) => ({
...changeRequest(index + 1, `2026-07-${String(40 - index).padStart(2, "0")}T00:00:00Z`),
labels: index % 2 === 0 ? [{ name: "bug", color: null }] : [],
}));
const service = yield* makeService({
projects: [
project({
id: "p1",
title: "on gitlab",
workspaceRoot: "/a",
repository: "group/project",
provider: SourceControlProviderKind.make("gitlab"),
}),
],
providers: [
fakeProvider(SourceControlProviderKind.make("gitlab"), {
listChangeRequests: (input) => {
const delivered = input.cursor?.delivered ?? 0;
reads.push(delivered);
return Effect.succeed({
items: rows.slice(delivered, delivered + input.limit),
truncated: delivered + input.limit < rows.length,
continues: true,
});
},
}),
],
});

const bugs = yield* service.list({ state: "open", limit: 4, filters: { labels: [["bug"]] } });
assert.deepStrictEqual(
bugs.entries.map((entry) => entry.number),
[1, 3, 5, 7],
);
assert.deepStrictEqual(reads, [0, 4]);

reads.length = 0;
const none = yield* service.list({
state: "open",
limit: 4,
filters: { labels: [["missing"]] },
});
assert.deepStrictEqual(none.entries, []);
assert.strictEqual(reads.length, 5);
assert.match(Object.values(none.nextCursors)[0] ?? "", /\|20\|/);
}),
);

it.effect("hands a host's own resume position back on the next read", () =>
Effect.gen(function* () {
const resumed: Array<string | undefined> = [];
const service = yield* makeService({
projects: [
project({
id: "p1",
title: "on gitlab",
workspaceRoot: "/a",
repository: "group/project",
provider: SourceControlProviderKind.make("gitlab"),
}),
],
providers: [
fakeProvider(SourceControlProviderKind.make("gitlab"), {
listChangeRequests: (input) => {
resumed.push(input.cursor?.resumeAfter);
return Effect.succeed({
items: [changeRequest(resumed.length, "2026-07-02T00:00:00Z")],
truncated: true,
continues: true,
resumeAfter: `2026-07-01T00:00:00.000Z~${resumed.length}`,
});
},
}),
],
});

const first = yield* service.list({ state: "open", limit: 1 });
yield* service.list({ state: "open", limit: 1, cursors: first.nextCursors });
assert.deepStrictEqual(resumed, [undefined, "2026-07-01T00:00:00.000Z~1"]);
}),
);

it.effect("judges the review filter only on a host that summarises its reviews", () =>
Effect.gen(function* () {
const service = yield* makeService({
Expand Down
52 changes: 46 additions & 6 deletions apps/server/src/pullRequest/PullRequestService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,7 @@ function supersedesReading(current: PullRequestReading | undefined, next: PullRe
const DEFAULT_REPOSITORY_LIST_LIMIT = 99;
const REPOSITORY_CONCURRENCY = 12;
const REPOSITORY_SEARCH_CHUNK = 100;
const FILTERED_LIST_READS = 5;

/**
* Every read leaves the process — a CLI per repository, against hosts whose limits are low
Expand Down Expand Up @@ -341,7 +342,7 @@ interface ListCursor extends ProviderListCursor {
}

const LIST_CURSOR_PATTERN =
/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2}))\|(\d{1,9})\|(\d{1,9}(?:,\d{1,9})*)?$/;
/^(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d{1,9})?(?:Z|[+-]\d{2}:\d{2}))\|(\d{1,9})\|(\d{1,9}(?:,\d{1,9})*)?(?:\|([\w.:~+-]{1,128}))?$/;

function parseListCursor(raw: string): ListCursor | null {
const match = LIST_CURSOR_PATTERN.exec(raw);
Expand All @@ -351,6 +352,7 @@ function parseListCursor(raw: string): ListCursor | null {
updatedBefore: match[1]!,
delivered: Number(match[2]),
seenAt: seenAt === undefined ? [] : seenAt.split(",").map(Number),
...(match[4] === undefined ? {} : { resumeAfter: match[4] }),
};
}

Expand All @@ -368,27 +370,30 @@ function nextListCursor(
delivered: ReadonlyArray<ProviderChangeRequest>,
/** A provider may consume malformed offset-paged rows that never appear in `delivered`. */
cursorAdvance = delivered.length,
resumeAfter?: string,
): string | null {
if (fetched.length === 0) return null;
// Taken from what the host answered rather than from what survived de-duplication: a slice can
// be entirely rows already sent — a hundred change requests touched in the same second is one
// repository's boring afternoon — and reading "nothing new" as "nothing left" would end the
// walk on the instant it was stuck on, with everything older unreachable for good.
const oldest = fetched.reduce((left, right) => (right.updatedAt < left.updatedAt ? right : left));
return listCursorAt(previous, oldest.updatedAt, fetched, cursorAdvance);
return listCursorAt(previous, oldest.updatedAt, fetched, cursorAdvance, resumeAfter);
}

function listCursorAt(
previous: ListCursor | undefined,
boundary: string,
fetched: ReadonlyArray<ProviderChangeRequest>,
deliveredCount: number,
resumeAfter?: string,
): string {
const seenAt = new Set([
...(previous?.updatedBefore === boundary ? previous.seenAt : []),
...fetched.filter((item) => item.updatedAt === boundary).map((item) => item.number),
]);
return `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${[...seenAt].join(",")}`;
const cursor = `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${[...seenAt].join(",")}`;
return resumeAfter === undefined ? cursor : `${cursor}|${resumeAfter}`;
}

function isProviderUnusable(error: PullRequestProviderError): boolean {
Expand Down Expand Up @@ -1165,11 +1170,13 @@ export const make = Effect.gen(function* () {
const cursorOf = (project: SupportedProject): ListCursor | undefined =>
continuation?.get(project.cursorKey);

const readRepository = (project: SupportedProject): Effect.Effect<RepositoryBatch> => {
const readSlice = (
project: SupportedProject,
cursor: ListCursor | undefined,
): Effect.Effect<RepositoryBatch> => {
{
const viewer = viewers[project.host]!;
const key = project.cursorKey;
const cursor = cursorOf(project);
return project.api
.listChangeRequests({
cwd: project.project.workspaceRoot,
Expand All @@ -1188,6 +1195,7 @@ export const make = Effect.gen(function* () {
updatedBefore: cursor.updatedBefore,
delivered: cursor.delivered,
seenAt: cursor.seenAt,
resumeAfter: cursor.resumeAfter,
},
}),
})
Expand All @@ -1214,7 +1222,13 @@ export const make = Effect.gen(function* () {
truncated: page.truncated,
nextCursor:
page.continues && page.truncated
? nextListCursor(cursor, page.items, items, page.cursorAdvance)
? nextListCursor(
cursor,
page.items,
items,
page.cursorAdvance,
page.resumeAfter,
)
: null,
};
}),
Expand All @@ -1235,6 +1249,32 @@ export const make = Effect.gen(function* () {
}
};

// Filters a host cannot apply thin out the slices it hands over, so a repository keeps
// reading until it has a page's worth or runs out. The reads are bounded: a filter that
// matches nothing would otherwise walk the repository's whole history in one request.
const readRepository = (project: SupportedProject): Effect.Effect<RepositoryBatch> => {
const fill = (batch: RepositoryBatch, reads: number): Effect.Effect<RepositoryBatch> => {
const cursor = batch.nextCursor === null ? null : parseListCursor(batch.nextCursor);
if (
input.filters === undefined ||
cursor === null ||
batch.entries.length >= limit ||
reads >= FILTERED_LIST_READS
) {
return Effect.succeed(batch);
}
return readSlice(project, cursor).pipe(
Effect.flatMap((next) =>
// A slice that failed keeps the cursor before it, so the reader can try again.
next.errors.length > 0
? Effect.succeed(batch)
: fill({ ...next, entries: [...batch.entries, ...next.entries] }, reads + 1),
),
);
};
return readSlice(project, cursorOf(project)).pipe(Effect.flatMap((batch) => fill(batch, 1)));
};

const readTogether = (
chunk: ReadonlyArray<SupportedProject>,
): Effect.Effect<ReadonlyArray<RepositoryBatch>> => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,12 @@ export interface ProviderChangeRequestPage {
* may need to count malformed raw rows it consumed too.
*/
readonly cursorAdvance?: number;
/**
* Where a host that can only page from the top carries on: an opaque position past the last
* row delivered, handed back as the cursor's `resumeAfter`. Unlike a count of rows it survives
* rows above it closing between reads.
*/
readonly resumeAfter?: string;
readonly continues: boolean;
}

Expand All @@ -160,6 +166,7 @@ export interface ProviderListCursor {
* applying their limit so a full page of tied updates does not repeat the same prefix.
*/
readonly seenAt?: ReadonlyArray<number> | undefined;
readonly resumeAfter?: string | undefined;
}

export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,20 +448,87 @@ describe("GitCafePullRequestProvider", () => {
}).pipe(Effect.provide(server.layer));
});

it.effect("leaves an author GitCafe has no profile for to be narrowed by login", () => {
const server = fakeGitCafe({ "GET /repos/owner/repo/pulls": { items: [pull], next: null } });
it.effect("finds a GitHub-synced author's actor on their pull requests to narrow by it", () => {
const synced = {
...pull,
number: 8,
author: { kind: "github", actorId: "act_synced", login: "Synced", avatarUrl: null },
};
const server = fakeGitCafe({
"GET /repos/owner/repo/pulls": (request: SentRequest) =>
request.query.get("authors") === '["act_synced"]'
? { items: [synced], next: null }
: request.query.get("after") === null
? { items: [pull], next: "o1" }
: { items: [synced], next: null },
});
return Effect.gen(function* () {
const provider = yield* GitCafePullRequestProvider.make;
const page = yield* provider.listChangeRequests({
...target,
state: "open",
involvement: "all",
viewer: "alice",
limit: 10,
filters: { author: "synced-only" },
});
assert.strictEqual(page.items.length, 1);
assert.isNull(server.sent.at(-1)!.query.get("authors"));
const list = (author: string) =>
provider.listChangeRequests({
...target,
state: "open",
involvement: "all",
viewer: "alice",
limit: 10,
filters: { author },
});
const page = yield* list("synced");
assert.deepStrictEqual(
page.items.map((item) => item.number),
[8],
);
assert.strictEqual(server.sent.at(-1)!.query.get("authors"), '["act_synced"]');
// Once seen, the actor is narrowed by straight away.
const sentBefore = server.sent.length;
yield* list("synced");
assert.deepStrictEqual(
server.sent.slice(sentBefore).map((request) => request.query.get("authors")),
[null, '["act_synced"]'],
);
// An author on none of the pages it reads gets nothing rather than unnarrowed rows a
// continuation would count against a different listing.
const missing = yield* list("nobody");
assert.deepStrictEqual(missing, { items: [], truncated: false, continues: false });
}).pipe(Effect.provide(server.layer));
});

it.effect("carries a listing on after the last row delivered when rows above it close", () => {
const created = (number: number) => `2026-09-12T12:00:0${9 - number}Z`;
let pulls = [1, 2, 3, 4, 5].map((number) => ({ ...pull, number, createdAt: created(number) }));
const server = fakeGitCafe({
"GET /repos/owner/repo/pulls": (request: SentRequest) => {
const offset = Number(request.query.get("after")?.slice(1) ?? 0);
const end = offset + Number(request.query.get("limit"));
return { items: pulls.slice(offset, end), next: end < pulls.length ? `o${end}` : null };
},
});
return Effect.gen(function* () {
const provider = yield* GitCafePullRequestProvider.make;
const list = (cursor?: { readonly resumeAfter?: string }) =>
provider.listChangeRequests({
...target,
state: "open",
involvement: "all",
viewer: "alice",
limit: 2,
...(cursor === undefined
? {}
: { cursor: { updatedBefore: timestamp, delivered: 2, ...cursor } }),
});
const first = yield* list();
assert.deepStrictEqual(
first.items.map((item) => item.number),
[1, 2],
);
pulls = pulls.filter((candidate) => candidate.number !== 1);
const next = yield* list({ resumeAfter: first.resumeAfter! });
assert.deepStrictEqual(
next.items.map((item) => item.number),
[3, 4],
);
assert.isTrue(next.truncated);
assert.isTrue(next.continues);
}).pipe(Effect.provide(server.layer));
});

Expand Down
Loading
Loading