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.

Revalidate imported issue links after provider reads

Tin6104cd002118
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 87d565aca..007abb156 100644
--- a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
+++ b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts
@@ -3,6 +3,7 @@ import { HTTPException } from "hono/http-exception";
import db from "../../database";
import {
activityTable,
+ externalLinkTable,
integrationTable,
labelTable,
projectTable,
@@ -223,6 +224,19 @@ async function importSingleIssue(
existingLink.taskId,
{ id: integrationId, projectId, project: { workspaceId } },
async (database) => {
+ const [linked] = await database
+ .select({ id: externalLinkTable.id })
+ .from(externalLinkTable)
+ .where(
+ and(
+ eq(externalLinkTable.id, existingLink.id),
+ eq(externalLinkTable.taskId, existingLink.taskId),
+ eq(externalLinkTable.integrationId, integrationId),
+ ),
+ )
+ .for("update");
+ if (!linked) return "skipped" as const;
+
const updateData: Record<string, unknown> = {
title: issue.title,
description: formatTaskDescriptionFromIssue(issue.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 2a09b4ce6..eef47d7f3 100644
--- a/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
+++ b/apps/api/src/gitlab-integration/controllers/import-gitlab-issues.ts
@@ -3,6 +3,7 @@ import { HTTPException } from "hono/http-exception";
import db from "../../database";
import {
activityTable,
+ externalLinkTable,
integrationTable,
labelTable,
projectTable,
@@ -199,6 +200,19 @@ async function importSingleIssue(
existingLink.taskId,
{ id: integrationId, projectId, project: { workspaceId } },
async (database) => {
+ const [linked] = await database
+ .select({ id: externalLinkTable.id })
+ .from(externalLinkTable)
+ .where(
+ and(
+ eq(externalLinkTable.id, existingLink.id),
+ eq(externalLinkTable.taskId, existingLink.taskId),
+ eq(externalLinkTable.integrationId, integrationId),
+ ),
+ )
+ .for("update");
+ if (!linked) return "skipped" as const;
+
const updateData: Record<string, unknown> = {
title: issue.title,
description: taskDescriptionFromIssue(issue.description),
diff --git a/tests/api-integration/integration-task-ownership.test.ts b/tests/api-integration/integration-task-ownership.test.ts
index 191d92b4b..978d30048 100644
--- a/tests/api-integration/integration-task-ownership.test.ts
+++ b/tests/api-integration/integration-task-ownership.test.ts
@@ -196,6 +196,38 @@ describe("integration task ownership", () => {
},
);
+ it.each(["gitea", "gitlab"])(
+ "%s import cannot revive a removed link after a task moves away and back",
+ 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,
+ });
+ await moveTask({
+ taskId: f.task.id,
+ destinationProjectId: f.project.id,
+ currentUserId: f.source.user.id,
+ });
+ return [];
+ });
+ const result = await (
+ type === "gitea" ? importGiteaIssues : importGitlabIssues
+ )(f.project.id);
+ expect(result).toMatchObject({ updated: 0, skipped: 1 });
+ await expectPrivateTask(f.task.id);
+ expect(
+ await db.query.externalLinkTable.findFirst({
+ where: eq(schema.externalLinkTable.id, f.link.id),
+ }),
+ ).toBeUndefined();
+ },
+ );
+
it("gitea edits, labels and comments cannot follow stale links", async () => {
const f = await setup();
await moveWithoutCleanup(f);
diff --git a/tests/api/gitlab-integration/import-gitlab-issues.test.ts b/tests/api/gitlab-integration/import-gitlab-issues.test.ts
index a5fd23135..018b14524 100644
--- a/tests/api/gitlab-integration/import-gitlab-issues.test.ts
+++ b/tests/api/gitlab-integration/import-gitlab-issues.test.ts
@@ -8,6 +8,11 @@ const mocks = vi.hoisted(() => ({
vi.mock("../../../apps/api/src/database", () => ({
default: {
+ select: () => ({
+ from: () => ({
+ where: () => ({ for: async () => [{ id: "link-1" }] }),
+ }),
+ }),
query: {
projectTable: {
findFirst: async () => ({
diff --git a/tests/api/plugins/gitea/comment-sync.test.ts b/tests/api/plugins/gitea/comment-sync.test.ts
index 55925b728..096ac6813 100644
--- a/tests/api/plugins/gitea/comment-sync.test.ts
+++ b/tests/api/plugins/gitea/comment-sync.test.ts
@@ -17,6 +17,11 @@ const config = {
vi.mock("../../../../apps/api/src/database", () => ({
default: {
+ select: () => ({
+ from: () => ({
+ where: () => ({ for: async () => [{ id: "link-1" }] }),
+ }),
+ }),
insert: () => ({ values: mocks.values }),
update: () => ({ set: () => ({ where: vi.fn() }) }),
delete: () => ({ where: vi.fn() }),