kaneo (minimalist kanban) fork to experiment adding a tangled integration
github.com/usekaneo/kaneo
1import type { PluginContext, TaskCreatedEvent } from "../../types";
2import type { GitHubConfig } from "../config";
3import {
4 createExternalLink,
5 findExternalLinkByTaskAndType,
6} from "../services/link-manager";
7import {
8 formatIssueBody,
9 formatIssueTitle,
10 getLabelsForIssue,
11} from "../utils/format";
12import { getGithubApp, getInstallationIdForRepo } from "../utils/github-app";
13import { addLabelsToIssue } from "../utils/labels";
14
15export async function handleTaskCreated(
16 event: TaskCreatedEvent,
17 context: PluginContext,
18): Promise<void> {
19 const githubApp = getGithubApp();
20 if (!githubApp) {
21 return;
22 }
23
24 const config = context.config as GitHubConfig;
25 const { repositoryOwner, repositoryName } = config;
26
27 const existingLink = await findExternalLinkByTaskAndType(
28 event.taskId,
29 context.integrationId,
30 "issue",
31 );
32
33 if (existingLink) {
34 return;
35 }
36
37 try {
38 let installationId = config.installationId;
39 if (!installationId) {
40 installationId = await getInstallationIdForRepo(
41 repositoryOwner,
42 repositoryName,
43 );
44 }
45
46 const octokit = await githubApp.getInstallationOctokit(installationId);
47
48 const createdIssue = await octokit.rest.issues.create({
49 owner: repositoryOwner,
50 repo: repositoryName,
51 title: formatIssueTitle(event.title),
52 body: formatIssueBody(event.description, event.taskId),
53 });
54
55 const labels = getLabelsForIssue(event.priority, event.status);
56 await addLabelsToIssue(
57 octokit,
58 repositoryOwner,
59 repositoryName,
60 createdIssue.data.number,
61 labels,
62 );
63
64 await createExternalLink({
65 taskId: event.taskId,
66 integrationId: context.integrationId,
67 resourceType: "issue",
68 externalId: createdIssue.data.number.toString(),
69 url: createdIssue.data.html_url,
70 title: createdIssue.data.title,
71 metadata: {
72 state: createdIssue.data.state,
73 createdFrom: "kaneo",
74 },
75 });
76 } catch (error) {
77 console.error("Failed to create GitHub issue:", error);
78 }
79}