⚡ 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
fix(api): harden gitea integration imports
Tin9db74f96a8d4
diff --git a/apps/api/drizzle/0020_gitea_dedup_guards.sql b/apps/api/drizzle/0020_gitea_dedup_guards.sqlnew file mode 100644index 000000000..642d3c392--- /dev/null+++ b/apps/api/drizzle/0020_gitea_dedup_guards.sql@@ -0,0 +1,35 @@+DELETE FROM "activity" a+USING "activity" b+WHERE a.ctid < b.ctid+ AND a.task_id = b.task_id+ AND a.external_source = b.external_source+ AND a.external_url = b.external_url+ AND a.external_url IS NOT NULL;++DELETE FROM "label" a+USING "label" b+WHERE a.ctid < b.ctid+ AND a.task_id = b.task_id+ AND a.name = b.name+ AND a.task_id IS NOT NULL;++DELETE FROM "label" a+USING "label" b+WHERE a.ctid < b.ctid+ AND a.workspace_id = b.workspace_id+ AND a.name = b.name+ AND a.task_id IS NULL+ AND b.task_id IS NULL+ AND a.workspace_id IS NOT NULL;++ALTER TABLE "activity"+ ADD CONSTRAINT "activity_task_external_source_external_url_unique"+ UNIQUE ("task_id", "external_source", "external_url");++ALTER TABLE "label"+ ADD CONSTRAINT "label_task_name_unique"+ UNIQUE ("task_id", "name");++CREATE UNIQUE INDEX "label_workspace_name_unique"+ ON "label" ("workspace_id", "name")+ WHERE "task_id" IS NULL;diff --git a/apps/api/drizzle/meta/_journal.json b/apps/api/drizzle/meta/_journal.jsonindex 37f81d76c..897220b15 100644--- a/apps/api/drizzle/meta/_journal.json+++ b/apps/api/drizzle/meta/_journal.json@@ -141,6 +141,13 @@"when": 1774823200346,"tag": "0019_sloppy_meteorite","breakpoints": true+ },+ {+ "idx": 20,+ "version": "7",+ "when": 1774908000000,+ "tag": "0020_gitea_dedup_guards",+ "breakpoints": true}]}diff --git a/apps/api/src/database/schema.ts b/apps/api/src/database/schema.tsindex e46fcef0e..a54a049b7 100644--- a/apps/api/src/database/schema.ts+++ b/apps/api/src/database/schema.ts@@ -1,5 +1,5 @@import { createId } from "@paralleldrive/cuid2";-import { relations } from "drizzle-orm";+import { relations, sql } from "drizzle-orm";import {boolean,index,@@ -9,6 +9,7 @@ import {text,timestamp,unique,+ uniqueIndex,} from "drizzle-orm/pg-core";export const userTable = pgTable("user", {@@ -320,29 +321,39 @@ export const timeEntryTable = pgTable("time_entry", {createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(),});-export const activityTable = pgTable("activity", {- id: text("id")- .$defaultFn(() => createId())- .primaryKey(),- taskId: text("task_id")- .notNull()- .references(() => taskTable.id, {+export const activityTable = pgTable(+ "activity",+ {+ id: text("id")+ .$defaultFn(() => createId())+ .primaryKey(),+ taskId: text("task_id")+ .notNull()+ .references(() => taskTable.id, {+ onDelete: "cascade",+ onUpdate: "cascade",+ }),+ type: text("type").notNull(),+ createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(),+ userId: text("user_id").references(() => userTable.id, {onDelete: "cascade",onUpdate: "cascade",}),- type: text("type").notNull(),- createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(),- userId: text("user_id").references(() => userTable.id, {- onDelete: "cascade",- onUpdate: "cascade",- }),- content: text("content"),- eventData: jsonb("event_data"),- externalUserName: text("external_user_name"),- externalUserAvatar: text("external_user_avatar"),- externalSource: text("external_source"),- externalUrl: text("external_url"),-});+ content: text("content"),+ eventData: jsonb("event_data"),+ externalUserName: text("external_user_name"),+ externalUserAvatar: text("external_user_avatar"),+ externalSource: text("external_source"),+ externalUrl: text("external_url"),+ },+ (table) => [+ unique("activity_task_external_source_external_url_unique").on(+ table.taskId,+ table.externalSource,+ table.externalUrl,+ ),+ ],+);export const assetTable = pgTable("asset",@@ -390,22 +401,31 @@ export const assetTable = pgTable(],);-export const labelTable = pgTable("label", {- id: text("id")- .$defaultFn(() => createId())- .primaryKey(),- name: text("name").notNull(),- color: text("color").notNull(),- createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(),- taskId: text("task_id").references(() => taskTable.id, {- onDelete: "cascade",- onUpdate: "cascade",- }),- workspaceId: text("workspace_id").references(() => workspaceTable.id, {- onDelete: "cascade",- onUpdate: "cascade",- }),-});+export const labelTable = pgTable(+ "label",+ {+ id: text("id")+ .$defaultFn(() => createId())+ .primaryKey(),+ name: text("name").notNull(),+ color: text("color").notNull(),+ createdAt: timestamp("created_at", { mode: "date" }).defaultNow().notNull(),+ taskId: text("task_id").references(() => taskTable.id, {+ onDelete: "cascade",+ onUpdate: "cascade",+ }),+ workspaceId: text("workspace_id").references(() => workspaceTable.id, {+ onDelete: "cascade",+ onUpdate: "cascade",+ }),+ },+ (table) => [+ unique("label_task_name_unique").on(table.taskId, table.name),+ uniqueIndex("label_workspace_name_unique")+ .on(table.workspaceId, table.name)+ .where(sql`${table.taskId} is null`),+ ],+);export const notificationTable = pgTable("notification", {id: text("id")diff --git a/apps/api/src/gitea-integration/controllers/create-gitea-integration.ts b/apps/api/src/gitea-integration/controllers/create-gitea-integration.tsindex a4fcc868c..9fd9611ee 100644--- a/apps/api/src/gitea-integration/controllers/create-gitea-integration.ts+++ b/apps/api/src/gitea-integration/controllers/create-gitea-integration.ts@@ -46,8 +46,15 @@ async function createGiteaIntegration({let resolvedToken = accessToken?.trim() ?? "";if (!resolvedToken && existingIntegration) {- const prev = JSON.parse(existingIntegration.config) as GiteaConfig;- resolvedToken = prev.accessToken;+ try {+ const prev = JSON.parse(existingIntegration.config) as GiteaConfig;+ resolvedToken = prev.accessToken;+ } catch (error) {+ console.warn("Failed to parse existing Gitea integration config", {+ integrationId: existingIntegration.id,+ error,+ });+ }}if (!resolvedToken) {@@ -91,13 +98,31 @@ async function createGiteaIntegration({if (error instanceof HTTPException) {throw error;}+ console.warn(+ "Skipping invalid Gitea integration config during conflict check",+ {+ integrationId: integration.id,+ error,+ },+ );+ throw error;}}- const webhookSecret = existingIntegration- ? ((JSON.parse(existingIntegration.config) as GiteaConfig).webhookSecret ??- randomBytes(24).toString("hex"))- : randomBytes(24).toString("hex");+ let webhookSecret = randomBytes(24).toString("hex");+ if (existingIntegration) {+ try {+ const previousConfig = JSON.parse(+ existingIntegration.config,+ ) as GiteaConfig;+ webhookSecret = previousConfig.webhookSecret ?? webhookSecret;+ } catch (error) {+ console.warn("Failed to parse existing Gitea config for webhook secret", {+ integrationId: existingIntegration.id,+ error,+ });+ }+ }const config: GiteaConfig = getDefaultGiteaConfig(normalizedBase,diff --git a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts b/apps/api/src/gitea-integration/controllers/import-gitea-issues.tsindex 2055746b6..58e4799fc 100644--- a/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts+++ b/apps/api/src/gitea-integration/controllers/import-gitea-issues.ts@@ -8,11 +8,13 @@ import {projectTable,taskTable,} from "../../database/schema";+import { publishEvent } from "../../events";import type { GiteaConfig } from "../../plugins/gitea/config";import { extractTaskNumberGitea } from "../../plugins/gitea/utils/branch-matcher";import {createGiteaClient,type GiteaIssue,+ type GiteaLabel,type GiteaPullRequest,} from "../../plugins/gitea/utils/gitea-api";import {@@ -34,6 +36,12 @@ type ImportResult = {errors?: string[];};+type LabelLike = { name?: string };++function toPriorityLabels(labels: GiteaLabel[]): LabelLike[] {+ return labels.map((label) => ({ name: label.name }));+}+export async function importGiteaIssues(projectId: string,): Promise<ImportResult> {@@ -185,8 +193,9 @@ async function importSingleIssue();const labels = issue.labels ?? [];- const priority = extractIssuePriority(labels as never);- const status = extractIssueStatus(labels as never);+ const adaptedLabels = toPriorityLabels(labels);+ const priority = extractIssuePriority(adaptedLabels);+ const status = extractIssueStatus(adaptedLabels);if (existingLink) {const updateData: Record<string, unknown> = {@@ -253,6 +262,17 @@ async function importSingleIssue(await importCommentsForTask(issue.number, createdTask.id, config, client);+ await publishEvent("task.created", {+ ...createdTask,+ taskId: createdTask.id,+ userId: createdTask.userId ?? "",+ type: "task",+ content: null,+ source: "gitea-import",+ integrationId,+ externalId: issue.number.toString(),+ });+return "imported";}@@ -339,6 +359,7 @@ async function importCommentsForTask(client: ReturnType<typeof createGiteaClient>,): Promise<void> {const allComments: Array<{+ id: number;body: string;html_url: string;user?: { login?: string; username?: string; avatar_url?: string } | null;@@ -362,36 +383,33 @@ async function importCommentsForTask(page++;}- const existingActivities = await db.query.activityTable.findMany({- where: and(- eq(activityTable.taskId, taskId),- eq(activityTable.externalSource, "gitea"),- ),- });-- const existingExternalUrls = new Set(- existingActivities.filter((a) => a.externalUrl).map((a) => a.externalUrl),- );-for (const comment of allComments) {const username = comment.user?.login ?? comment.user?.username ?? "";if (username.endsWith("[bot]")) {continue;}- if (existingExternalUrls.has(comment.html_url)) {- continue;- }-- await db.insert(activityTable).values({- taskId,- type: "comment",- content: comment.body,- externalUserName: username || "Unknown",- externalUserAvatar: comment.user?.avatar_url ?? null,- externalSource: "gitea",- externalUrl: comment.html_url,- });+ await db+ .insert(activityTable)+ .values({+ taskId,+ type: "comment",+ content: comment.body,+ externalUserName: username || "Unknown",+ externalUserAvatar: comment.user?.avatar_url ?? null,+ externalSource: "gitea",+ externalUrl: comment.html_url,+ eventData: {+ externalCommentId: comment.id,+ },+ })+ .onConflictDoNothing({+ target: [+ activityTable.taskId,+ activityTable.externalSource,+ activityTable.externalUrl,+ ],+ });}}diff --git a/apps/api/src/plugins/gitea/utils/gitea-api.ts b/apps/api/src/plugins/gitea/utils/gitea-api.tsindex 4809d4826..ac49d5f66 100644--- a/apps/api/src/plugins/gitea/utils/gitea-api.ts+++ b/apps/api/src/plugins/gitea/utils/gitea-api.ts@@ -62,7 +62,7 @@ export async function giteaFetch<T>(token: string,path: string,init?: RequestInit,-): Promise<T> {+): Promise<T | undefined> {const root = normalizeGiteaBaseUrl(baseUrl);const url = `${root}/api/v1${path.startsWith("/") ? path : `/${path}`}`;const res = await fetch(url, {@@ -79,7 +79,7 @@ export async function giteaFetch<T>(}if (res.status === 204 || text === "") {- return undefined as T;+ return undefined;}try {@@ -101,18 +101,43 @@ export function createGiteaClient(`/repos/${encodeURIComponent(o)}/${encodeURIComponent(r)}`;return {- async getRepo(repositoryOwner: string, repositoryName: string) {- return giteaFetch<{+ async getRepo(+ repositoryOwner: string,+ repositoryName: string,+ ): Promise<{+ name: string;+ owner: { login?: string; username?: string };+ html_url: string;+ private: boolean;+ permissions?: { admin?: boolean; push?: boolean; pull?: boolean };+ }> {+ const repo = await giteaFetch<{name: string;owner: { login?: string; username?: string };html_url: string;private: boolean;permissions?: { admin?: boolean; push?: boolean; pull?: boolean };}>(baseUrl, accessToken, owner(repositoryOwner, repositoryName));+ if (!repo) {+ throw new GiteaApiError("Gitea repository response was empty", 500);+ }+ return repo;},- async listUserRepos(page = 1, limit = 50) {- return giteaFetch<+ async listUserRepos(+ page = 1,+ limit = 50,+ ): Promise<+ Array<{+ id: number;+ name: string;+ full_name: string;+ owner: { login?: string; username?: string };+ private: boolean;+ html_url: string;+ }>+ > {+ const repos = await giteaFetch<Array<{id: number;name: string;@@ -122,14 +147,18 @@ export function createGiteaClient(html_url: string;}>>(baseUrl, accessToken, `/user/repos?page=${page}&limit=${limit}`);+ if (!repos) {+ throw new GiteaApiError("Gitea repositories response was empty", 500);+ }+ return repos;},async createIssue(repositoryOwner: string,repositoryName: string,body: { title: string; body?: string | null; closed?: boolean },- ) {- return giteaFetch<GiteaIssue>(+ ): Promise<GiteaIssue> {+ const issue = await giteaFetch<GiteaIssue>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/issues`,@@ -138,6 +167,10 @@ export function createGiteaClient(body: JSON.stringify(body),},);+ if (!issue) {+ throw new GiteaApiError("Gitea create issue response was empty", 500);+ }+ return issue;},async updateIssue(@@ -145,8 +178,8 @@ export function createGiteaClient(repositoryName: string,index: number,body: Record<string, unknown>,- ) {- return giteaFetch<GiteaIssue>(+ ): Promise<GiteaIssue> {+ const issue = await giteaFetch<GiteaIssue>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/issues/${index}`,@@ -155,6 +188,10 @@ export function createGiteaClient(body: JSON.stringify(body),},);+ if (!issue) {+ throw new GiteaApiError("Gitea update issue response was empty", 500);+ }+ return issue;},async listIssueComments(@@ -163,12 +200,16 @@ export function createGiteaClient(index: number,page: number,limit: number,- ) {- return giteaFetch<GiteaComment[]>(+ ): Promise<GiteaComment[]> {+ const comments = await giteaFetch<GiteaComment[]>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/issues/${index}/comments?page=${page}&limit=${limit}`,);+ if (!comments) {+ throw new GiteaApiError("Gitea comments response was empty", 500);+ }+ return comments;},async createIssueComment(@@ -176,8 +217,8 @@ export function createGiteaClient(repositoryName: string,index: number,body: string,- ) {- return giteaFetch<GiteaComment>(+ ): Promise<GiteaComment> {+ const comment = await giteaFetch<GiteaComment>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/issues/${index}/comments`,@@ -186,14 +227,25 @@ export function createGiteaClient(body: JSON.stringify({ body }),},);+ if (!comment) {+ throw new GiteaApiError("Gitea create comment response was empty", 500);+ }+ return comment;},- async listLabels(repositoryOwner: string, repositoryName: string) {- return giteaFetch<GiteaLabel[]>(+ async listLabels(+ repositoryOwner: string,+ repositoryName: string,+ ): Promise<GiteaLabel[]> {+ const labels = await giteaFetch<GiteaLabel[]>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/labels`,);+ if (!labels) {+ throw new GiteaApiError("Gitea labels response was empty", 500);+ }+ return labels;},async createLabel(@@ -201,8 +253,8 @@ export function createGiteaClient(repositoryName: string,name: string,color: string,- ) {- return giteaFetch<GiteaLabel>(+ ): Promise<GiteaLabel> {+ const label = await giteaFetch<GiteaLabel>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/labels`,@@ -214,6 +266,10 @@ export function createGiteaClient(}),},);+ if (!label) {+ throw new GiteaApiError("Gitea create label response was empty", 500);+ }+ return label;},async addLabelsToIssue(@@ -271,12 +327,16 @@ export function createGiteaClient(repositoryOwner: string,repositoryName: string,index: number,- ) {- return giteaFetch<GiteaIssue>(+ ): Promise<GiteaIssue> {+ const issue = await giteaFetch<GiteaIssue>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/issues/${index}`,);+ if (!issue) {+ throw new GiteaApiError("Gitea issue response was empty", 500);+ }+ return issue;},async listIssues(@@ -284,32 +344,44 @@ export function createGiteaClient(repositoryName: string,page: number,state: "open" | "closed" | "all",- ) {- return giteaFetch<GiteaIssue[]>(+ ): Promise<GiteaIssue[]> {+ const issues = await giteaFetch<GiteaIssue[]>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/issues?state=${state}&page=${page}&limit=100`,);+ if (!issues) {+ throw new GiteaApiError("Gitea issues response was empty", 500);+ }+ return issues;},async listPulls(repositoryOwner: string,repositoryName: string,page: number,- ) {- return giteaFetch<GiteaPullRequest[]>(+ ): Promise<GiteaPullRequest[]> {+ const pulls = await giteaFetch<GiteaPullRequest[]>(baseUrl,accessToken,`${owner(repositoryOwner, repositoryName)}/pulls?state=open&page=${page}&limit=100`,);+ if (!pulls) {+ throw new GiteaApiError("Gitea pull requests response was empty", 500);+ }+ return pulls;},};}export async function verifyGiteaToken(baseUrl: string, token: string) {- return giteaFetch<{ id: number; login: string }>(+ const user = await giteaFetch<{ id: number; login: string }>(normalizeGiteaBaseUrl(baseUrl),token,"/user",);+ if (!user) {+ throw new GiteaApiError("Gitea user response was empty", 500);+ }+ return user;}diff --git a/apps/api/src/plugins/gitea/utils/labels.ts b/apps/api/src/plugins/gitea/utils/labels.tsindex 21db069e0..135ce9e68 100644--- a/apps/api/src/plugins/gitea/utils/labels.ts+++ b/apps/api/src/plugins/gitea/utils/labels.ts@@ -24,12 +24,7 @@ async function getOrCreateLabelId(name: string,): Promise<number> {const { repositoryOwner, repositoryName } = config;- let labels: GiteaLabel[];- try {- labels = await client.listLabels(repositoryOwner, repositoryName);- } catch {- labels = [];- }+ const labels = await client.listLabels(repositoryOwner, repositoryName);const found = labels.find((l) => l.name === name);if (found) {