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): handle duplicate activity and label rows on integration inserts

Tin83e635aad1a8
diff --git a/apps/api/src/github-integration/controllers/import-issues.ts b/apps/api/src/github-integration/controllers/import-issues.ts
index d7a02d330..221c2350d 100644
--- a/apps/api/src/github-integration/controllers/import-issues.ts
+++ b/apps/api/src/github-integration/controllers/import-issues.ts
@@ -321,12 +321,17 @@ async function importLabelsForTask(
const colorToUse = existingWorkspaceLabel?.color || labelData.color;
- await db.insert(labelTable).values({
- name: labelData.name,
- color: colorToUse,
- taskId,
- workspaceId,
- });
+ await db
+ .insert(labelTable)
+ .values({
+ name: labelData.name,
+ color: colorToUse,
+ taskId,
+ workspaceId,
+ })
+ .onConflictDoNothing({
+ target: [labelTable.taskId, labelTable.name],
+ });
}
}
@@ -378,15 +383,24 @@ async function importCommentsForTask(
continue;
}
- await db.insert(activityTable).values({
- taskId,
- type: "comment",
- content: comment.body,
- externalUserName: comment.user?.login ?? "Unknown",
- externalUserAvatar: comment.user?.avatar_url ?? null,
- externalSource: "github",
- externalUrl: comment.html_url,
- });
+ await db
+ .insert(activityTable)
+ .values({
+ taskId,
+ type: "comment",
+ content: comment.body,
+ externalUserName: comment.user?.login ?? "Unknown",
+ externalUserAvatar: comment.user?.avatar_url ?? null,
+ externalSource: "github",
+ externalUrl: comment.html_url,
+ })
+ .onConflictDoNothing({
+ target: [
+ activityTable.taskId,
+ activityTable.externalSource,
+ activityTable.externalUrl,
+ ],
+ });
}
}
diff --git a/apps/api/src/label/controllers/create-label.ts b/apps/api/src/label/controllers/create-label.ts
index b2d2efc06..74e7eb536 100644
--- a/apps/api/src/label/controllers/create-label.ts
+++ b/apps/api/src/label/controllers/create-label.ts
@@ -1,3 +1,4 @@
+import { and, eq, isNull, sql } from "drizzle-orm";
import db from "../../database";
import { labelTable } from "../../database/schema";
import { syncLabelToGitea } from "../../plugins/gitea/utils/sync-label-to-gitea";
@@ -9,18 +10,58 @@ async function createLabel(
taskId: string | undefined,
workspaceId: string,
) {
- const [label] = await db
+ if (taskId) {
+ const [inserted] = await db
+ .insert(labelTable)
+ .values({ name, color, taskId, workspaceId })
+ .onConflictDoNothing({
+ target: [labelTable.taskId, labelTable.name],
+ })
+ .returning();
+
+ const label =
+ inserted ??
+ (await db.query.labelTable.findFirst({
+ where: and(eq(labelTable.taskId, taskId), eq(labelTable.name, name)),
+ }));
+
+ if (!label) {
+ throw new Error("Failed to create or resolve label");
+ }
+
+ if (inserted) {
+ syncLabelToGitHub(taskId, name, color).catch((error) => {
+ console.error("Failed to sync label to GitHub:", error);
+ });
+ syncLabelToGitea(taskId, name, color).catch((error) => {
+ console.error("Failed to sync label to Gitea:", error);
+ });
+ }
+
+ return label;
+ }
+
+ const [inserted] = await db
.insert(labelTable)
- .values({ name, color, taskId, workspaceId })
+ .values({ name, color, taskId: null, workspaceId })
+ .onConflictDoNothing({
+ target: [labelTable.workspaceId, labelTable.name],
+ where: sql`${labelTable.taskId} is null`,
+ })
.returning();
- if (taskId) {
- syncLabelToGitHub(taskId, name, color).catch((error) => {
- console.error("Failed to sync label to GitHub:", error);
- });
- syncLabelToGitea(taskId, name, color).catch((error) => {
- console.error("Failed to sync label to Gitea:", error);
- });
+ const label =
+ inserted ??
+ (await db.query.labelTable.findFirst({
+ where: and(
+ eq(labelTable.workspaceId, workspaceId),
+ eq(labelTable.name, name),
+ isNull(labelTable.taskId),
+ ),
+ }));
+
+ if (!label) {
+ throw new Error("Failed to create or resolve label");
}
return label;
diff --git a/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts b/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts
index 74fa97bec..b75ab7c7b 100644
--- a/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts
+++ b/apps/api/src/plugins/gitea/webhooks/issue-labeled.ts
@@ -67,7 +67,12 @@ async function syncGiteaLabelsToTask(
}));
if (labelsToInsert.length > 0) {
- await db.insert(labelTable).values(labelsToInsert).onConflictDoNothing();
+ await db
+ .insert(labelTable)
+ .values(labelsToInsert)
+ .onConflictDoNothing({
+ target: [labelTable.taskId, labelTable.name],
+ });
}
const labelsToDelete = existingRows
@@ -170,12 +175,17 @@ export async function handleGiteaIssueLabeled(payload: IssueLabeledPayload) {
const color = addedLabel.color
? `#${addedLabel.color.replace(/^#/, "")}`
: "#6B7280";
- await db.insert(labelTable).values({
- name: addedLabel.name,
- color,
- taskId: task.id,
- workspaceId: task.project.workspaceId,
- });
+ await db
+ .insert(labelTable)
+ .values({
+ name: addedLabel.name,
+ color,
+ taskId: task.id,
+ workspaceId: task.project.workspaceId,
+ })
+ .onConflictDoNothing({
+ target: [labelTable.taskId, labelTable.name],
+ });
}
}
}
diff --git a/apps/api/src/plugins/github/webhooks/issue-comment-created.ts b/apps/api/src/plugins/github/webhooks/issue-comment-created.ts
index 82aa921c8..e5efecf3f 100644
--- a/apps/api/src/plugins/github/webhooks/issue-comment-created.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-comment-created.ts
@@ -54,15 +54,24 @@ export async function handleIssueCommentCreated(
continue;
}
- await db.insert(activityTable).values({
- taskId: existingLink.taskId,
- type: "comment",
- content: comment.body,
- externalUserName: comment.user?.login ?? "Unknown",
- externalUserAvatar: comment.user?.avatar_url ?? null,
- externalSource: "github",
- externalUrl: comment.html_url,
- });
+ await db
+ .insert(activityTable)
+ .values({
+ taskId: existingLink.taskId,
+ type: "comment",
+ content: comment.body,
+ externalUserName: comment.user?.login ?? "Unknown",
+ externalUserAvatar: comment.user?.avatar_url ?? null,
+ externalSource: "github",
+ externalUrl: comment.html_url,
+ })
+ .onConflictDoNothing({
+ target: [
+ activityTable.taskId,
+ activityTable.externalSource,
+ activityTable.externalUrl,
+ ],
+ });
return;
}
diff --git a/apps/api/src/plugins/github/webhooks/issue-labeled.ts b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
index f8a0b8291..264c0feb1 100644
--- a/apps/api/src/plugins/github/webhooks/issue-labeled.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
@@ -92,12 +92,17 @@ export async function handleIssueLabeled(payload: IssueLabeledPayload) {
if (!existingLabel) {
const color = addedLabel.color ? `#${addedLabel.color}` : "#6B7280";
- await db.insert(labelTable).values({
- name: addedLabel.name,
- color,
- taskId: task.id,
- workspaceId: task.project.workspaceId,
- });
+ await db
+ .insert(labelTable)
+ .values({
+ name: addedLabel.name,
+ color,
+ taskId: task.id,
+ workspaceId: task.project.workspaceId,
+ })
+ .onConflictDoNothing({
+ target: [labelTable.taskId, labelTable.name],
+ });
}
}
}
diff --git a/apps/api/src/plugins/github/webhooks/label-created.ts b/apps/api/src/plugins/github/webhooks/label-created.ts
index 7d809e867..bef816c19 100644
--- a/apps/api/src/plugins/github/webhooks/label-created.ts
+++ b/apps/api/src/plugins/github/webhooks/label-created.ts
@@ -1,4 +1,4 @@
-import { eq } from "drizzle-orm";
+import { eq, sql } from "drizzle-orm";
import db from "../../../database";
import { labelTable, projectTable } from "../../../database/schema";
import { findAllIntegrationsByRepo } from "../services/task-service";
@@ -51,10 +51,16 @@ export async function handleLabelCreated(payload: LabelCreatedPayload) {
const color = label.color ? `#${label.color}` : "#6B7280";
- await db.insert(labelTable).values({
- name: label.name,
- color,
- workspaceId: project.workspaceId,
- });
+ await db
+ .insert(labelTable)
+ .values({
+ name: label.name,
+ color,
+ workspaceId: project.workspaceId,
+ })
+ .onConflictDoNothing({
+ target: [labelTable.workspaceId, labelTable.name],
+ where: sql`${labelTable.taskId} is null`,
+ });
}
}
diff --git a/apps/api/src/task/controllers/bulk-update-tasks.ts b/apps/api/src/task/controllers/bulk-update-tasks.ts
index fed0a8eac..236ed886f 100644
--- a/apps/api/src/task/controllers/bulk-update-tasks.ts
+++ b/apps/api/src/task/controllers/bulk-update-tasks.ts
@@ -173,12 +173,17 @@ async function bulkUpdateTasks({
});
if (!existingAssignment) {
- await db.insert(labelTable).values({
- name: label.name,
- color: label.color,
- workspaceId: workspaceId,
- taskId: task.id,
- });
+ await db
+ .insert(labelTable)
+ .values({
+ name: label.name,
+ color: label.color,
+ workspaceId: workspaceId,
+ taskId: task.id,
+ })
+ .onConflictDoNothing({
+ target: [labelTable.taskId, labelTable.name],
+ });
updatedCount++;
}
}