⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.
TypeScript honoissue-managementissue-trackerjira-alternativekanbanlinear-alternativemcpproject-managementreactself-hostedtypescript
import { and, eq, isNull, sql } from "drizzle-orm";
import { HTTPException } from "hono/http-exception";
import db from "../../database";
import { labelTable, projectTable, taskTable } from "../../database/schema";
import { publishEvent } from "../../events";
import { syncLabelToGitea } from "../../plugins/gitea/utils/sync-label-to-gitea";
import { syncLabelToGitHub } from "../../plugins/github/utils/sync-label-to-github";
import { syncLabelToGitlab } from "../../plugins/gitlab/utils/sync-label-to-gitlab";
import { assertProjectAccess } from "../../project-access/assert-project-access";
async function createLabel(
name: string,
color: string,
taskId: string | undefined,
workspaceId: string,
userId: string,
) {
const deleting = await db.query.labelTable.findFirst({
where: and(
eq(labelTable.workspaceId, workspaceId),
eq(labelTable.name, name),
isNull(labelTable.taskId),
),
});
if (deleting?.deletionStartedAt)
throw new HTTPException(409, { message: "This label is being deleted" });
if (taskId) {
const [task] = await db
.select({
id: taskTable.id,
projectId: taskTable.projectId,
workspaceId: projectTable.workspaceId,
})
.from(taskTable)
.innerJoin(projectTable, eq(taskTable.projectId, projectTable.id))
.where(eq(taskTable.id, taskId))
.limit(1);
if (!task) {
throw new HTTPException(404, {
message: "Task not found",
});
}
if (task.workspaceId !== workspaceId) {
throw new HTTPException(404, {
message: "Task not found",
});
}
await assertProjectAccess(userId, task.projectId);
const [inserted] = await db
.insert(labelTable)
.values({ name, color, taskId, workspaceId: task.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);
});
syncLabelToGitlab(taskId, name, color).catch((error) => {
console.error("Failed to sync label to GitLab:", error);
});
await publishEvent("task.label_created", {
projectId: task.projectId,
taskId: task.id,
userId: userId,
type: "label_created",
});
}
return label;
}
const [inserted] = await db
.insert(labelTable)
.values({ name, color, taskId: null, workspaceId })
.onConflictDoNothing({
target: [labelTable.workspaceId, labelTable.name],
where: sql`${labelTable.taskId} is null`,
})
.returning();
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;
}
export default createLabel;