kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import db from "../../../database";
2import { activityTable } from "../../../database/schema";
3import { findExternalLink } from "../../github/services/link-manager";
4import {
5 findAllIntegrationsByGiteaRepo,
6 repoOwnerLogin,
7} from "../services/integration-lookup";
8import { baseUrlFromRepositoryHtmlUrl } from "../utils/webhook-repo";
9
10type IssueCommentCreatedPayload = {
11 action: string;
12 issue: {
13 number: number;
14 };
15 comment: {
16 id: number;
17 body: string;
18 html_url: string;
19 user: {
20 login?: string;
21 username?: string;
22 avatar_url: string;
23 } | null;
24 created_at: string;
25 };
26 repository: {
27 owner: { login?: string; username?: string };
28 name: string;
29 html_url: string;
30 };
31};
32
33export async function handleGiteaIssueCommentCreated(
34 payload: IssueCommentCreatedPayload,
35) {
36 const { issue, comment, repository } = payload;
37
38 if (payload.action !== "created") {
39 return;
40 }
41
42 const username = comment.user?.login ?? comment.user?.username ?? "";
43 if (username.endsWith("[bot]")) {
44 return;
45 }
46
47 const baseUrl = baseUrlFromRepositoryHtmlUrl(repository.html_url);
48 if (!baseUrl) return;
49
50 const owner = repoOwnerLogin(repository);
51 const integrations = await findAllIntegrationsByGiteaRepo(
52 baseUrl,
53 owner,
54 repository.name,
55 );
56
57 for (const integration of integrations) {
58 const existingLink = await findExternalLink(
59 integration.id,
60 "issue",
61 issue.number.toString(),
62 );
63
64 if (!existingLink) {
65 continue;
66 }
67
68 await db
69 .insert(activityTable)
70 .values({
71 taskId: existingLink.taskId,
72 type: "comment",
73 content: comment.body,
74 externalUserName: username || "Unknown",
75 externalUserAvatar: comment.user?.avatar_url ?? null,
76 externalSource: "gitea",
77 externalUrl: comment.html_url,
78 eventData: {
79 externalCommentId: comment.id,
80 },
81 })
82 .onConflictDoNothing({
83 target: [
84 activityTable.taskId,
85 activityTable.externalSource,
86 activityTable.externalUrl,
87 ],
88 });
89 }
90}