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): seed field label records and bound inbound label reads

Tin232168f3e4a5
diff --git a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
index 87b8fc0a7..6388b3494 100644
--- a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
+++ b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
@@ -1,3 +1,4 @@
+import { confirmedFieldLabels } from "../../plugins/sync/field-labels";
import { acceptsIssue } from "../../plugins/sync/rules";
import { canSyncTask } from "../../plugins/sync/eligibility";
import { sameConfig } from "../../plugins/sync/same-config";
@@ -338,6 +339,7 @@ async function importSingleIssue(
state: issue.state,
createdFrom: "gitea-import",
author: issue.user?.login ?? issue.user?.username,
+ ...confirmedFieldLabels(issue.labels),
},
},
tx,
diff --git a/apps/api/src/github-integration/controllers/import-issues.ts b/apps/api/src/github-integration/controllers/import-issues.ts
index d8e418760..0577988e9 100644
--- a/apps/api/src/github-integration/controllers/import-issues.ts
+++ b/apps/api/src/github-integration/controllers/import-issues.ts
@@ -1,3 +1,4 @@
+import { confirmedFieldLabels } from "../../plugins/sync/field-labels";
import { acceptsIssue, readSyncRules } from "../../plugins/sync/rules";
import { canSyncTask } from "../../plugins/sync/eligibility";
import { importIssueLabels } from "../../plugins/sync/issue-labels";
@@ -582,6 +583,7 @@ async function importIssue(
state: issue.state.toLowerCase(),
createdFrom: "github-import",
author: issue.author?.login,
+ ...confirmedFieldLabels(issue.labels.nodes),
}),
});
return { id: created.id, result: "imported" };
diff --git a/apps/api/src/integration-sync/controllers/apply-resume.ts b/apps/api/src/integration-sync/controllers/apply-resume.ts
index eacc8246a..85339da5c 100644
--- a/apps/api/src/integration-sync/controllers/apply-resume.ts
+++ b/apps/api/src/integration-sync/controllers/apply-resume.ts
@@ -1,3 +1,4 @@
+import { confirmedFieldLabels } from "../../plugins/sync/field-labels";
import { randomUUID } from "node:crypto";
import { asc, eq } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
@@ -101,6 +102,7 @@ export async function applySyncResume(
syncFilterPaused: false,
syncResumeUncertain: false,
syncResumeLabelBaseline: review.remoteIssueLabels,
+ ...confirmedFieldLabels(review.remoteIssueLabels),
...(provider === "gitlab"
? {
lastOutboundStateSyncAt: Date.now(),
diff --git a/apps/api/src/plugins/gitea/events/task-created.ts b/apps/api/src/plugins/gitea/events/task-created.ts
index f92f46cb8..40791e62c 100644
--- a/apps/api/src/plugins/gitea/events/task-created.ts
+++ b/apps/api/src/plugins/gitea/events/task-created.ts
@@ -70,6 +70,7 @@ async function createTaskIssue(
metadata: {
state: createdIssue.state,
createdFrom: "kaneo",
+ syncFieldLabels: [],
syncInitializationPending: true,
syncCreatedText: {
title: event.title,
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-opened.ts b/apps/api/src/plugins/gitea/webhooks/issue-opened.ts
index f5dd9c996..dd88123cd 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-opened.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-opened.ts
@@ -1,3 +1,4 @@
+import { confirmedFieldLabels } from "../../sync/field-labels";
import { acceptsIssue } from "../../sync/rules";
import { importIssueLabels } from "../../sync/issue-labels";
import { canSyncTask } from "../../sync/eligibility";
@@ -151,6 +152,7 @@ export async function handleGiteaIssueOpened(
state: closed ? "closed" : "open",
createdFrom: "gitea",
author: issue.user?.login ?? issue.user?.username,
+ ...confirmedFieldLabels(issue.labels),
};
const link = await createExternalLink(
{
diff --git a/apps/api/src/plugins/github/events/task-created.ts b/apps/api/src/plugins/github/events/task-created.ts
index 61d09468b..c5a365b67 100644
--- a/apps/api/src/plugins/github/events/task-created.ts
+++ b/apps/api/src/plugins/github/events/task-created.ts
@@ -78,6 +78,7 @@ async function createTaskIssue(
metadata: {
state: createdIssue.data.state,
createdFrom: "kaneo",
+ syncFieldLabels: [],
syncInitializationPending: true,
syncCreatedText: {
title: event.title,
diff --git a/apps/api/src/plugins/github/webhooks/issue-labeled.ts b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
index baf8d559e..2bcf2c1d8 100644
--- a/apps/api/src/plugins/github/webhooks/issue-labeled.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
@@ -64,7 +64,12 @@ export async function handleIssueLabeled(payload: IssueLabeledPayload) {
addedLabel.name.startsWith("status:");
// Other label events skip the provider read; status and priority come
// only from the issue's current labels, never from this snapshot.
- if (fieldEvent) await syncLabelFieldsFromIssue(integration, existingLink);
+ if (fieldEvent)
+ await syncLabelFieldsFromIssue(
+ integration,
+ existingLink,
+ payload.action === "labeled" ? addedLabel?.name : null,
+ );
await withIntegrationLink(
existingLink,
diff --git a/apps/api/src/plugins/github/webhooks/issue-opened.ts b/apps/api/src/plugins/github/webhooks/issue-opened.ts
index 81a2fc3df..ea0562b43 100644
--- a/apps/api/src/plugins/github/webhooks/issue-opened.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-opened.ts
@@ -14,6 +14,7 @@ import {
import { claimTaskNumber } from "../../../task/controllers/claim-task-numbers";
import type { GitHubConfig } from "../config";
import { createExternalLink, findExternalLink } from "../services/link-manager";
+import { confirmedFieldLabels } from "../../sync/field-labels";
import { findAllIntegrationsByRepo } from "../services/task-service";
import {
extractIssuePriority,
@@ -144,6 +145,7 @@ export async function handleIssueOpened(
state: closed ? "closed" : "open",
createdFrom: "github",
author: issue.user?.login,
+ ...confirmedFieldLabels(issue.labels),
},
},
tx,
diff --git a/apps/api/src/plugins/sync/changed-label-fields.test.ts b/apps/api/src/plugins/sync/changed-label-fields.test.ts
index 9d5b6b78d..9fdfc0706 100644
--- a/apps/api/src/plugins/sync/changed-label-fields.test.ts
+++ b/apps/api/src/plugins/sync/changed-label-fields.test.ts
@@ -1,24 +1,19 @@
import { expect, it } from "vite-plus/test";
import { changedLabelFields } from "./changed-label-fields";
-const link = { id: "link", metadata: null };
const task = { status: "to-do", priority: "low" };
-it("ignores labels already confirmed on the provider", () => {
- const known = ["status:to-do", "priority:low"];
- expect(changedLabelFields(link, known, known, task)).toEqual({
+it("ignores labels Kaneo already accounted for", () => {
+ const known = ["status:in-review", "priority:high"];
+ expect(changedLabelFields(known, known, task)).toEqual({
status: undefined,
priority: undefined,
});
- expect(
- changedLabelFields(link, ["status:in-review"], ["status:in-review"], task),
- ).toEqual({ status: undefined, priority: undefined });
});
-it("applies only labels added since the last confirmation", () => {
+it("applies only labels added since then, per field", () => {
expect(
changedLabelFields(
- link,
["priority:low", "status:to-do", "status:in-review"],
["priority:low", "status:to-do"],
task,
@@ -26,7 +21,6 @@ it("applies only labels added since the last confirmation", () => {
).toEqual({ status: "in-review", priority: undefined });
expect(
changedLabelFields(
- link,
["priority:high", "status:to-do"],
["priority:low", "status:to-do"],
{ status: "done", priority: "low" },
@@ -34,26 +28,12 @@ it("applies only labels added since the last confirmation", () => {
).toEqual({ status: undefined, priority: "high" });
});
-it("falls back to the task value without a confirmation", () => {
- expect(
- changedLabelFields(
- link,
- ["status:to-do", "status:in-review"],
- undefined,
- task,
- ),
- ).toEqual({ status: "in-review", priority: undefined });
-});
-
-it("keeps fields unchanged since a resume", () => {
- const resumed = {
- id: "link",
- metadata: JSON.stringify({ syncResumeLabelBaseline: ["status:to-do"] }),
- };
+it("ignores removals and values the task already has", () => {
expect(
- changedLabelFields(resumed, ["status:to-do"], undefined, {
- status: "planned",
- priority: null,
- }),
+ changedLabelFields(["status:to-do"], ["status:in-review"], task),
).toEqual({ status: undefined, priority: undefined });
+ expect(changedLabelFields([], ["status:to-do"], task)).toEqual({
+ status: undefined,
+ priority: undefined,
+ });
});
diff --git a/apps/api/src/plugins/sync/changed-label-fields.ts b/apps/api/src/plugins/sync/changed-label-fields.ts
index 5d7c869d4..20d4ee329 100644
--- a/apps/api/src/plugins/sync/changed-label-fields.ts
+++ b/apps/api/src/plugins/sync/changed-label-fields.ts
@@ -2,38 +2,24 @@ import {
extractIssuePriority,
extractIssueStatus,
} from "../github/utils/extract-priority";
-import { resumeLabelChanges } from "./resume-label-changes";
-
-export const isFieldLabel = (name: string) =>
- name.startsWith("status:") || name.startsWith("priority:");
-
-// Links synced before `known` was recorded fall back to every current label.
-const added = (labels: string[], known: string[] | undefined, prefix: string) =>
- labels.filter(
- (name) => name.startsWith(prefix) && !(known ?? []).includes(name),
- );
/**
* Status and priority set by the issue's current labels. `known` holds the
- * field labels last confirmed on the provider; only labels added since then
- * count, so echoes and redeliveries of the same labels change nothing.
+ * field labels Kaneo has accounted for; only labels added since then count,
+ * so echoes and redeliveries of the same labels change nothing.
*/
export function changedLabelFields(
- link: { id: string; metadata: string | null },
labels: string[],
- known: string[] | undefined,
+ known: string[],
task: { status: string; priority: string | null },
) {
- const resumed = resumeLabelChanges(link, labels);
- const status = resumed.statusChanged
- ? added(labels, known, "status:")
- .map((name) => extractIssueStatus([name]))
- .find((value) => value && value !== task.status)
- : undefined;
- const priority = resumed.priorityChanged
- ? added(labels, known, "priority:")
- .map((name) => extractIssuePriority([name]))
- .find((value) => value && value !== task.priority)
- : undefined;
+ const added = (prefix: string) =>
+ labels.filter((name) => name.startsWith(prefix) && !known.includes(name));
+ const status = added("status:")
+ .map((name) => extractIssueStatus([name]))
+ .find((value) => value && value !== task.status);
+ const priority = added("priority:")
+ .map((name) => extractIssuePriority([name]))
+ .find((value) => value && value !== task.priority);
return { status: status ?? undefined, priority: priority ?? undefined };
}
diff --git a/apps/api/src/plugins/sync/field-labels.ts b/apps/api/src/plugins/sync/field-labels.ts
new file mode 100644
index 000000000..9a3b56b77
--- /dev/null
+++ b/apps/api/src/plugins/sync/field-labels.ts
@@ -0,0 +1,12 @@
+import { issueLabelNames } from "./rules";
+
+export const isFieldLabel = (name: string) =>
+ name.startsWith("status:") || name.startsWith("priority:");
+
+/**
+ * Link metadata recording the status and priority labels Kaneo has accounted
+ * for on the issue. Inbound label sync applies only labels added since.
+ */
+export const confirmedFieldLabels = (labels: unknown) => ({
+ syncFieldLabels: issueLabelNames(labels).filter(isFieldLabel),
+});
diff --git a/apps/api/src/plugins/sync/replay-deferred-label-fields.ts b/apps/api/src/plugins/sync/replay-deferred-label-fields.ts
index 0718af2c1..26d52c09c 100644
--- a/apps/api/src/plugins/sync/replay-deferred-label-fields.ts
+++ b/apps/api/src/plugins/sync/replay-deferred-label-fields.ts
@@ -20,6 +20,8 @@ export function replayDeferredLabelFields() {
async function replayClaimedLabelFields() {
let degraded = false;
+ // Each read is bounded, so stopping here keeps the run inside its lease.
+ const deadline = Date.now() + 45_000;
const links = await db.query.externalLinkTable.findMany({
where: and(
sql`${externalLinkTable.resourceType} = 'issue' AND ${externalLinkTable.metadata} LIKE '%"syncLabelFieldsDeferred":"%'`,
@@ -30,8 +32,10 @@ async function replayClaimedLabelFields() {
limit: BATCH,
});
// A short batch reached the end; the next run starts from the first link.
- cursor = links.length === BATCH ? links.at(-1)!.id : undefined;
+ if (links.length < BATCH) cursor = undefined;
for (const link of links) {
+ if (Date.now() >= deadline) break;
+ if (links.length === BATCH) cursor = link.id;
const integration = link.integration;
try {
if (
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 fa158f325..9832e1f4a 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
@@ -1,5 +1,4 @@
import { randomUUID } from "node:crypto";
-import { setTimeout as delay } from "node:timers/promises";
import { eq } from "drizzle-orm";
import db from "../../database";
import { externalLinkTable, taskTable } from "../../database/schema";
@@ -9,12 +8,17 @@ import { updateExternalLink } from "../github/services/link-manager";
import { updateTaskStatus } from "../github/services/task-service";
import { withIntegrationLink } from "../github/services/with-integration-link";
import { parseLinkMetadata } from "../github/utils/parse-link-metadata";
-import { changedLabelFields, isFieldLabel } from "./changed-label-fields";
+import { changedLabelFields } from "./changed-label-fields";
+import { isFieldLabel } from "./field-labels";
import { withSyncLease } from "./lease";
import { SyncLeaseBusyError } from "./lease-busy-error";
import { providerIssue } from "./provider-issue";
import { isTransientProviderError } from "./transient-provider-error";
+// GitHub counts deliveries slower than 10s as failed; replay takes the rest.
+const BUDGET_MS = 8_000;
+const LEASE_WAIT_MS = 3_000;
+
type Integration = {
id: string;
projectId: string;
@@ -33,22 +37,12 @@ const metadataOf = (link: { id: string; metadata: string | null }) =>
source: "label_fields",
});
-async function readLabels(integration: Integration, link: Link) {
- for (let attempt = 1; ; attempt++) {
- try {
- return (await (await providerIssue(integration, link)).read()).labels;
- } catch (error) {
- if (attempt === 3 || !isTransientProviderError(error)) throw error;
- await delay(250 * attempt);
- }
- }
-}
-
async function applyLabelFields(
integration: Integration,
link: Link,
labels: string[],
deferred: unknown,
+ added: string | undefined,
) {
await withIntegrationLink(
link,
@@ -60,15 +54,11 @@ async function applyLabelFields(
});
if (!task) return;
const metadata = metadataOf(link);
+ // Links from before this record only trust the label the event added.
const known = Array.isArray(metadata.syncFieldLabels)
? metadata.syncFieldLabels.filter((name) => typeof name === "string")
- : undefined;
- const { status, priority } = changedLabelFields(
- link,
- labels,
- known,
- task,
- );
+ : labels.filter((name) => isFieldLabel(name) && name !== added);
+ const { status, priority } = changedLabelFields(labels, known, task);
await updateExternalLink(
link.id,
{
@@ -116,53 +106,99 @@ async function applyLabelFields(
);
}
+/** Reads the issue under the field-label lease; "defer" leaves it to replay. */
+async function readAndApply(
+ integration: Integration,
+ link: Link,
+ added: string | null | undefined,
+): Promise<"done" | "defer"> {
+ try {
+ return await withSyncLease(
+ `sync-field-labels:${link.id}`,
+ async () => {
+ const stored = await db.query.externalLinkTable.findFirst({
+ where: eq(externalLinkTable.id, link.id),
+ columns: { id: true, metadata: true },
+ });
+ if (!stored) return "done";
+ const metadata = metadataOf(stored);
+ if (added === null && !Array.isArray(metadata.syncFieldLabels))
+ return "done";
+ const deferred = metadata.syncLabelFieldsDeferred;
+ let labels: string[];
+ try {
+ labels = (await (await providerIssue(integration, link)).read())
+ .labels;
+ } catch (error) {
+ if (isTransientProviderError(error)) return "defer";
+ console.error("Issue labels could not be read", {
+ integrationId: integration.id,
+ linkId: link.id,
+ });
+ // Replaying a permanent failure cannot succeed.
+ if (deferred)
+ await updateExternalLink(link.id, {
+ metadata: { syncLabelFieldsDeferred: null },
+ });
+ return "done";
+ }
+ await applyLabelFields(
+ integration,
+ link,
+ labels,
+ deferred,
+ added ?? undefined,
+ );
+ return "done";
+ },
+ { maxWaitMs: LEASE_WAIT_MS },
+ );
+ } catch (error) {
+ if (error instanceof SyncLeaseBusyError) return "defer";
+ throw error;
+ }
+}
+
/**
* Applies status and priority from the issue's current labels. Webhook label
* snapshots can predate Kaneo's latest write, so applying them echoed old
- * values back (#1956). Reads under the lease outbound field-label sync holds
- * while it rewrites labels. When that lease stays busy or the provider fails
- * transiently, the scheduler replays the read instead of dropping it.
+ * values back (#1956). The lease is the one outbound field-label sync holds
+ * while it rewrites labels. One attempt runs within the delivery's budget; a
+ * busy lease, transient failure or slow provider defers the read to replay.
+ *
+ * Links from before `syncFieldLabels` existed have no record of the labels
+ * Kaneo accounted for. They apply only `added`, the field label a webhook
+ * reports as added; `null` marks a removal, which leaves them untouched.
+ * Without either, the first read only records the labels.
*/
export async function syncLabelFieldsFromIssue(
integration: Integration,
link: Link,
+ added?: string | null,
) {
- let defer = false;
- try {
- await withSyncLease(`sync-field-labels:${link.id}`, async () => {
- const stored = await db.query.externalLinkTable.findFirst({
- where: eq(externalLinkTable.id, link.id),
- columns: { id: true, metadata: true },
+ let timedOut = false;
+ let timer: NodeJS.Timeout | undefined;
+ const work = readAndApply(integration, link, added);
+ // A read that outlives the budget finishes in the background.
+ work.catch(() => {
+ if (timedOut)
+ console.error("Issue label read failed after deferral", {
+ integrationId: integration.id,
+ linkId: link.id,
});
- if (!stored) return;
- const deferred = metadataOf(stored).syncLabelFieldsDeferred;
- let labels: string[];
- try {
- labels = await readLabels(integration, link);
- } catch (error) {
- if (isTransientProviderError(error)) {
- defer = true;
- return;
- }
- console.error("Issue labels could not be read", {
- integrationId: integration.id,
- linkId: link.id,
- });
- // Replaying a permanent failure cannot succeed.
- if (deferred)
- await updateExternalLink(link.id, {
- metadata: { syncLabelFieldsDeferred: null },
- });
- return;
- }
- await applyLabelFields(integration, link, labels, deferred);
- });
- } catch (error) {
- if (!(error instanceof SyncLeaseBusyError)) throw error;
- defer = true;
+ });
+ const budget = new Promise<"defer">((resolve) => {
+ timer = setTimeout(() => {
+ timedOut = true;
+ resolve("defer");
+ }, BUDGET_MS);
+ });
+ try {
+ if ((await Promise.race([work, budget])) === "done") return;
+ } finally {
+ clearTimeout(timer);
}
- if (defer)
- await updateExternalLink(link.id, {
- metadata: { syncLabelFieldsDeferred: randomUUID() },
- });
+ await updateExternalLink(link.id, {
+ metadata: { syncLabelFieldsDeferred: randomUUID() },
+ });
}
diff --git a/apps/api/src/plugins/sync/sync-task-field-labels.ts b/apps/api/src/plugins/sync/sync-task-field-labels.ts
index c67f4bd4d..067f996dc 100644
--- a/apps/api/src/plugins/sync/sync-task-field-labels.ts
+++ b/apps/api/src/plugins/sync/sync-task-field-labels.ts
@@ -7,7 +7,7 @@ import { parseLinkMetadata } from "../github/utils/parse-link-metadata";
import type { PluginContext } from "../types";
import { createIssueWrite } from "./dispatch-issue-write";
import { canSyncTask } from "./eligibility";
-import { isFieldLabel } from "./changed-label-fields";
+import { isFieldLabel } from "./field-labels";
import type { IssueWrite } from "./issue-write";
import { taskIssueLabels } from "./issue-labels";
import { withSyncLease } from "./lease";
diff --git a/tests/api-integration/integration-label-echo-loop.test.ts b/tests/api-integration/integration-label-echo-loop.test.ts
index 75a9d1026..160fcfeea 100644
--- a/tests/api-integration/integration-label-echo-loop.test.ts
+++ b/tests/api-integration/integration-label-echo-loop.test.ts
@@ -26,6 +26,8 @@ const mocks = vi.hoisted(() => ({
deliveries: [] as Delivery[],
events: [] as Array<{ name: string; data: unknown }>,
readFailures: [] as unknown[],
+ readGate: undefined as Promise<void> | undefined,
+ onRead: undefined as (() => void) | undefined,
}));
vi.mock("../../apps/api/src/events", () => ({
publishEvent: async (name: string, data: unknown) => {
@@ -47,6 +49,7 @@ function change(action: "labeled" | "unlabeled", name: string) {
mocks.deliveries.push({ action, label: name, labels: [...mocks.labels] });
}
const issue = () => {
+ mocks.onRead?.();
const failure = mocks.readFailures.shift();
if (failure) throw failure;
return {
@@ -62,7 +65,10 @@ vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({
getVerifiedInstallationOctokit: async () => ({
rest: {
issues: {
- get: async () => ({ data: issue() }),
+ get: async () => {
+ await mocks.readGate;
+ return { data: issue() };
+ },
getLabel: async () => ({}),
removeLabel: async ({ name }: { name: string }) => {
if (mocks.labels.has(name)) change("unlabeled", name);
@@ -77,7 +83,10 @@ vi.mock("../../apps/api/src/plugins/github/utils/github-app", () => ({
}));
vi.mock("../../apps/api/src/plugins/gitea/utils/gitea-api", () => ({
createGiteaClient: () => ({
- getIssue: async () => issue(),
+ getIssue: async () => {
+ await mocks.readGate;
+ return issue();
+ },
listLabels: async () => names.map((name, id) => ({ name, id: id + 1 })),
removeLabelFromIssue: async (
_owner: string,
@@ -110,9 +119,15 @@ beforeEach(async () => {
mocks.deliveries = [];
mocks.events = [];
mocks.readFailures = [];
+ mocks.readGate = undefined;
+ mocks.onRead = undefined;
});
-async function setup(provider: "github" | "gitea", metadata = {}) {
+// Links record the field labels they start with; `{}` is a link from before.
+async function setup(
+ provider: "github" | "gitea",
+ metadata: Record<string, unknown> = { syncFieldLabels: [...mocks.labels] },
+) {
const { workspace, user } = await createWorkspaceMember();
const { project } = await createProjectFixture({ workspaceId: workspace.id });
const [task] = await db
@@ -344,28 +359,115 @@ it.each(["github", "gitea"] as const)(
);
it.each(["github", "gitea"] as const)(
- "%s retries transient read failures, then defers them",
+ "%s defers a transient read failure to replay",
async (provider) => {
const f = await setup(provider);
- const unavailable = Object.assign(new Error("unavailable"), {
- status: 503,
- });
- mocks.readFailures = [unavailable, unavailable];
+ mocks.readFailures = [
+ Object.assign(new Error("unavailable"), { status: 503 }),
+ ];
+ change("unlabeled", "status:to-do");
change("labeled", "status:in-review");
- await f.receive(mocks.deliveries.shift()!);
+ await f.receive(mocks.deliveries.pop()!);
+ expect(await f.status()).toBe("to-do");
+ await replayDeferredLabelFields();
expect(await f.status()).toBe("in-review");
- expect((await f.metadataOf()).syncLabelFieldsDeferred).toBeUndefined();
+ },
+);
- mocks.readFailures = [unavailable, unavailable, unavailable];
- change("unlabeled", "status:in-review");
- change("labeled", "status:in-progress");
+it.each(["github", "gitea"] as const)(
+ "%s replays a deferred change on a resumed link",
+ async (provider) => {
+ // Resume records the provider's labels as both baselines.
+ const f = await setup(provider, {
+ syncResumeLabelBaseline: ["status:to-do"],
+ syncFieldLabels: ["status:to-do"],
+ });
+ const lease = `sync-field-labels:${f.link.id}`;
+ await db.insert(schema.jobLeaseTable).values({
+ name: lease,
+ owner: "slow-reader",
+ expiresAt: new Date(Date.now() + 60_000),
+ });
+ change("unlabeled", "status:to-do");
+ change("labeled", "status:in-review");
+ // The webhook still advances the resume baseline from its snapshot.
await f.receive(mocks.deliveries.pop()!);
- expect(await f.status()).toBe("in-review");
+ expect((await f.metadataOf()).syncResumeLabelBaseline).toEqual([
+ "status:in-review",
+ ]);
+ await db
+ .delete(schema.jobLeaseTable)
+ .where(eq(schema.jobLeaseTable.name, lease));
await replayDeferredLabelFields();
- expect(await f.status()).toBe("in-progress");
+ expect(await f.status()).toBe("in-review");
},
+ 15_000,
);
+it("github defers a provider read that outlives the delivery budget", async () => {
+ const f = await setup("github");
+ let release!: () => void;
+ mocks.readGate = new Promise((resolve) => {
+ release = resolve;
+ });
+ change("labeled", "status:in-review");
+ const started = Date.now();
+ await f.receive(mocks.deliveries.shift()!);
+ expect(Date.now() - started).toBeLessThan(9_500);
+ expect((await f.metadataOf()).syncLabelFieldsDeferred).toEqual(
+ expect.any(String),
+ );
+ // The slow read still finishes; replay then confirms the same labels.
+ release();
+ await vi.waitFor(async () => expect(await f.status()).toBe("in-review"));
+ await replayDeferredLabelFields();
+ expect(await f.status()).toBe("in-review");
+ expect((await f.metadataOf()).syncLabelFieldsDeferred).toBeNull();
+}, 20_000);
+
+it("github applies only the event's label on links without a record", async () => {
+ mocks.labels = new Set(["status:to-do", "priority:low"]);
+ const f = await setup("github", {});
+ // An imported closed issue: done in Kaneo, status:to-do on GitHub.
+ await db
+ .update(schema.taskTable)
+ .set({ status: "done", priority: "low" })
+ .where(eq(schema.taskTable.id, f.task.id));
+ change("labeled", "priority:high");
+ await f.receive(mocks.deliveries.shift()!);
+ expect(await f.current()).toMatchObject({
+ status: "done",
+ priority: "high",
+ });
+});
+
+it("github waits for the addition when a link without a record sees a removal first", async () => {
+ const f = await setup("github", {});
+ change("unlabeled", "status:to-do");
+ change("labeled", "status:in-review");
+ await f.receive(mocks.deliveries.shift()!);
+ expect((await f.metadataOf()).syncFieldLabels).toBeUndefined();
+ await f.receive(mocks.deliveries.shift()!);
+ expect(await f.status()).toBe("in-review");
+});
+
+it("gitea records labels without applying them on links without a record", async () => {
+ const f = await setup("gitea", {});
+ await db
+ .update(schema.taskTable)
+ .set({ status: "done" })
+ .where(eq(schema.taskTable.id, f.task.id));
+ change("labeled", "one");
+ await f.receive(mocks.deliveries.shift()!);
+ expect(await f.status()).toBe("done");
+ expect((await f.metadataOf()).syncFieldLabels).toEqual(["status:to-do"]);
+
+ change("unlabeled", "status:to-do");
+ change("labeled", "status:in-review");
+ await f.receive(mocks.deliveries.pop()!);
+ expect(await f.status()).toBe("in-review");
+});
+
it("gitea still syncs ordinary labels when the issue cannot be read", async () => {
const f = await setup("gitea");
const errors = vi.spyOn(console, "error").mockImplementation(() => {});
@@ -383,3 +485,36 @@ it("gitea still syncs ordinary labels when the issue cannot be read", async () =
// Replaying a permanent failure cannot succeed, so none is queued.
expect((await f.metadataOf()).syncLabelFieldsDeferred).toBeUndefined();
});
+
+it("replay stops at its deadline and resumes on the next run", async () => {
+ const first = await setup("gitea");
+ const second = await setup("gitea");
+ const [earlier, later] = [first, second].sort((a, b) =>
+ a.link.id.localeCompare(b.link.id),
+ );
+ for (const f of [earlier!, later!])
+ await db
+ .update(schema.externalLinkTable)
+ .set({
+ metadata: JSON.stringify({
+ syncFieldLabels: ["status:to-do"],
+ syncLabelFieldsDeferred: "queued",
+ }),
+ })
+ .where(eq(schema.externalLinkTable.id, f.link.id));
+ vi.useFakeTimers({ toFake: ["Date"] });
+ try {
+ // The first read runs past the run's deadline.
+ mocks.onRead = () => {
+ mocks.onRead = undefined;
+ vi.setSystemTime(Date.now() + 46_000);
+ };
+ await replayDeferredLabelFields();
+ expect((await earlier!.metadataOf()).syncLabelFieldsDeferred).toBeNull();
+ expect((await later!.metadataOf()).syncLabelFieldsDeferred).toBe("queued");
+ await replayDeferredLabelFields();
+ expect((await later!.metadataOf()).syncLabelFieldsDeferred).toBeNull();
+ } finally {
+ vi.useRealTimers();
+ }
+});
diff --git a/tests/api-integration/provider-board-live-updates.test.ts b/tests/api-integration/provider-board-live-updates.test.ts
index cc6b16395..8925362ce 100644
--- a/tests/api-integration/provider-board-live-updates.test.ts
+++ b/tests/api-integration/provider-board-live-updates.test.ts
@@ -108,7 +108,8 @@ it.each([
resourceType: "issue",
externalId: "1",
url: "https://provider.example/issues/1",
- metadata: "{}",
+ // Links record the field labels they start with.
+ metadata: JSON.stringify({ syncFieldLabels: [] }),
});
if (kind === "label-delete")
await db.insert(schema.labelTable).values({