feat(FN-4378): complete Step 3 — honor github issue action on delete

Fusion-Task-Id: FN-4378
Fusion-Task-Lineage: d3867ba8-f852-41e3-9db0-584c0ffc23b1
This commit is contained in:
Fusion
2026-05-13 13:32:42 -07:00
committed by gsxdsm
parent 1f84b169ef
commit 19f2b96f9f
2 changed files with 73 additions and 19 deletions

View File

@@ -3,8 +3,9 @@ import { beforeEach, describe, expect, it, vi, type Mock } from "vitest";
import type { TaskStore } from "@fusion/core";
import { decideIssueAction, GitHubTrackingStateService } from "../github-tracking-state.js";
const { mockSetIssueState } = vi.hoisted(() => ({
const { mockSetIssueState, mockDeleteIssue } = vi.hoisted(() => ({
mockSetIssueState: vi.fn(),
mockDeleteIssue: vi.fn(),
}));
const { mockResolveGithubTrackingAuth } = vi.hoisted(() => ({
@@ -14,6 +15,7 @@ const { mockResolveGithubTrackingAuth } = vi.hoisted(() => ({
vi.mock("../github.js", () => ({
GitHubClient: vi.fn().mockImplementation(() => ({
setIssueState: (...args: unknown[]) => mockSetIssueState(...args),
deleteIssue: (...args: unknown[]) => mockDeleteIssue(...args),
})),
}));
@@ -266,14 +268,44 @@ describe("GitHubTrackingStateService", () => {
});
describe("on task:deleted", () => {
it("closes the linked issue with not_planned and does not log to the store", async () => {
it.each([undefined, "auto", "close"] as const)("closes the linked issue with not_planned when action is %s", async (action) => {
service.start();
store.emit("task:deleted", createTask());
if (action === undefined) {
store.emit("task:deleted", createTask());
} else {
store.emit("task:deleted", createTask(), { githubIssueAction: action });
}
await flushAsync();
expect(mockSetIssueState).toHaveBeenCalledWith("owner", "repo", 42, "closed", "not_planned");
expect(store.logEntry).not.toHaveBeenCalled();
expect(mockDeleteIssue).not.toHaveBeenCalled();
});
it("deletes linked issue when githubIssueAction is delete", async () => {
service.start();
store.emit("task:deleted", createTask(), { githubIssueAction: "delete" });
await flushAsync();
expect(mockDeleteIssue).toHaveBeenCalledWith("owner", "repo", 42);
expect(mockSetIssueState).not.toHaveBeenCalled();
expect(store.logEntry).toHaveBeenCalledWith("FN-1", "Deleted linked GitHub tracking issue", "owner/repo#42");
});
it("leaves linked issue untouched when githubIssueAction is leave", async () => {
service.start();
store.emit("task:deleted", createTask(), { githubIssueAction: "leave" });
await flushAsync();
expect(mockDeleteIssue).not.toHaveBeenCalled();
expect(mockSetIssueState).not.toHaveBeenCalled();
expect(store.logEntry).toHaveBeenCalledWith(
"FN-1",
"Left linked GitHub tracking issue unchanged on task delete",
"owner/repo#42",
);
});
it.each([
@@ -304,24 +336,32 @@ describe("GitHubTrackingStateService", () => {
await flushAsync();
expect(mockSetIssueState).not.toHaveBeenCalled();
expect(mockDeleteIssue).not.toHaveBeenCalled();
expect(store.logEntry).not.toHaveBeenCalled();
});
it("swallows network failures without throwing and does not log to the store", async () => {
it("logs close failures without throwing", async () => {
service.start();
const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
mockSetIssueState.mockRejectedValueOnce(new Error("delete close failed"));
expect(() => {
store.emit("task:deleted", createTask());
store.emit("task:deleted", createTask(), { githubIssueAction: "close" });
}).not.toThrow();
await flushAsync();
expect(warnSpy).toHaveBeenCalledWith(
"[github-tracking-state] Failed to close linked GitHub tracking issue for deleted task FN-1: delete close failed",
);
expect(store.logEntry).not.toHaveBeenCalled();
warnSpy.mockRestore();
expect(store.logEntry).toHaveBeenCalledWith("FN-1", "Failed to close linked GitHub tracking issue", "delete close failed");
});
it("logs delete failures without throwing", async () => {
service.start();
mockDeleteIssue.mockRejectedValueOnce(new Error("delete failed"));
expect(() => {
store.emit("task:deleted", createTask(), { githubIssueAction: "delete" });
}).not.toThrow();
await flushAsync();
expect(store.logEntry).toHaveBeenCalledWith("FN-1", "Failed to delete linked GitHub tracking issue", "delete failed");
});
});
});

View File

@@ -1,4 +1,4 @@
import type { GlobalSettings, ProjectSettings, Task, TaskStore } from "@fusion/core";
import type { GithubIssueAction, GlobalSettings, ProjectSettings, Task, TaskStore } from "@fusion/core";
import { GitHubClient } from "./github.js";
import { resolveGithubTrackingAuth } from "./github-auth.js";
@@ -43,8 +43,8 @@ export class GitHubTrackingStateService {
private readonly onTaskMoved = (event: TaskMovedEvent): void => {
void this.handleTaskMoved(event);
};
private readonly onTaskDeleted = (task: Task): void => {
void this.handleTaskDeleted(task);
private readonly onTaskDeleted = (task: Task, meta?: { githubIssueAction?: GithubIssueAction }): void => {
void this.handleTaskDeleted(task, meta);
};
private started = false;
@@ -129,7 +129,7 @@ export class GitHubTrackingStateService {
}
}
private async handleTaskDeleted(task: Task): Promise<void> {
private async handleTaskDeleted(task: Task, meta?: { githubIssueAction?: GithubIssueAction }): Promise<void> {
if (task.githubTracking?.enabled !== true) {
return;
}
@@ -144,6 +144,12 @@ export class GitHubTrackingStateService {
return;
}
const githubIssueAction = meta?.githubIssueAction ?? "auto";
if (githubIssueAction === "leave") {
await this.store.logEntry(task.id, "Left linked GitHub tracking issue unchanged on task delete", `${owner}/${repo}#${number}`);
return;
}
const projectSettings = await this.store.getSettings() as Pick<ProjectSettings, "githubAuthMode" | "githubAuthToken">;
const globalSettings = (await this.store.getGlobalSettingsStore?.()?.getSettings?.() ?? {}) as Pick<GlobalSettings, never>;
const resolution = resolveGithubTrackingAuth({ projectSettings, globalSettings });
@@ -155,12 +161,20 @@ export class GitHubTrackingStateService {
? new GitHubClient({ token: resolution.auth.token, forceMode: "token" })
: new GitHubClient({ forceMode: "gh-cli" });
if (githubIssueAction === "delete") {
try {
await client.deleteIssue(owner, repo, number);
await this.store.logEntry(task.id, "Deleted linked GitHub tracking issue", `${owner}/${repo}#${number}`);
} catch (err) {
await this.store.logEntry(task.id, "Failed to delete linked GitHub tracking issue", err instanceof Error ? err.message : String(err));
}
return;
}
try {
await client.setIssueState(owner, repo, number, "closed", "not_planned");
} catch (err) {
console.warn(
`[github-tracking-state] Failed to close linked GitHub tracking issue for deleted task ${task.id}: ${err instanceof Error ? err.message : String(err)}`,
);
await this.store.logEntry(task.id, "Failed to close linked GitHub tracking issue", err instanceof Error ? err.message : String(err));
}
}
}