⚡ 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 { dispatchIssueWrite } from "../../sync/dispatch-issue-write";
import { canSyncTask } from "../../sync/eligibility";
import { eq } from "drizzle-orm";
import db from "../../../database";
import { externalLinkTable } from "../../../database/schema";
import type { GiteaConfig } from "../config";
import { createGiteaClient } from "./gitea-api";
const namedColorToHex: Record<string, string> = {
red: "EF4444",
orange: "F97316",
amber: "F59E0B",
yellow: "EAB308",
lime: "84CC16",
green: "22C55E",
emerald: "10B981",
teal: "14B8A6",
cyan: "0092B8",
sky: "00A6F4",
blue: "155DFC",
indigo: "6366F1",
violet: "8B5CF6",
purple: "A855F7",
fuchsia: "C800DE",
pink: "EC4899",
rose: "F43F5E",
gray: "6B7280",
slate: "64748B",
zinc: "71717A",
neutral: "737373",
stone: "78716C",
};
function toHexColor(color: string): string {
const lower = color.toLowerCase().replace(/^#/, "");
if (namedColorToHex[lower]) {
return namedColorToHex[lower];
}
if (/^[0-9a-f]{6}$/i.test(lower)) {
return lower;
}
if (/^[0-9a-f]{3}$/i.test(lower)) {
const [r, g, b] = lower.split("");
return `${r}${r}${g}${g}${b}${b}`;
}
return "6B7280";
}
async function getGiteaIssueContext(taskId: string) {
const externalLinks = await db.query.externalLinkTable.findMany({
where: eq(externalLinkTable.taskId, taskId),
with: {
integration: true,
},
});
const externalLink = externalLinks.find(
(link) =>
link.resourceType === "issue" && link.integration?.type === "gitea",
);
if (!externalLink) {
return null;
}
const integration = externalLink.integration;
if (!integration) {
return null;
}
if (!(await canSyncTask(taskId, integration.id))) return null;
let config: GiteaConfig;
try {
config = JSON.parse(integration.config) as GiteaConfig;
} catch {
return null;
}
if (!config.accessToken || !config.baseUrl) {
return null;
}
const client = createGiteaClient(config);
const issueNumber = Number.parseInt(externalLink.externalId, 10);
if (Number.isNaN(issueNumber)) {
console.warn("Invalid Gitea issue externalId for label sync", {
externalLinkId: externalLink.id,
externalId: externalLink.externalId,
taskId,
});
return null;
}
return {
externalLink,
expectedConfig: integration.config,
client,
config,
issueNumber,
};
}
export async function syncLabelToGitea(
taskId: string,
labelName: string,
labelColor: string,
) {
const ctx = await getGiteaIssueContext(taskId);
if (!ctx) return;
const { client, config, issueNumber } = ctx;
const color = toHexColor(labelColor);
const labels = await client.listLabels(
config.repositoryOwner,
config.repositoryName,
);
let label = labels.find((l) => l.name === labelName);
if (!label) {
try {
const created = await dispatchIssueWrite(
ctx.externalLink,
ctx.expectedConfig,
() =>
client.createLabel(
config.repositoryOwner,
config.repositoryName,
labelName,
color,
),
);
if (!created) return;
label = created.value;
} catch (error) {
console.error(`Failed to create label "${labelName}" in Gitea:`, error);
return;
}
}
try {
const issue = await client.getIssue(
config.repositoryOwner,
config.repositoryName,
issueNumber,
);
const existingIds = (issue.labels ?? []).map((l) => l.id);
if (existingIds.includes(label.id)) {
return;
}
const labelId = label.id;
await dispatchIssueWrite(ctx.externalLink, ctx.expectedConfig, () =>
client.addLabelsToIssue(
config.repositoryOwner,
config.repositoryName,
issueNumber,
[labelId],
),
);
} catch (error) {
console.error(`Failed to add label "${labelName}" to Gitea issue:`, error);
}
}
export async function removeLabelFromGitea(taskId: string, labelName: string) {
const ctx = await getGiteaIssueContext(taskId);
if (!ctx) return;
const { client, config, issueNumber } = ctx;
const labels = await client.listLabels(
config.repositoryOwner,
config.repositoryName,
);
const label = labels.find((l) => l.name === labelName);
if (!label) return;
try {
await dispatchIssueWrite(ctx.externalLink, ctx.expectedConfig, () =>
client.removeLabelFromIssue(
config.repositoryOwner,
config.repositoryName,
issueNumber,
label.id,
),
);
} catch (error) {
console.error(
`Failed to remove label "${labelName}" from Gitea issue:`,
error,
);
}
}