⚡ 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(api): keep linked issues closed when a completed task is archived
Tinecbdc626d4a0
diff --git a/apps/api/src/plugins/gitea/events/task-status-changed.ts b/apps/api/src/plugins/gitea/events/task-status-changed.tsindex 0d9b65560..eb9b34fb0 100644--- a/apps/api/src/plugins/gitea/events/task-status-changed.ts+++ b/apps/api/src/plugins/gitea/events/task-status-changed.ts@@ -1,7 +1,7 @@import { syncTaskFieldLabels } from "../../sync/sync-task-field-labels";import { canSyncTask } from "../../sync/eligibility";import { syncLatestTaskValue } from "../../github/services/sync-latest-task-value";-import { isTaskInFinalState } from "../../github/services/task-service";+import { resolveIssueStateChange } from "../../sync/issue-state-change";import { findExternalLinksByTask } from "../../github/services/link-manager";import type { PluginContext, TaskStatusChangedEvent } from "../../types";import type { GiteaConfig } from "../config";@@ -57,19 +57,13 @@ export async function handleTaskStatusChanged(},);if (currentValue === undefined) return;- const closing = await isTaskInFinalState({- projectId: event.projectId,- status: currentValue,- columnId: null,- });- const reopening =- !closing &&- (await isTaskInFinalState({- projectId: event.projectId,- status: event.oldStatus,- columnId: null,- }));- if (closing || reopening) {+ const change = await resolveIssueStateChange(+ event.projectId,+ event.oldStatus,+ currentValue,+ );+ if (change) {+ const closing = change === "close";await syncLatestTaskValue(event.taskId,event.projectId,diff --git a/apps/api/src/plugins/github/events/task-status-changed.ts b/apps/api/src/plugins/github/events/task-status-changed.tsindex 646615211..0f5c89db8 100644--- a/apps/api/src/plugins/github/events/task-status-changed.ts+++ b/apps/api/src/plugins/github/events/task-status-changed.ts@@ -1,7 +1,7 @@import { syncTaskFieldLabels } from "../../sync/sync-task-field-labels";import { canSyncTask } from "../../sync/eligibility";import { syncLatestTaskValue } from "../services/sync-latest-task-value";-import { isTaskInFinalState } from "../services/task-service";+import { resolveIssueStateChange } from "../../sync/issue-state-change";import type { PluginContext, TaskStatusChangedEvent } from "../../types";import { type GitHubConfig, hasVerifiedGitHubBinding } from "../config";import { findExternalLinksByTask } from "../services/link-manager";@@ -77,19 +77,13 @@ export async function handleTaskStatusChanged(},);if (currentValue === undefined) return;- const closing = await isTaskInFinalState({- projectId: event.projectId,- status: currentValue,- columnId: null,- });- const reopening =- !closing &&- (await isTaskInFinalState({- projectId: event.projectId,- status: event.oldStatus,- columnId: null,- }));- if (closing || reopening) {+ const change = await resolveIssueStateChange(+ event.projectId,+ event.oldStatus,+ currentValue,+ );+ if (change) {+ const closing = change === "close";await syncLatestTaskValue(event.taskId,event.projectId,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 138a26a67..2ec8139f4 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@@ -197,6 +197,8 @@ export async function syncLatestTaskValue(},});if (!task) return;+ // Archiving keeps the issue state just written; see resolveIssueStateChange.+ if (field === "state" && task.status === "archived") return true;const current =field === "state"? (await isTaskInFinalState(task))diff --git a/apps/api/src/plugins/gitlab/events/task-status-changed.ts b/apps/api/src/plugins/gitlab/events/task-status-changed.tsindex 7a1513c94..9c9b7c12b 100644--- a/apps/api/src/plugins/gitlab/events/task-status-changed.ts+++ b/apps/api/src/plugins/gitlab/events/task-status-changed.ts@@ -1,7 +1,7 @@import { createIssueWrite } from "../../sync/dispatch-issue-write";import { syncTaskFieldLabels } from "../../sync/sync-task-field-labels";import { canSyncTask } from "../../sync/eligibility";-import { isTaskInFinalState } from "../../github/services/task-service";+import { resolveIssueStateChange } from "../../sync/issue-state-change";import {findExternalLinksByTask,updateExternalLink,@@ -70,22 +70,15 @@ export async function handleTaskStatusChanged(JSON.stringify(context.config),);- const closing = await isTaskInFinalState({- projectId: event.projectId,- status: currentValue,- columnId: null,- });- const reopening =- !closing &&- (await isTaskInFinalState({- projectId: event.projectId,- status: event.oldStatus,- columnId: null,- }));-- if (!closing && !reopening) {+ const change = await resolveIssueStateChange(+ event.projectId,+ event.oldStatus,+ currentValue,+ );+ if (!change) {return;}+ const closing = change === "close";await write(() =>createGitlabClient(config).updateIssue(config.projectPath, issueIid, {diff --git a/apps/api/src/plugins/sync/issue-state-change.ts b/apps/api/src/plugins/sync/issue-state-change.tsnew file mode 100644index 000000000..410d4f41e--- /dev/null+++ b/apps/api/src/plugins/sync/issue-state-change.ts@@ -0,0 +1,19 @@+import { isTaskInFinalState } from "../github/services/task-service";++// Archiving keeps the issue's current state: a completed task's issue stays+// closed, and shelving unfinished work does not close it. Unarchiving into an+// open column reopens the issue in case the task was completed before.+export async function resolveIssueStateChange(+ projectId: string,+ oldStatus: string,+ status: string,+): Promise<"close" | "reopen" | undefined> {+ if (status === "archived") return;+ if (await isTaskInFinalState({ projectId, status, columnId: null }))+ return "close";+ if (+ oldStatus === "archived" ||+ (await isTaskInFinalState({ projectId, status: oldStatus, columnId: null }))+ )+ return "reopen";+}diff --git a/tests/api-integration/integration-sync-providers.test.ts b/tests/api-integration/integration-sync-providers.test.tsindex a2266ae29..c12d53d65 100644--- a/tests/api-integration/integration-sync-providers.test.ts+++ b/tests/api-integration/integration-sync-providers.test.ts@@ -514,6 +514,95 @@ describe.each(["github", "gitea", "gitlab"] as const)();});+ describe("archiving", () => {+ const closed =+ type === "gitlab" ? { state_event: "close" } : { state: "closed" };+ const reopened =+ type === "gitlab" ? { state_event: "reopen" } : { state: "open" };+ async function exported() {+ const f = await setup(type);+ await f.assign();+ await reconcileProjectSync(f.project.id, f.integration.id);+ const context = {+ integrationId: f.integration.id,+ projectId: f.project.id,+ config: f.config,+ };+ const changeStatus = async (+ oldStatus: string,+ newStatus: string,+ columnId: string | null,+ ) => {+ await db+ .update(schema.taskTable)+ .set({ status: newStatus, columnId })+ .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,+ newStatus,+ },+ context,+ );+ };+ mocks.update.mockClear();+ return { ...f, changeStatus };+ }++ it("keeps a completed issue closed and reopens it when unarchived", async () => {+ const f = await exported();+ const { done, todo } = f.columns;+ await f.changeStatus(todo.slug, done.slug, done.id);+ expect(mocks.update).toHaveBeenCalledOnce();+ expect(mocks.update.mock.calls[0]!.at(-1)).toMatchObject(closed);++ await f.changeStatus(done.slug, "archived", null);+ expect(mocks.update).toHaveBeenCalledOnce();++ await f.changeStatus("archived", todo.slug, todo.id);+ expect(mocks.update).toHaveBeenCalledTimes(2);+ expect(mocks.update.mock.calls[1]!.at(-1)).toMatchObject(reopened);+ });++ it("leaves an open issue alone when archiving unfinished work", async () => {+ const f = await exported();+ await f.changeStatus(f.columns.todo.slug, "archived", null);+ expect(mocks.update).not.toHaveBeenCalled();++ await f.changeStatus(+ "archived",+ f.columns.done.slug,+ f.columns.done.id,+ );+ expect(mocks.update).toHaveBeenCalledOnce();+ expect(mocks.update.mock.calls[0]!.at(-1)).toMatchObject(closed);+ });++ it.skipIf(type === "gitlab")(+ "does not reopen an issue archived while its close is in flight",+ async () => {+ const f = await exported();+ mocks.update.mockImplementationOnce(async () => {+ await db+ .update(schema.taskTable)+ .set({ status: "archived", columnId: null })+ .where(eq(schema.taskTable.id, f.task.id));+ });+ await f.changeStatus(+ f.columns.todo.slug,+ f.columns.done.slug,+ f.columns.done.id,+ );+ expect(mocks.update).toHaveBeenCalledOnce();+ expect(mocks.update.mock.calls[0]!.at(-1)).toMatchObject(closed);+ },+ );+ });+it("repairs a late initialization close after a concurrent reopen", async () => {const f = await setup(type);await f.assign();