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): keep archived issue state in deferred repairs and sync resume

Tinf180d1415e90
diff --git a/apps/api/src/integration-sync/controllers/review-resume.ts b/apps/api/src/integration-sync/controllers/review-resume.ts
index bde44f459..b54311c70 100644
--- a/apps/api/src/integration-sync/controllers/review-resume.ts
+++ b/apps/api/src/integration-sync/controllers/review-resume.ts
@@ -56,13 +56,6 @@ export async function reviewSyncResume(
message:
"The task must match the current rule and be paused before resuming",
});
- const local = {
- title: task.title,
- description: task.description ?? "",
- state: (await isTaskInFinalState(task, database))
- ? ("closed" as const)
- : ("open" as const),
- };
let snapshot = providerSnapshot;
if (!snapshot) {
try {
@@ -87,6 +80,17 @@ export async function reviewSyncResume(
description: remoteIssue.description,
state: remoteIssue.state,
};
+ const local = {
+ title: task.title,
+ description: task.description ?? "",
+ // Archived tasks have no issue state of their own; keep the provider's.
+ state:
+ task.status === "archived"
+ ? remote.state
+ : (await isTaskInFinalState(task, database))
+ ? ("closed" as const)
+ : ("open" as const),
+ };
const token = createHash("sha256")
.update(
JSON.stringify({
diff --git a/apps/api/src/plugins/github/services/deferred-issue-edits.ts b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
index cd1253808..49a5bcec2 100644
--- a/apps/api/src/plugins/github/services/deferred-issue-edits.ts
+++ b/apps/api/src/plugins/github/services/deferred-issue-edits.ts
@@ -259,7 +259,12 @@ async function replayClaimedIssueEdits() {
const taskIsClosed = fields.includes("state")
? await isTaskInFinalState(task, tx)
: false;
+ // Archived tasks have no outbound state target; keep the provider's.
+ const keepsState =
+ fields.includes("state") && task.status === "archived";
+ if (keepsState) current.state = values.state;
for (const field of fields) {
+ if (field === "state" && keepsState) continue;
const stamp = current.lastSync?.[field];
const uncertain = uncertainOutboundIntents(stamp, values[field]);
const intentIds = uncertain.flatMap((entry) =>
@@ -357,6 +362,9 @@ async function replayClaimedIssueEdits() {
{
metadata: current,
...(accepted.includes("title") ? { title: values.title } : {}),
+ ...(keepsState
+ ? { retireUncertainOutbound: "state" as const }
+ : {}),
...(repairs.length ? {} : { completeDeferredEdit: job.id }),
},
tx,
diff --git a/tests/api-integration/deferred-issue-edits.test.ts b/tests/api-integration/deferred-issue-edits.test.ts
index 1716965b5..fbf18138d 100644
--- a/tests/api-integration/deferred-issue-edits.test.ts
+++ b/tests/api-integration/deferred-issue-edits.test.ts
@@ -1690,6 +1690,54 @@ it("durably queues an uncertain standalone writer and later settles it", async (
expect((await current(task.id))?.title).toBe("A");
});
+it.each(["github", "gitea"])(
+ "keeps a timed-out %s close when the task is archived before replay",
+ async (type) => {
+ const { task, integration, link } = await seed(type);
+ await db
+ .update(schema.externalLinkTable)
+ .set({ metadata: "{}" })
+ .where(eq(schema.externalLinkTable.id, link.id));
+ await db
+ .update(schema.taskTable)
+ .set({ status: "done" })
+ .where(eq(schema.taskTable.id, task.id));
+ await expect(
+ syncLatestTaskValue(
+ task.id,
+ integration.projectId,
+ link,
+ "state",
+ "closed",
+ async () => {
+ throw new Error("provider timeout");
+ },
+ ),
+ ).rejects.toThrow("provider timeout");
+ expect((await metadata(link.id)).deferredIssueEdit.repairFields).toEqual([
+ "state",
+ ]);
+ await db
+ .update(schema.taskTable)
+ .set({ status: "archived", columnId: null })
+ .where(eq(schema.taskTable.id, task.id));
+
+ await replayDeferredIssueEdits();
+
+ expect(m.write).not.toHaveBeenCalled();
+ const after = await metadata(link.id);
+ expect(after.deferredIssueEdit).toBeUndefined();
+ expect(after.state).toBe("closed");
+ expect(
+ after.lastSync.state.outbound.filter(
+ (entry: { uncertain?: boolean; cancelled?: boolean }) =>
+ entry.uncertain && !entry.cancelled,
+ ),
+ ).toEqual([]);
+ expect((await current(task.id))?.status).toBe("archived");
+ },
+);
+
it("skips provider reads and writes while another replica holds the worker lease", async () => {
const { link, integration } = await seed();
await deferTaskSync(link, integration, ["title"]);
diff --git a/tests/api-integration/integration-sync-providers.test.ts b/tests/api-integration/integration-sync-providers.test.ts
index c12d53d65..8d4ffa564 100644
--- a/tests/api-integration/integration-sync-providers.test.ts
+++ b/tests/api-integration/integration-sync-providers.test.ts
@@ -601,6 +601,65 @@ describe.each(["github", "gitea", "gitlab"] as const)(
expect(mocks.update.mock.calls[0]!.at(-1)).toMatchObject(closed);
},
);
+
+ it("ignores planned and repeated archive transitions", async () => {
+ const f = await exported();
+ await f.changeStatus(f.columns.todo.slug, "planned", null);
+ await f.changeStatus("planned", "archived", null);
+ await f.changeStatus("archived", "archived", null);
+ expect(mocks.update).not.toHaveBeenCalled();
+
+ await f.changeStatus("archived", "planned", null);
+ expect(mocks.update).toHaveBeenCalledOnce();
+ expect(mocks.update.mock.calls[0]!.at(-1)).toMatchObject(reopened);
+ });
+
+ it.each(["kaneo", "provider"] as const)(
+ "keeps a closed issue and the archive when resuming with %s",
+ async (source) => {
+ const f = await exported();
+ const link = (await db.query.externalLinkTable.findMany())[0]!;
+ await db
+ .update(schema.taskTable)
+ .set({ status: "archived", columnId: null })
+ .where(eq(schema.taskTable.id, f.task.id));
+ await updateExternalLink(link.id, {
+ metadata: { syncFilterPaused: true },
+ });
+ mocks.read.mockResolvedValue({
+ title: "Repository title",
+ description: "Repository body",
+ state: "closed",
+ updatedAt: "remote-time",
+ labels: [],
+ });
+ const review = await reviewSyncResume(
+ f.project.id,
+ type,
+ link.id,
+ f.workspace.id,
+ );
+ expect(review.local.state).toBe("closed");
+ await resumeSync(
+ f.project.id,
+ type,
+ link.id,
+ review.token,
+ source,
+ f.workspace.id,
+ );
+ if (source === "kaneo")
+ expect(mocks.write).toHaveBeenCalledWith(
+ expect.objectContaining({ title: f.task.title, state: "closed" }),
+ );
+ else expect(mocks.write).not.toHaveBeenCalled();
+ expect(
+ await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, f.task.id),
+ }),
+ ).toMatchObject({ status: "archived", columnId: null });
+ },
+ );
});
it("repairs a late initialization close after a concurrent reopen", async () => {