kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { and, eq, isNull, sql } from "drizzle-orm";
2import db from "../../database";
3import { labelTable } from "../../database/schema";
4import { syncLabelToGitea } from "../../plugins/gitea/utils/sync-label-to-gitea";
5import { syncLabelToGitHub } from "../../plugins/github/utils/sync-label-to-github";
6
7async function createLabel(
8 name: string,
9 color: string,
10 taskId: string | undefined,
11 workspaceId: string,
12) {
13 if (taskId) {
14 const [inserted] = await db
15 .insert(labelTable)
16 .values({ name, color, taskId, workspaceId })
17 .onConflictDoNothing({
18 target: [labelTable.taskId, labelTable.name],
19 })
20 .returning();
21
22 const label =
23 inserted ??
24 (await db.query.labelTable.findFirst({
25 where: and(eq(labelTable.taskId, taskId), eq(labelTable.name, name)),
26 }));
27
28 if (!label) {
29 throw new Error("Failed to create or resolve label");
30 }
31
32 if (inserted) {
33 syncLabelToGitHub(taskId, name, color).catch((error) => {
34 console.error("Failed to sync label to GitHub:", error);
35 });
36 syncLabelToGitea(taskId, name, color).catch((error) => {
37 console.error("Failed to sync label to Gitea:", error);
38 });
39 }
40
41 return label;
42 }
43
44 const [inserted] = await db
45 .insert(labelTable)
46 .values({ name, color, taskId: null, workspaceId })
47 .onConflictDoNothing({
48 target: [labelTable.workspaceId, labelTable.name],
49 where: sql`${labelTable.taskId} is null`,
50 })
51 .returning();
52
53 const label =
54 inserted ??
55 (await db.query.labelTable.findFirst({
56 where: and(
57 eq(labelTable.workspaceId, workspaceId),
58 eq(labelTable.name, name),
59 isNull(labelTable.taskId),
60 ),
61 }));
62
63 if (!label) {
64 throw new Error("Failed to create or resolve label");
65 }
66
67 return label;
68}
69
70export default createLabel;