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: guard inbound sync after acquiring link locks

Tin75e460770666
diff --git a/apps/api/src/plugins/gitea/utils/gitea-api.ts b/apps/api/src/plugins/gitea/utils/gitea-api.ts
index 01d1ced5e..8eb0381dc 100644
--- a/apps/api/src/plugins/gitea/utils/gitea-api.ts
+++ b/apps/api/src/plugins/gitea/utils/gitea-api.ts
@@ -11,6 +11,7 @@ export type GiteaLabel = {
export type GiteaIssue = {
updated_at?: string;
+ content_version?: number;
id: number;
number: number;
title: string;
diff --git a/apps/api/src/plugins/github/services/with-integration-link.ts b/apps/api/src/plugins/github/services/with-integration-link.ts
index e2a97dd40..a718653f6 100644
--- a/apps/api/src/plugins/github/services/with-integration-link.ts
+++ b/apps/api/src/plugins/github/services/with-integration-link.ts
@@ -2,6 +2,7 @@ import { and, eq } from "drizzle-orm";
import { PendingEcho } from "../utils/inbound-echo";
import { updateExternalLink } from "./link-manager";
import { externalLinkTable } from "../../../database/schema";
+import { canSyncTask } from "../../sync/eligibility";
import {
withIntegrationTask,
type IntegrationDatabase,
@@ -33,6 +34,15 @@ export function withIntegrationLink<T>(
)
.for("update");
if (!lockedLink) return;
+ if (
+ !(await canSyncTask(
+ link.taskId,
+ integration.id,
+ database,
+ expectedBinding?.config,
+ ))
+ )
+ return;
try {
return await apply(database, afterCommit, lockedLink);
} catch (error) {
diff --git a/apps/api/src/plugins/sync/provider-issue.test.ts b/apps/api/src/plugins/sync/provider-issue.test.ts
new file mode 100644
index 000000000..bc9c375ec
--- /dev/null
+++ b/apps/api/src/plugins/sync/provider-issue.test.ts
@@ -0,0 +1,78 @@
+import { beforeEach, expect, it, vi } from "vite-plus/test";
+import { providerIssue } from "./provider-issue";
+
+const client = vi.hoisted(() => ({ getIssue: vi.fn(), updateIssue: vi.fn() }));
+vi.mock("../gitea/utils/gitea-api", () => ({
+ createGiteaClient: () => client,
+}));
+
+beforeEach(() => {
+ vi.resetAllMocks();
+ client.updateIssue.mockResolvedValue({
+ title: "Chosen title",
+ body: "Chosen body",
+ state: "open",
+ });
+});
+
+it.each([0, 42, undefined])(
+ "uses the observed Gitea content version %s when resuming",
+ async (version) => {
+ client.getIssue.mockResolvedValue({
+ title: "Remote title",
+ body: "Remote body",
+ state: "open",
+ content_version: version,
+ });
+ const access = await providerIssue(
+ {
+ type: "gitea",
+ config: JSON.stringify({
+ repositoryOwner: "team",
+ repositoryName: "repo",
+ }),
+ },
+ { externalId: "7", taskId: "task" },
+ );
+ expect((await access.read()).contentVersion).toBe(version ?? null);
+ await access.write({
+ title: "Chosen title",
+ description: "Chosen body",
+ state: "open",
+ });
+ const payload = client.updateIssue.mock.calls[0]![3];
+ if (version === undefined)
+ expect(payload).not.toHaveProperty("content_version");
+ else expect(payload.content_version).toBe(version);
+ },
+);
+
+it("propagates a Gitea version conflict without retrying an unconditional write", async () => {
+ client.getIssue.mockResolvedValue({
+ title: "Remote title",
+ body: "Remote body",
+ state: "open",
+ content_version: 2,
+ });
+ client.updateIssue.mockRejectedValue(new Error("Test version conflict"));
+ const access = await providerIssue(
+ {
+ type: "gitea",
+ config: JSON.stringify({
+ repositoryOwner: "team",
+ repositoryName: "repo",
+ }),
+ },
+ { externalId: "7", taskId: "task" },
+ );
+ await access.read();
+ await expect(
+ access.write({
+ title: "Chosen title",
+ description: "Chosen body",
+ state: "open",
+ }),
+ ).rejects.toThrow("Test version conflict");
+ expect(client.updateIssue).toHaveBeenCalledOnce();
+ expect(client.updateIssue.mock.calls[0]![3].content_version).toBe(2);
+});
diff --git a/apps/api/src/plugins/sync/provider-issue.ts b/apps/api/src/plugins/sync/provider-issue.ts
index 6f73c9eca..0e192fb57 100644
--- a/apps/api/src/plugins/sync/provider-issue.ts
+++ b/apps/api/src/plugins/sync/provider-issue.ts
@@ -31,6 +31,7 @@ export async function providerIssue(
description?: string | null;
state: string;
updated_at?: string;
+ content_version?: number;
}) => ({
title: issue.title,
description: formatTaskDescriptionFromIssue(
@@ -39,17 +40,26 @@ export async function providerIssue(
),
state: issue.state === "closed" ? ("closed" as const) : ("open" as const),
updatedAt: issue.updated_at ?? null,
+ contentVersion: issue.content_version ?? null,
});
if (integration.type === "gitea") {
const client = createGiteaClient(config as GiteaConfig);
+ let contentVersion: number | undefined;
return {
- read: async () => normalize(await client.getIssue(owner, repo, number)),
+ read: async () => {
+ const issue = await client.getIssue(owner, repo, number);
+ contentVersion = issue.content_version;
+ return normalize(issue);
+ },
write: async (values: IssueValues) =>
normalize(
await client.updateIssue(owner, repo, number, {
title: values.title,
body: formatIssueBody(values.description, link.taskId),
state: values.state,
+ ...(contentVersion === undefined
+ ? {}
+ : { content_version: contentVersion }),
}),
),
};
diff --git a/apps/docs/core/integrations/index.mdx b/apps/docs/core/integrations/index.mdx
index e6212882f..1a83f6fc1 100644
--- a/apps/docs/core/integrations/index.mdx
+++ b/apps/docs/core/integrations/index.mdx
@@ -30,6 +30,8 @@ Enable **Advanced settings** in **Account → Preferences** or **Workspace → G
When a linked task loses its qualifying labels, synchronization pauses in both directions. Both records and their link remain. Reapplying the label does not overwrite either side: choose **Review and resume**, compare the current title, description, and open/closed state, then keep the Kaneo or repository values. Historical comments are not replayed. Missing or deleted workspace labels keep the filter closed until you repair the rule.
+Kaneo rereads the issue when you resume and rejects a changed comparison. Gitea's content version, when available, also protects the body write from concurrent edits. Provider APIs do not offer an atomic version check for every issue field, so an edit made after the final read can still conflict with a resume write. Avoid editing the repository issue while resuming it. A rejected provider write leaves synchronization paused for another review.
+
An issue creation request already sent to a repository may finish after its task stops matching. Kaneo keeps the returned issue link paused and stops further updates. Local edits remain available while the repository responds.
Newly matching tasks without an issue link are exported in the background when rules or task labels change. Use **Export matching tasks** to retry tasks that still have no issue link after a failed export. An inactive integration does not export or resume tasks. Existing installations retain unrestricted sync until a rule is saved.
diff --git a/tests/api-integration/integration-task-ownership.test.ts b/tests/api-integration/integration-task-ownership.test.ts
index af2adabeb..559ad46f3 100644
--- a/tests/api-integration/integration-task-ownership.test.ts
+++ b/tests/api-integration/integration-task-ownership.test.ts
@@ -1,4 +1,5 @@
import { eq } from "drizzle-orm";
+import * as eligibility from "../../apps/api/src/plugins/sync/eligibility";
import {
afterEach,
beforeEach,
@@ -659,3 +660,66 @@ it.each(["gitea", "gitlab"])(
]);
},
);
+
+it("does not apply a webhook after its link is paused while waiting for the link lock", async () => {
+ const f = await setup();
+ let release!: () => void;
+ let locked!: () => void;
+ let checked!: () => void;
+ const gate = new Promise<void>((resolve) => {
+ release = resolve;
+ });
+ const acquired = new Promise<void>((resolve) => {
+ locked = resolve;
+ });
+ const eligibilityChecked = new Promise<void>((resolve) => {
+ checked = resolve;
+ });
+ const original = eligibility.canSyncTask;
+ const guard = vi
+ .spyOn(eligibility, "canSyncTask")
+ .mockImplementation(async (...args) => {
+ const result = await original(...args);
+ checked();
+ return result;
+ });
+ const pause = db.transaction(async (tx) => {
+ await tx
+ .select()
+ .from(schema.externalLinkTable)
+ .where(eq(schema.externalLinkTable.id, f.link.id))
+ .for("update");
+ await tx
+ .update(schema.externalLinkTable)
+ .set({ metadata: JSON.stringify({ syncFilterPaused: true }) })
+ .where(eq(schema.externalLinkTable.id, f.link.id));
+ locked();
+ await gate;
+ });
+ await acquired;
+ const webhook = handleGiteaIssueEdited(
+ {
+ action: "edited",
+ issue: remoteIssue,
+ repository,
+ changes: {
+ title: { from: "Private title" },
+ body: { from: "Private description" },
+ },
+ },
+ f.integration.id,
+ );
+ try {
+ await eligibilityChecked;
+ release();
+ await pause;
+ await webhook;
+ expect(guard).toHaveBeenCalledTimes(2);
+ await expectPrivateTask(f.task.id);
+ } finally {
+ release();
+ await pause;
+ await webhook;
+ guard.mockRestore();
+ }
+});