kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { eq } from "drizzle-orm";
2import { HTTPException } from "hono/http-exception";
3import db from "../../database";
4import { labelTable, projectTable, taskTable } from "../../database/schema";
5import {
6 removeLabelFromGitea,
7 syncLabelToGitea,
8} from "../../plugins/gitea/utils/sync-label-to-gitea";
9import {
10 removeLabelFromGitHub,
11 syncLabelToGitHub,
12} from "../../plugins/github/utils/sync-label-to-github";
13
14async function assignLabelToTask(id: string, taskId: string) {
15 const label = await db.query.labelTable.findFirst({
16 where: (label, { eq }) => eq(label.id, id),
17 });
18
19 if (!label) {
20 throw new HTTPException(404, {
21 message: "Label not found",
22 });
23 }
24
25 const [task] = await db
26 .select({
27 id: taskTable.id,
28 workspaceId: projectTable.workspaceId,
29 })
30 .from(taskTable)
31 .innerJoin(projectTable, eq(taskTable.projectId, projectTable.id))
32 .where(eq(taskTable.id, taskId))
33 .limit(1);
34
35 if (!task) {
36 throw new HTTPException(404, {
37 message: "Task not found",
38 });
39 }
40
41 if (label.workspaceId && label.workspaceId !== task.workspaceId) {
42 throw new HTTPException(400, {
43 message: "Label and task must belong to the same workspace",
44 });
45 }
46
47 const [updatedLabel] = await db
48 .update(labelTable)
49 .set({ taskId })
50 .where(eq(labelTable.id, id))
51 .returning();
52
53 if (!updatedLabel) {
54 throw new HTTPException(500, {
55 message: "Failed to attach label to task",
56 });
57 }
58
59 if (label.taskId && label.taskId !== taskId) {
60 removeLabelFromGitHub(label.taskId, label.name).catch((error) => {
61 console.error("Failed to remove label from GitHub:", error);
62 });
63 removeLabelFromGitea(label.taskId, label.name).catch((error) => {
64 console.error("Failed to remove label from Gitea:", error);
65 });
66 }
67
68 syncLabelToGitHub(taskId, updatedLabel.name, updatedLabel.color).catch(
69 (error) => {
70 console.error("Failed to sync label to GitHub:", error);
71 },
72 );
73 syncLabelToGitea(taskId, updatedLabel.name, updatedLabel.color).catch(
74 (error) => {
75 console.error("Failed to sync label to Gitea:", error);
76 },
77 );
78
79 return updatedLabel;
80}
81
82export default assignLabelToTask;