Skip to content

Commit 3bd6709

Browse files
saphidcodexjuliusmarminge
authored
fix(bitbucket): advance pull request pages through tied updates (pingdotgg#18813)
Co-authored-by: SAPHID <[email protected]> Co-authored-by: GPT-6.1 Sol <[email protected]> Co-authored-by: Julius Marminge <[email protected]>
1 parent 1945d38 commit 3bd6709

4 files changed

Lines changed: 98 additions & 9 deletions

File tree

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

Lines changed: 83 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import * as SourceControlProviderRegistry from "../sourceControl/SourceControlPr
3636
import * as SourceControlRateLimit from "@t3tools/source-control-core/server/SourceControlRateLimit";
3737
import * as ForgejoCli from "@t3tools/source-control-forgejo/server/ForgejoCli";
3838
import * as ForgejoPullRequestProvider from "@t3tools/source-control-forgejo/server/ForgejoPullRequestProvider";
39+
import * as BitbucketApi from "@t3tools/source-control-bitbucket/server/BitbucketApi";
40+
import * as BitbucketPullRequestApi from "@t3tools/source-control-bitbucket/server/BitbucketPullRequestApi";
41+
import * as BitbucketPullRequestProvider from "@t3tools/source-control-bitbucket/server/BitbucketPullRequestProvider";
3942
import * as GitHubRest from "@t3tools/source-control-github/server/GitHubRest";
4043
import * as GitHubCredentials from "@t3tools/source-control-github/server/GitHubCredentials";
4144
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"
921924
}),
922925
);
923926

927+
it.effect("continues Bitbucket listings through tied timestamps and terminates", () =>
928+
Effect.gen(function* () {
929+
const boundary = "2026-10-12T00:00:00.000Z";
930+
const older = "2026-10-11T00:00:00.000Z";
931+
const rows = [1, 2, 3, 4, 5].map((id) => ({
932+
id,
933+
title: `Pull request ${id}`,
934+
state: "OPEN",
935+
created_on: older,
936+
updated_on: id <= 3 ? boundary : older,
937+
source: { branch: { name: `feat/${id}` } },
938+
destination: { branch: { name: "main" } },
939+
links: { html: { href: `https://bitbucket.org/acme/web/pull-requests/${id}` } },
940+
}));
941+
const provider = yield* BitbucketPullRequestProvider.make.pipe(
942+
Effect.provide(
943+
BitbucketPullRequestApi.layer.pipe(
944+
Layer.provide(
945+
Layer.mock(BitbucketApi.BitbucketApi)({
946+
request: ({ url }) => {
947+
if (url === "/user") {
948+
return Effect.succeed({
949+
body: JSON.stringify({ nickname: "bilal" }),
950+
truncated: false,
951+
});
952+
}
953+
const filter =
954+
new URL(url, "https://api.bitbucket.org").searchParams.get("q") ?? "";
955+
const updatedBefore = /updated_on <= (\S+)/.exec(filter)?.[1];
956+
const excluded = new Set(
957+
Array.from(filter.matchAll(/id != (\d+)/g), (match) => Number(match[1])),
958+
);
959+
const values = rows.filter(
960+
(row) =>
961+
(updatedBefore === undefined || row.updated_on <= updatedBefore) &&
962+
!excluded.has(row.id),
963+
);
964+
return Effect.succeed({ body: JSON.stringify({ values }), truncated: false });
965+
},
966+
}),
967+
),
968+
),
969+
),
970+
);
971+
const service = yield* makeService({
972+
projects: [
973+
project({
974+
id: "p1",
975+
title: "web",
976+
workspaceRoot: "/w",
977+
repository: "acme/web",
978+
provider: "bitbucket",
979+
host: "bitbucket.org",
980+
}),
981+
],
982+
providers: [provider],
983+
});
984+
let page = yield* service.list({ state: "open", limit: 2 });
985+
assert.deepStrictEqual(
986+
page.entries.map((entry) => entry.number),
987+
[1, 2],
988+
);
989+
for (const expected of [[3, 4], [5]]) {
990+
assert.isTrue(page.truncated);
991+
assert.isNotEmpty(page.nextCursors);
992+
page = yield* service.list({ state: "open", limit: 2, cursors: page.nextCursors });
993+
assert.deepStrictEqual(
994+
page.entries.map((entry) => entry.number),
995+
expected,
996+
);
997+
}
998+
assert.isFalse(page.truncated);
999+
assert.deepStrictEqual(page.nextCursors, {});
1000+
}),
1001+
);
1002+
9241003
it.effect("reads only the repositories it was asked to carry on with", () =>
9251004
Effect.gen(function* () {
9261005
const listed: string[] = [];
@@ -947,7 +1026,9 @@ it.effect("reads only the repositories it was asked to carry on with", () =>
9471026
});
9481027

9491028
assert.deepStrictEqual(listed, ["acme/web"]);
950-
assert.deepStrictEqual(cursors, [{ updatedBefore: "2026-07-02T00:00:00Z", delivered: 99 }]);
1029+
assert.deepStrictEqual(cursors, [
1030+
{ updatedBefore: "2026-07-02T00:00:00Z", delivered: 99, seenAt: [7] },
1031+
]);
9511032
assert.strictEqual(result.providers.length, 1);
9521033
}),
9531034
);
@@ -4554,7 +4635,7 @@ it.effect("carries a slice on without sending the rows it already sent", () =>
45544635
[4],
45554636
);
45564637
assert.deepStrictEqual(result.nextCursors, {
4557-
"github.com acme/web": "2026-07-02T00:00:00Z|2|3,3,4",
4638+
"github.com acme/web": "2026-07-02T00:00:00Z|2|3,4",
45584639
});
45594640
}),
45604641
);

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

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -376,11 +376,11 @@ function listCursorAt(
376376
fetched: ReadonlyArray<ProviderChangeRequest>,
377377
deliveredCount: number,
378378
): string {
379-
const seenAt = [
379+
const seenAt = new Set([
380380
...(previous?.updatedBefore === boundary ? previous.seenAt : []),
381381
...fetched.filter((item) => item.updatedAt === boundary).map((item) => item.number),
382-
];
383-
return `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${seenAt.join(",")}`;
382+
]);
383+
return `${boundary}|${(previous?.delivered ?? 0) + deliveredCount}|${[...seenAt].join(",")}`;
384384
}
385385

386386
function isProviderUnusable(error: PullRequestProviderError): boolean {
@@ -1176,7 +1176,11 @@ export const make = Effect.gen(function* () {
11761176
...(cursor === undefined
11771177
? {}
11781178
: {
1179-
cursor: { updatedBefore: cursor.updatedBefore, delivered: cursor.delivered },
1179+
cursor: {
1180+
updatedBefore: cursor.updatedBefore,
1181+
delivered: cursor.delivered,
1182+
seenAt: cursor.seenAt,
1183+
},
11801184
}),
11811185
})
11821186
.pipe(

‎packages/source-control-bitbucket/src/server/BitbucketPullRequestApi.ts‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -561,12 +561,11 @@ export const make = Effect.gen(function* () {
561561
listPullRequests: (input) =>
562562
withRepository(input.repository, (path) => {
563563
const search = input.query?.trim() ?? "";
564-
// The boundary instant is read inclusively — the rows already sent at it come back and
565-
// the caller drops them, which is what keeps their neighbours at the same instant from
566-
// being skipped.
564+
// Exclude already-sent IDs at the inclusive timestamp before applying the page limit.
567565
const predicates = [
568566
...(search.length === 0 ? [] : [searchFilter(search)]),
569567
...(input.cursor === undefined ? [] : [`updated_on <= ${input.cursor.updatedBefore}`]),
568+
...(input.cursor?.seenAt ?? []).map((number) => `id != ${number}`),
570569
];
571570
return listPage({
572571
// Reviewers are not on a listing by default, and `viewerReviewRequested` needs them.

‎packages/source-control-core/src/server/PullRequestProvider.ts‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,11 @@ export interface ProviderListCursor {
155155
*/
156156
readonly updatedBefore: string;
157157
readonly delivered: number;
158+
/**
159+
* Rows already handed over at `updatedBefore`. Timestamp-paged hosts can exclude these before
160+
* applying their limit so a full page of tied updates does not repeat the same prefix.
161+
*/
162+
readonly seenAt?: ReadonlyArray<number> | undefined;
158163
}
159164

160165
export interface ProviderBatchedChangeRequest extends ProviderChangeRequest {

0 commit comments

Comments
 (0)