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(integrations): preserve resources and fetch comments before locking tasks

Tin878742043580
diff --git a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
index 0e89e3ad1..87d565aca 100644
--- a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
+++ b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
@@ -14,6 +14,7 @@ import { isKaneoComment } from "../../plugins/gitea/utils/comment-origin";
import {
createGiteaClient,
type GiteaIssue,
+ type GiteaComment,
type GiteaLabel,
type GiteaPullRequest,
} from "../../plugins/gitea/utils/gitea-api";
@@ -215,6 +216,8 @@ async function importSingleIssue(
const priority = extractIssuePriority(adaptedLabels);
const status = extractIssueStatus(adaptedLabels);
+ const comments = await fetchIssueComments(issue.number, config, client);
+
if (existingLink) {
const result = await withIntegrationTask(
existingLink.taskId,
@@ -240,13 +243,7 @@ async function importSingleIssue(
database,
);
- await importCommentsForTask(
- issue.number,
- existingLink.taskId,
- config,
- client,
- database,
- );
+ await importCommentsForTask(comments, existingLink.taskId, database);
return "updated" as const;
},
@@ -298,7 +295,7 @@ async function importSingleIssue(
await importLabelsForTask(labels, created.id, workspaceId, tx);
- await importCommentsForTask(issue.number, created.id, config, client, tx);
+ await importCommentsForTask(comments, created.id, tx);
return created;
},
@@ -401,19 +398,12 @@ async function importLabelsForTask(
}
}
-async function importCommentsForTask(
+async function fetchIssueComments(
issueNumber: number,
- taskId: string,
config: GiteaConfig,
client: ReturnType<typeof createGiteaClient>,
- database: IntegrationDatabase = db,
-): Promise<void> {
- const allComments: Array<{
- id: number;
- body: string;
- html_url: string;
- user?: { login?: string; username?: string; avatar_url?: string } | null;
- }> = [];
+): Promise<GiteaComment[]> {
+ const allComments: GiteaComment[] = [];
let page = 1;
while (true) {
@@ -433,6 +423,14 @@ async function importCommentsForTask(
page++;
}
+ return allComments;
+}
+
+async function importCommentsForTask(
+ allComments: GiteaComment[],
+ taskId: string,
+ database: IntegrationDatabase,
+): Promise<void> {
for (const comment of allComments) {
const username = comment.user?.login ?? comment.user?.username ?? "";
if (username.endsWith("[bot]") || isKaneoComment(comment.body)) {
diff --git a/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts b/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
index bc90b9e33..62b0ef6b4 100644
--- a/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
+++ b/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
@@ -23,6 +23,7 @@ import { extractTaskNumberGitlab } from "../../plugins/gitlab/utils/branch-match
import {
createGitlabClient,
type GitlabIssue,
+ type GitlabNote,
type GitlabMergeRequest,
} from "../../plugins/gitlab/utils/gitlab-api";
import { taskDescriptionFromIssue } from "../../plugins/gitlab/utils/issue-description";
@@ -192,6 +193,8 @@ async function importSingleIssue(
const priority = extractIssuePriority(labels);
const status = extractIssueStatus(labels);
+ const notes = await fetchIssueNotes(issue, config, client);
+
if (existingLink) {
const result = await withIntegrationTask(
existingLink.taskId,
@@ -216,13 +219,7 @@ async function importSingleIssue(
workspaceId,
database,
);
- await importNotesForTask(
- issue,
- existingLink.taskId,
- config,
- client,
- database,
- );
+ await importNotesForTask(issue, notes, existingLink.taskId, database);
return "updated" as const;
},
@@ -273,7 +270,7 @@ async function importSingleIssue(
);
await importLabelsForTask(labels, created.id, workspaceId, tx);
- await importNotesForTask(issue, created.id, config, client, tx);
+ await importNotesForTask(issue, notes, created.id, tx);
return created;
},
@@ -337,13 +334,12 @@ async function importLabelsForTask(
}
}
-async function importNotesForTask(
+async function fetchIssueNotes(
issue: GitlabIssue,
- taskId: string,
config: GitlabConfig,
client: GitlabClient,
- database: IntegrationDatabase = db,
-): Promise<void> {
+): Promise<GitlabNote[]> {
+ const allNotes: GitlabNote[] = [];
for (let page = 1; page <= MAX_PAGES; page++) {
const notes = await client.listIssueNotes(
config.projectPath,
@@ -354,39 +350,48 @@ async function importNotesForTask(
if (notes.length === 0) break;
- for (const note of notes) {
- // Skip system notes (label/state changes) and internal notes.
- if (note.system || note.internal) {
- continue;
- }
+ allNotes.push(...notes);
+ if (notes.length < PER_PAGE) break;
+ }
+ return allNotes;
+}
- const username = note.author?.username ?? note.author?.name ?? "";
-
- await database
- .insert(activityTable)
- .values({
- taskId,
- type: "comment",
- content: note.body,
- externalUserName: username || "Unknown",
- externalUserAvatar: note.author?.avatar_url ?? null,
- externalSource: "gitlab",
- // The notes API has no URL, so link to the anchor on the issue page.
- externalUrl: `${issue.web_url}#note_${note.id}`,
- eventData: {
- externalCommentId: note.id,
- },
- })
- .onConflictDoNothing({
- target: [
- activityTable.taskId,
- activityTable.externalSource,
- activityTable.externalUrl,
- ],
- });
+async function importNotesForTask(
+ issue: GitlabIssue,
+ notes: GitlabNote[],
+ taskId: string,
+ database: IntegrationDatabase,
+): Promise<void> {
+ for (const note of notes) {
+ // Skip system notes (label/state changes) and internal notes.
+ if (note.system || note.internal) {
+ continue;
}
- if (notes.length < PER_PAGE) break;
+ const username = note.author?.username ?? note.author?.name ?? "";
+
+ await database
+ .insert(activityTable)
+ .values({
+ taskId,
+ type: "comment",
+ content: note.body,
+ externalUserName: username || "Unknown",
+ externalUserAvatar: note.author?.avatar_url ?? null,
+ externalSource: "gitlab",
+ // The notes API has no URL, so link to the anchor on the issue page.
+ externalUrl: `${issue.web_url}#note_${note.id}`,
+ eventData: {
+ externalCommentId: note.id,
+ },
+ })
+ .onConflictDoNothing({
+ target: [
+ activityTable.taskId,
+ activityTable.externalSource,
+ activityTable.externalUrl,
+ ],
+ });
}
}
diff --git a/apps/api/src/task/controllers/move-task.ts b/apps/api/src/task/controllers/move-task.ts
index d2c2918e8..b38184c5d 100644
--- a/apps/api/src/task/controllers/move-task.ts
+++ b/apps/api/src/task/controllers/move-task.ts
@@ -1,4 +1,4 @@
-import { and, asc, eq } from "drizzle-orm";
+import { and, asc, eq, isNotNull } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import {
@@ -163,7 +163,12 @@ async function moveTask({
await tx
.delete(externalLinkTable)
- .where(eq(externalLinkTable.taskId, taskId));
+ .where(
+ and(
+ eq(externalLinkTable.taskId, taskId),
+ isNotNull(externalLinkTable.integrationId),
+ ),
+ );
await tx
.update(assetTable)
diff --git a/apps/web/src/hooks/use-project-websocket-lifecycle.test.tsx b/apps/web/src/hooks/use-project-websocket-lifecycle.test.tsx
index ae642ab48..e007bdab3 100644
--- a/apps/web/src/hooks/use-project-websocket-lifecycle.test.tsx
+++ b/apps/web/src/hooks/use-project-websocket-lifecycle.test.tsx
@@ -132,6 +132,22 @@ describe("project WebSocket lifecycle", () => {
}
});
+ it("refreshes task resources after a task move", () => {
+ renderHook(() => useProjectWebSocket("project-a"));
+ act(() =>
+ TestSocket.instances[0].onmessage?.({
+ data: JSON.stringify({
+ type: "TASK_MOVED",
+ projectId: "project-a",
+ taskId: "task-a",
+ }),
+ }),
+ );
+ expect(client.invalidateQueries).toHaveBeenCalledWith({
+ queryKey: ["external-links", "task-a"],
+ });
+ });
+
it("preserves bounded exponential reconnects and active message invalidation", () => {
const { unmount } = renderHook(() => useProjectWebSocket("project-a"));
for (let retry = 0; retry < 5; retry++) {
diff --git a/apps/web/src/hooks/use-project-websocket.ts b/apps/web/src/hooks/use-project-websocket.ts
index 82033e8b9..57b73e88e 100644
--- a/apps/web/src/hooks/use-project-websocket.ts
+++ b/apps/web/src/hooks/use-project-websocket.ts
@@ -127,7 +127,11 @@ export function useProjectWebSocket(projectId: string) {
});
}
- if (message.type === "TASK_UPDATED" && message.taskId) {
+ if (
+ (message.type === "TASK_UPDATED" ||
+ message.type === "TASK_MOVED") &&
+ message.taskId
+ ) {
queryClient.invalidateQueries({
queryKey: ["external-links", message.taskId],
});
diff --git a/tests/api-integration/integration-task-ownership.test.ts b/tests/api-integration/integration-task-ownership.test.ts
index de144beff..9d8f6fb2c 100644
--- a/tests/api-integration/integration-task-ownership.test.ts
+++ b/tests/api-integration/integration-task-ownership.test.ts
@@ -60,6 +60,8 @@ beforeEach(async () => {
await resetTestDatabase();
vi.clearAllMocks();
m.listIssues.mockResolvedValue([remoteIssue]);
+ m.listIssueComments.mockResolvedValue([]);
+ m.listIssueNotes.mockResolvedValue([]);
});
async function setup(type = "gitea") {
const source = await createWorkspaceMember({ role: "owner" });
@@ -158,6 +160,35 @@ describe("integration task ownership", () => {
await expectPrivateTask(fixture.task.id);
},
);
+ it.each(["gitea", "gitlab"])(
+ "%s comment fetching leaves task moves unlocked and rechecks scope afterwards",
+ async (type) => {
+ const f = await setup(type);
+ const fetchComments =
+ type === "gitea" ? m.listIssueComments : m.listIssueNotes;
+ fetchComments.mockImplementationOnce(async () => {
+ await moveTask({
+ taskId: f.task.id,
+ destinationProjectId: f.destination.id,
+ currentUserId: f.source.user.id,
+ });
+ return [];
+ });
+ const result = await (
+ type === "gitea" ? importGiteaIssues : importGitlabIssues
+ )(f.project.id);
+ expect(result.updated).toBe(0);
+ await expectPrivateTask(f.task.id);
+ expect(
+ (
+ await db.query.taskTable.findFirst({
+ where: eq(schema.taskTable.id, f.task.id),
+ })
+ )?.projectId,
+ ).toBe(f.destination.id);
+ },
+ );
+
it("gitea edits, labels and comments cannot follow stale links", async () => {
const f = await setup();
await moveWithoutCleanup(f);
@@ -204,6 +235,16 @@ describe("integration task ownership", () => {
});
it("moving a task removes links to its previous project's integration atomically", async () => {
const f = await setup();
+ const [manual] = await db
+ .insert(schema.externalLinkTable)
+ .values({
+ taskId: f.task.id,
+ resourceType: "url",
+ integrationId: null,
+ url: "https://example.test/manual",
+ externalId: "manual",
+ })
+ .returning();
await moveTask({
taskId: f.task.id,
destinationProjectId: f.destination.id,
@@ -213,7 +254,7 @@ describe("integration task ownership", () => {
await db.query.externalLinkTable.findMany({
where: eq(schema.externalLinkTable.taskId, f.task.id),
}),
- ).toEqual([]);
+ ).toEqual([manual]);
});
it("moving a project removes legacy links owned by another project", async () => {
const f = await setup();