Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

fix(api): trust label additions queued during a running read

Tin22001ec14727
diff --git a/apps/api/src/plugins/sync/sync-label-fields-from-issue.ts b/apps/api/src/plugins/sync/sync-label-fields-from-issue.ts
index 77eb39b35..30ae8484a 100644
--- a/apps/api/src/plugins/sync/sync-label-fields-from-issue.ts
+++ b/apps/api/src/plugins/sync/sync-label-fields-from-issue.ts
@@ -62,10 +62,15 @@ async function applyLabelFields(
});
if (!task) return;
const metadata = metadataOf(link);
- // Links from before this record only trust the label the event added.
+ // Links from before this record only trust labels events added. Read the
+ // queue under this row lock: deferrals may have joined it during the read,
+ // and every entry is handled here, so clearing it below loses none.
+ const trusted = [...added, ...names(metadata.syncLabelFieldsAdded)];
const known = Array.isArray(metadata.syncFieldLabels)
? names(metadata.syncFieldLabels)
- : labels.filter((name) => isFieldLabel(name) && !added.includes(name));
+ : labels.filter(
+ (name) => isFieldLabel(name) && !trusted.includes(name),
+ );
const { status, priority } = changedLabelFields(labels, known, task);
await updateExternalLink(
link.id,
diff --git a/tests/api-integration/integration-label-echo-loop.test.ts b/tests/api-integration/integration-label-echo-loop.test.ts
index 67fd2b160..1169ff5ff 100644
--- a/tests/api-integration/integration-label-echo-loop.test.ts
+++ b/tests/api-integration/integration-label-echo-loop.test.ts
@@ -567,3 +567,35 @@ it("github shares one delivery budget across integrations", async () => {
release();
await vi.waitFor(async () => expect(await first.status()).toBe("in-review"));
}, 25_000);
+
+it("github keeps an addition deferred while another read is running", async () => {
+ const f = await setup("github", {});
+ change("labeled", "status:in-review");
+ change("labeled", "priority:high");
+ const [status, priority] = mocks.deliveries.splice(0);
+ let release!: () => void;
+ mocks.readGate = new Promise((resolve) => {
+ release = resolve;
+ });
+ // The status read stalls while holding the field-label lease.
+ const stalled = f.receive(status!);
+ await vi.waitFor(async () =>
+ expect(
+ await db.query.jobLeaseTable.findFirst({
+ where: eq(schema.jobLeaseTable.name, `sync-field-labels:${f.link.id}`),
+ }),
+ ).toBeDefined(),
+ );
+ // The priority event defers behind it and queues its label.
+ await f.receive(priority!);
+ expect((await f.metadataOf()).syncLabelFieldsAdded).toEqual([
+ "priority:high",
+ ]);
+ release();
+ await stalled;
+ await replayDeferredLabelFields();
+ expect(await f.current()).toMatchObject({
+ status: "in-review",
+ priority: "high",
+ });
+}, 20_000);