Skip to content
TypeScript

fix(bitbucket): advance pull request pages through tied updates (#18813)

Alex Southwell3bd670918127
diff --git a/apps/server/src/pullRequest/PullRequestService.test.ts b/apps/server/src/pullRequest/PullRequestService.test.ts
index 5eb0ea9660f..7698f28be46 100644
--- a/apps/server/src/pullRequest/PullRequestService.test.ts
+++ b/apps/server/src/pullRequest/PullRequestService.test.ts
@@ -36,6 +36,9 @@ import * as SourceControlProviderRegistry from "../sourceControl/SourceControlPr
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
import * as ForgejoCli from "@t3tools/source-control-forgejo/server/ForgejoCli";
import * as ForgejoPullRequestProvider from "@t3tools/source-control-forgejo/server/ForgejoPullRequestProvider";
+import * as BitbucketApi from "@t3tools/source-control-bitbucket/server/BitbucketApi";
+import * as BitbucketPullRequestApi from "@t3tools/source-control-bitbucket/server/BitbucketPullRequestApi";
+import * as BitbucketPullRequestProvider from "@t3tools/source-control-bitbucket/server/BitbucketPullRequestProvider";
import * as GitHubRest from "@t3tools/source-control-github/server/GitHubRest";
import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials";
import * as GitHubPullRequestApi from "@t3tools/source-control-github/server/GitHubPullRequestApi";
@@ -921,6 +924,82 @@ it.effect("uses a provider's raw cursor advance when it consumed malformed rows"
}),
);
+it.effect("continues Bitbucket listings through tied timestamps and terminates", () =>
+ Effect.gen(function* () {
+ const boundary = "2026-10-12T00:00:00.000Z";
+ const older = "2026-10-11T00:00:00.000Z";
+ const rows = [1, 2, 3, 4, 5].map((id) => ({
+ id,
+ title: `Pull request ${id}`,
+ state: "OPEN",
+ created_on: older,
+ updated_on: id <= 3 ? boundary : older,
+ source: { branch: { name: `feat/${id}` } },
+ destination: { branch: { name: "main" } },
+ links: { html: { href: `https://bitbucket.org/acme/web/pull-requests/${id}` } },
+ }));
+ const provider = yield* BitbucketPullRequestProvider.make.pipe(
+ Effect.provide(
+ BitbucketPullRequestApi.layer.pipe(
+ Layer.provide(
+ Layer.mock(BitbucketApi.BitbucketApi)({
+ request: ({ url }) => {
+ if (url === "/user") {
+ return Effect.succeed({
+ body: JSON.stringify({ nickname: "bilal" }),
+ truncated: false,
+ });
+ }
+ const filter =
+ new URL(url, "https://api.bitbucket.org").searchParams.get("q") ?? "";
+ const updatedBefore = /updated_on <= (\S+)/.exec(filter)?.[1];
+ const excluded = new Set(
+ Array.from(filter.matchAll(/id != (\d+)/g), (match) => Number(match[1])),
+ );
+ const values = rows.filter(
+ (row) =>
+ (updatedBefore === undefined || row.updated_on <= updatedBefore) &&
+ !excluded.has(row.id),
+ );
+ return Effect.succeed({ body: JSON.stringify({ values }), truncated: false });
+ },
+ }),
+ ),
+ ),
+ ),
+ );
+ const service = yield* makeService({
+ projects: [
+ project({
+ id: "p1",
+ title: "web",
+ workspaceRoot: "/w",
+ repository: "acme/web",
+ provider: "bitbucket",
+ host: "bitbucket.org",
+ }),
+ ],
+ providers: [provider],
+ });
+ let page = yield* service.list({ state: "open", limit: 2 });
+ assert.deepStrictEqual(
+ page.entries.map((entry) => entry.number),
+ [1, 2],
+ );
+ for (const expected of [[3, 4], [5]]) {
+ assert.isTrue(page.truncated);
+ assert.isNotEmpty(page.nextCursors);
+ page = yield* service.list({ state: "open", limit: 2, cursors: page.nextCursors });
+ assert.deepStrictEqual(
+ page.entries.map((entry) => entry.number),
+ expected,
+ );
+ }
+ assert.isFalse(page.truncated);
+ assert.deepStrictEqual(page.nextCursors, {});
+ }),
+);
+
it.effect("reads only the repositories it was asked to carry on with", () =>
Effect.gen(function* () {
const listed: string[] = [];
@@ -947,7 +1026,9 @@ it.effect("reads only the repositories it was asked to carry on with", () =>
});
assert.deepStrictEqual(listed, ["acme/web"]);
- assert.deepStrictEqual(cursors, [{ updatedBefore: "2026-07-02T00:00:00Z", delivered: 99 }]);
+ assert.deepStrictEqual(cursors, [
+ { updatedBefore: "2026-07-02T00:00:00Z", delivered: 99, seenAt: [7] },
+ ]);
assert.strictEqual(result.providers.length, 1);
}),
);
@@ -4554,7 +4635,7 @@ it.effect("carries a slice on without sending the rows it already sent", () =>
[4],
);
assert.deepStrictEqual(result.nextCursors, {
- "github.com acme/web": "2026-07-02T00:00:00Z|2|3,3,4",
+ "github.com acme/web": "2026-07-02T00:00:00Z|2|3,4",
});
}),
);
diff --git a/apps/server/src/pullRequest/PullRequestService.ts b/apps/server/src/pullRequest/PullRequestService.ts
index fc2a3a143b8..ff77f99ebe4 100644
--- a/apps/server/src/pullRequest/PullRequestService.ts
+++ b/apps/server/src/pullRequest/PullRequestService.ts
@@ -376,11 +376,11 @@ function listCursorAt(
fetched: ReadonlyArray<ProviderChangeRequest>,
deliveredCount: number,
): string {
- const seenAt = [
+ 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(",")}`;
+ ]);
+ return `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${[...seenAt].join(",")}`;
}
function isProviderUnusable(error: PullRequestProviderError): boolean {
@@ -1176,7 +1176,11 @@ export const make = Effect.gen(function* () {
...(cursor === undefined
? {}
: {
- cursor: { updatedBefore: cursor.updatedBefore, delivered: cursor.delivered },
+ cursor: {
+ updatedBefore: cursor.updatedBefore,
+ delivered: cursor.delivered,
+ seenAt: cursor.seenAt,
+ },
}),
})
.pipe(
diff --git a/packages/source-control-bitbucket/src/server/BitbucketPullRequestApi.ts b/packages/source-control-bitbucket/src/server/BitbucketPullRequestApi.ts
index 4cb963ff30b..aefcbd01501 100644
--- a/packages/source-control-bitbucket/src/server/BitbucketPullRequestApi.ts
+++ b/packages/source-control-bitbucket/src/server/BitbucketPullRequestApi.ts
@@ -561,12 +561,11 @@ export const make = Effect.gen(function* () {
listPullRequests: (input) =>
withRepository(input.repository, (path) => {
const search = input.query?.trim() ?? "";
- // The boundary instant is read inclusively — the rows already sent at it come back and
- // the caller drops them, which is what keeps their neighbours at the same instant from
- // being skipped.
+ // Exclude already-sent IDs at the inclusive timestamp before applying the page limit.
const predicates = [
...(search.length === 0 ? [] : [searchFilter(search)]),
...(input.cursor === undefined ? [] : [`updated_on <= ${input.cursor.updatedBefore}`]),
+ ...(input.cursor?.seenAt ?? []).map((number) => `id != ${number}`),
];
return listPage({
// Reviewers are not on a listing by default, and `viewerReviewRequested` needs them.
diff --git a/packages/source-control-core/src/server/PullRequestProvider.ts b/packages/source-control-core/src/server/PullRequestProvider.ts
index 882bc73c115..0aadb0bbc92 100644
--- a/packages/source-control-core/src/server/PullRequestProvider.ts
+++ b/packages/source-control-core/src/server/PullRequestProvider.ts
@@ -155,6 +155,11 @@ export interface ProviderListCursor {
*/
readonly updatedBefore: string;
readonly delivered: number;
+ /**
+ * Rows already handed over at `updatedBefore`. Timestamp-paged hosts can exclude these before
+ * applying their limit so a full page of tied updates does not repeat the same prefix.
+ */
+ readonly seenAt?: ReadonlyArray<number> | undefined;
}
export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {