⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
fix(integrations): converge initial state and finish imported history
Tin269b82fa53c9
diff --git a/apps/api/src/github-integration/controllers/import-issues.ts b/apps/api/src/github-integration/controllers/import-issues.tsindex 70b213da9..32973a0b0 100644--- a/apps/api/src/github-integration/controllers/import-issues.ts+++ b/apps/api/src/github-integration/controllers/import-issues.ts@@ -347,6 +347,7 @@ async function applyPage(state.currentIssue = {number: issue.number,taskId: task.id,+ isNewTask: task.result === "imported",labelCursor: cursor(issue.labels.pageInfo, null),commentCursor: cursor(issue.comments.pageInfo, null),labelsRemaining,@@ -409,11 +410,11 @@ async function applyPage(),).for("update");- if (- !task ||- !linked ||- !(await canSyncTask(current.taskId, integrationId, tx))- ) {+ const eligible =+ task && linked && (await canSyncTask(current.taskId, integrationId, tx));+ // A newly admitted issue's initial history belongs to the explicit import,+ // even when its resulting link is paused for outgoing sync.+ if (!task || !linked || (!eligible && !current.isNewTask)) {state.skipped++;finishIssue(state);return;diff --git a/apps/api/src/github-integration/import-state.ts b/apps/api/src/github-integration/import-state.tsindex 53c5f48d2..12a353d75 100644--- a/apps/api/src/github-integration/import-state.ts+++ b/apps/api/src/github-integration/import-state.ts@@ -8,6 +8,7 @@ export type GitHubImportState = {currentIssue: {number: number;taskId: string;+ isNewTask?: boolean;labelCursor: string | null;commentCursor: string | null;moreComments: boolean;diff --git a/apps/api/src/plugins/gitea/events/task-created.ts b/apps/api/src/plugins/gitea/events/task-created.tsindex 3c1787861..f92f46cb8 100644--- a/apps/api/src/plugins/gitea/events/task-created.ts+++ b/apps/api/src/plugins/gitea/events/task-created.ts@@ -108,10 +108,17 @@ async function createTaskIssue(: { body: formatIssueBody(value, event.taskId) },))?.updated_at,- close: () =>- client.updateIssue(repositoryOwner, repositoryName, issueNumber, {- state: "closed",- }),+ state: async (value) =>+ (+ await client.updateIssue(+ repositoryOwner,+ repositoryName,+ issueNumber,+ {+ state: value === "closed" ? "closed" : "open",+ },+ )+ )?.updated_at,labels: () =>syncTaskFieldLabels(event.taskId,diff --git a/apps/api/src/plugins/github/events/task-created.ts b/apps/api/src/plugins/github/events/task-created.tsindex 8f3f53493..995f859c7 100644--- a/apps/api/src/plugins/github/events/task-created.ts+++ b/apps/api/src/plugins/github/events/task-created.ts@@ -126,13 +126,15 @@ async function createTaskIssue(: { body: formatIssueBody(value, event.taskId) }),}))?.data?.updated_at,- close: () =>- octokit.rest.issues.update({- owner: repositoryOwner,- repo: repositoryName,- issue_number: issueNumber,- state: "closed",- }),+ state: async (value) =>+ (+ await octokit.rest.issues.update({+ owner: repositoryOwner,+ repo: repositoryName,+ issue_number: issueNumber,+ state: value === "closed" ? "closed" : "open",+ })+ )?.data?.updated_at,labels: () =>syncTaskFieldLabels(event.taskId,diff --git a/apps/api/src/plugins/github/services/sync-latest-task-value.ts b/apps/api/src/plugins/github/services/sync-latest-task-value.tsindex b13b7fa98..138a26a67 100644--- a/apps/api/src/plugins/github/services/sync-latest-task-value.ts+++ b/apps/api/src/plugins/github/services/sync-latest-task-value.ts@@ -184,7 +184,7 @@ export async function syncLatestTaskValue(identity,)) === true)- return;+ return true;}const task = await db.query.taskTable.findFirst({where: linkedTaskScope(taskId, projectId),@@ -205,7 +205,7 @@ export async function syncLatestTaskValue(: field === "title"? task.title: task.description || "";- if (current === value) return;+ if (current === value) return true;if (attempts >= 3) {if (binding.integration)await deferTaskSync({ id: link.id, taskId }, binding.integration, [diff --git a/apps/api/src/plugins/gitlab/events/task-created.ts b/apps/api/src/plugins/gitlab/events/task-created.tsindex 15e1585fa..5787fc9a7 100644--- a/apps/api/src/plugins/gitlab/events/task-created.ts+++ b/apps/api/src/plugins/gitlab/events/task-created.ts@@ -101,10 +101,12 @@ async function createTaskIssue(: { description: formatIssueBody(value, event.taskId) },))?.updated_at,- close: () =>- client.updateIssue(config.projectPath, issueNumber, {- state_event: "close",- }),+ state: async (value) =>+ (+ await client.updateIssue(config.projectPath, issueNumber, {+ state_event: value === "closed" ? "close" : "reopen",+ })+ )?.updated_at,labels: () =>syncTaskFieldLabels(event.taskId,diff --git a/apps/api/src/plugins/sync/initialize-task-issue.ts b/apps/api/src/plugins/sync/initialize-task-issue.tsindex 944848965..dc5201497 100644--- a/apps/api/src/plugins/sync/initialize-task-issue.ts+++ b/apps/api/src/plugins/sync/initialize-task-issue.ts@@ -1,13 +1,12 @@import { updateExternalLink } from "../github/services/link-manager";-import { isTaskInFinalState } from "../github/services/task-service";import { parseLinkMetadata } from "../github/utils/parse-link-metadata";import type { PluginContext, TaskCreatedEvent } from "../types";import { createIssueWrite } from "./dispatch-issue-write";import { initializeTaskText } from "./initialize-task-text";+import { initializeTaskState } from "./initialize-task-state";type Initialization = {syncInitializationPending?: boolean;- syncInitializedState?: boolean;syncInitializedLabels?: boolean;syncInitializedComment?: boolean;};@@ -31,7 +30,7 @@ export async function initializeTaskIssue(field: "title" | "description",value: string,) => Promise<string | undefined>;- close: () => Promise<unknown>;+ state: (value: string) => Promise<string | undefined>;labels: () => Promise<string | undefined>;comment?: () => Promise<unknown>;},@@ -45,23 +44,7 @@ export async function initializeTaskIssue({ ...link, taskId: event.taskId, integrationId: context.integrationId },JSON.stringify(context.config),);- if (!progress.syncInitializedState) {- if (- await isTaskInFinalState({- projectId: event.projectId,- status: current.status,- columnId: null,- })- ) {- await write(actions.close);- await updateExternalLink(link.id, {- metadata: { state: "closed", lastOutboundStateSyncAt: Date.now() },- });- }- await updateExternalLink(link.id, {- metadata: { syncInitializedState: true },- });- }+ await initializeTaskState(current, context, link, actions.state);if (!progress.syncInitializedLabels) {if ((await actions.labels()) === undefined)throw new Error("Issue sync scope changed");diff --git a/apps/api/src/plugins/sync/initialize-task-state.ts b/apps/api/src/plugins/sync/initialize-task-state.tsnew file mode 100644index 000000000..ac6122430--- /dev/null+++ b/apps/api/src/plugins/sync/initialize-task-state.ts@@ -0,0 +1,56 @@+import { updateExternalLink } from "../github/services/link-manager";+import { syncLatestTaskValue } from "../github/services/sync-latest-task-value";+import { isTaskInFinalState } from "../github/services/task-service";+import { parseLinkMetadata } from "../github/utils/parse-link-metadata";+import type { SyncStamp } from "../github/utils/sync-echo";+import type { PluginContext, TaskCreatedEvent } from "../types";++export async function initializeTaskState(+ event: TaskCreatedEvent,+ context: PluginContext,+ link: { id: string; metadata?: string | null },+ send: (value: string) => Promise<string | undefined>,+) {+ const progress = parseLinkMetadata<{+ state?: string;+ lastSync?: { state?: SyncStamp };+ syncInitializedState?: boolean;+ }>(link.metadata, { externalLinkId: link.id, source: "sync_initialization" });+ if (progress.syncInitializedState) return;+ const closing = await isTaskInFinalState({+ projectId: event.projectId,+ status: event.status,+ columnId: null,+ });+ // Retried closes may have reached the provider even when their response failed.+ if (+ closing ||+ progress.state === "closed" ||+ progress.lastSync?.state?.outbound?.length+ ) {+ try {+ const synchronized = await syncLatestTaskValue(+ event.taskId,+ event.projectId,+ { id: link.id, integrationId: context.integrationId },+ "state",+ closing ? "closed" : "open",+ send,+ undefined,+ { config: JSON.stringify(context.config) },+ );+ if (!synchronized) throw new Error("Issue state needs retry");+ } catch {+ console.error("Issue state initialization failed", {+ projectId: event.projectId,+ taskId: event.taskId,+ integrationId: context.integrationId,+ linkId: link.id,+ });+ throw new Error("Issue state initialization incomplete");+ }+ }+ await updateExternalLink(link.id, {+ metadata: { syncInitializedState: true },+ });+}diff --git a/tests/api-integration/github-import-bounds.test.ts b/tests/api-integration/github-import-bounds.test.tsindex 1f8e5d430..6635df1b6 100644--- a/tests/api-integration/github-import-bounds.test.ts+++ b/tests/api-integration/github-import-bounds.test.ts@@ -299,6 +299,89 @@ describe("bounded resumable GitHub import", () => {syncFilterPaused: true,});});++ it("finishes a new paused issue's comment history across import requests", async () => {+ const { member, project, integration, config } = await setup();+ const [root] = await db+ .insert(schema.labelTable)+ .values({+ workspaceId: member.workspace.id,+ name: "export",+ color: "red",+ })+ .returning();+ await db+ .update(schema.integrationTable)+ .set({+ config: JSON.stringify({+ ...config,+ syncRules: {+ outgoing: { mode: "labels", match: "any", labels: [root.id] },+ incoming: { mode: "all" },+ },+ }),+ })+ .where(eq(schema.integrationTable.id, integration.id));+ mocks.verify.mockResolvedValue({+ graphql: mocks.graphql,+ rest: { issues: { get: async () => ({ data: { labels: [] } }) } },+ });+ const comments = Array.from({ length: 85 }, (_, index) => comment(index));+ mocks.graphql.mockImplementation(+ async (query: string, vars: { cursor: string | null }) => {+ if (query.includes("query ImportIssues("))+ return issuePage([+ issue(1, {+ comments: connection(+ comments.slice(0, 20),+ true,+ "20",+ comments.length,+ ),+ }),+ ]);+ if (query.includes("query ImportIssueComments(")) {+ const start = Number(vars.cursor);+ const end = Math.min(start + 20, comments.length);+ return {+ repository: {+ databaseId: 2,+ issue: {+ comments: connection(+ comments.slice(start, end),+ end < comments.length,+ String(end),+ comments.length,+ ),+ },+ },+ };+ }+ return emptyPulls();+ },+ );+ let result = await importIssues(project.id);+ expect(result).toMatchObject({ pending: true, imported: 1, skipped: 0 });+ expect(+ JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),+ ).toMatchObject({ syncFilterPaused: true });+ result = await importIssues(project.id, result.runId);+ expect(result).toMatchObject({ pending: false, imported: 1, skipped: 0 });+ expect(await db.query.activityTable.findMany()).toHaveLength(+ comments.length,+ );+ expect(+ JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),+ ).toMatchObject({ syncFilterPaused: true });+ expect(await importIssues(project.id)).toMatchObject({+ pending: false,+ imported: 0,+ skipped: 1,+ });+ expect(await db.query.activityTable.findMany()).toHaveLength(+ comments.length,+ );+ });it("rejects an incoming rule change while fetching a page before creating tasks or consuming its cursor", async () => {const { integration, config, request } = await setup();const started = deferred();diff --git a/tests/api-integration/integration-label-dispatch.test.ts b/tests/api-integration/integration-label-dispatch.test.tsindex 86558fc1a..93fdabde7 100644--- a/tests/api-integration/integration-label-dispatch.test.ts+++ b/tests/api-integration/integration-label-dispatch.test.ts@@ -137,7 +137,7 @@ it.each(["github", "gitea", "gitlab"] as const)(link,{text: async () => undefined,- close: mocks.addLabel,+ state: mocks.addLabel,labels: () =>syncTaskFieldLabels(f.task.id,diff --git a/tests/api-integration/integration-sync-providers.test.ts b/tests/api-integration/integration-sync-providers.test.tsindex fe2e7d7fc..63117d1fc 100644--- a/tests/api-integration/integration-sync-providers.test.ts+++ b/tests/api-integration/integration-sync-providers.test.ts@@ -503,6 +503,117 @@ describe.each(["github", "gitea", "gitlab"] as const)();});+ it("repairs a late initialization close after a concurrent reopen", async () => {+ const f = await setup(type);+ await f.assign();+ await db+ .update(schema.taskTable)+ .set({ status: f.columns.done.slug, columnId: f.columns.done.id })+ .where(eq(schema.taskTable.id, f.task.id));+ let started!: () => void;+ let release!: () => void;+ const dispatched = new Promise<void>((resolve) => {+ started = resolve;+ });+ const gate = new Promise<void>((resolve) => {+ release = resolve;+ });+ let firstClose = true;+ let remoteState = "open";+ mocks.update.mockImplementation(async (...args: unknown[]) => {+ const patch = args.at(-1) as { state?: string; state_event?: string };+ const state =+ patch.state ?? (patch.state_event === "close" ? "closed" : "open");+ if (state === "closed" && firstClose) {+ firstClose = false;+ started();+ await gate;+ }+ remoteState = state;+ });+ const exporting = reconcileTaskSync(f.project.id, f.task.id);+ try {+ await dispatched;+ await db.transaction(async (tx) => {+ await tx.execute(sql`set local lock_timeout = '1s'`);+ await tx+ .update(schema.taskTable)+ .set({ status: f.columns.todo.slug, columnId: f.columns.todo.id })+ .where(eq(schema.taskTable.id, f.task.id));+ });+ await plugin.onTaskStatusChanged!(+ {+ taskId: f.task.id,+ projectId: f.project.id,+ userId: null,+ title: f.task.title,+ oldStatus: f.columns.done.slug,+ newStatus: f.columns.todo.slug,+ },+ {+ integrationId: f.integration.id,+ projectId: f.project.id,+ config: f.config,+ },+ );+ expect(remoteState).toBe("open");+ } finally {+ release();+ await exporting;+ }+ expect(remoteState).toBe("open");+ const link = (await db.query.externalLinkTable.findMany())[0]!;+ expect(JSON.parse(link.metadata!)).toMatchObject({+ state: "open",+ syncInitializedState: true,+ syncInitializationPending: false,+ });+ });++ it("retries an uncertain initialization close as open after the task changes", async () => {+ const f = await setup(type);+ await f.assign();+ await db+ .update(schema.taskTable)+ .set({ status: f.columns.done.slug, columnId: f.columns.done.id })+ .where(eq(schema.taskTable.id, f.task.id));+ let remoteState = "open";+ const log = vi.spyOn(console, "error").mockImplementation(() => {});+ mocks.update.mockImplementationOnce(async () => {+ remoteState = "closed";+ throw new Error("private-state-response-token");+ });+ await reconcileTaskSync(f.project.id, f.task.id);+ const link = (await db.query.externalLinkTable.findMany())[0]!;+ expect(JSON.parse(link.metadata!)).toMatchObject({+ syncInitializationPending: true,+ });+ expect(JSON.parse(link.metadata!).syncInitializedState).not.toBe(true);+ expect(remoteState).toBe("closed");+ expect(JSON.stringify(log.mock.calls)).not.toContain(+ "private-state-response-token",+ );+ await db+ .update(schema.taskTable)+ .set({ status: f.columns.todo.slug, columnId: f.columns.todo.id })+ .where(eq(schema.taskTable.id, f.task.id));+ mocks.update.mockImplementation(async (...args: unknown[]) => {+ const patch = args.at(-1) as { state?: string; state_event?: string };+ remoteState =+ patch.state ?? (patch.state_event === "close" ? "closed" : "open");+ });+ await reconcileProjectSync(f.project.id, f.integration.id);+ expect(create).toHaveBeenCalledOnce();+ expect(remoteState).toBe("open");+ expect(+ JSON.parse((await db.query.externalLinkTable.findFirst())!.metadata!),+ ).toMatchObject({+ state: "open",+ syncInitializedState: true,+ syncInitializationPending: false,+ });+ });+it.each([false, true])("replays edits made during creation and retries failed text initialization (failure=%s)",async (failure) => {