kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import { eq, sql } from "drizzle-orm";
2import db from "../../../database";
3import { labelTable, projectTable } from "../../../database/schema";
4import { findAllIntegrationsByRepo } from "../services/task-service";
5
6type LabelCreatedPayload = {
7 action: string;
8 label: {
9 name: string;
10 color: string;
11 description?: string | null;
12 };
13 repository: {
14 owner: { login: string };
15 name: string;
16 };
17};
18
19export async function handleLabelCreated(payload: LabelCreatedPayload) {
20 const { repository, label } = payload;
21
22 const integrations = await findAllIntegrationsByRepo(
23 repository.owner.login,
24 repository.name,
25 );
26
27 for (const integration of integrations) {
28 if (!integration.project) {
29 continue;
30 }
31
32 const project = await db.query.projectTable.findFirst({
33 where: eq(projectTable.id, integration.project.id),
34 });
35
36 if (!project?.workspaceId) {
37 continue;
38 }
39
40 const labelExists = await db.query.labelTable.findFirst({
41 where: (table, { and, eq }) =>
42 and(
43 eq(table.workspaceId, project.workspaceId),
44 eq(table.name, label.name),
45 ),
46 });
47
48 if (labelExists) {
49 continue;
50 }
51
52 const color = label.color ? `#${label.color}` : "#6B7280";
53
54 await db
55 .insert(labelTable)
56 .values({
57 name: label.name,
58 color,
59 workspaceId: project.workspaceId,
60 })
61 .onConflictDoNothing({
62 target: [labelTable.workspaceId, labelTable.name],
63 where: sql`${labelTable.taskId} is null`,
64 });
65 }
66}