Skip to content

armgit / kaneo

Public Imported from GitHub
0

⚡ Project management that doesn't become the project. Boards, Gantt charts, and GitHub integration. Self-host it or use Kaneo Cloud.

feat: implement task comment creation event handling and integrate with GitHub plugin

Andrej Acevski089421dce8c9
diff --git a/apps/api/src/activity/index.ts b/apps/api/src/activity/index.ts
index 5145ba147..ab26b626d 100644
--- a/apps/api/src/activity/index.ts
+++ b/apps/api/src/activity/index.ts
@@ -1,7 +1,10 @@
+import { eq } from "drizzle-orm";
import { Hono } from "hono";
import { describeRoute, resolver, validator } from "hono-openapi";
import * as v from "valibot";
-import { subscribeToEvent } from "../events";
+import db from "../database";
+import { taskTable } from "../database/schema";
+import { publishEvent, subscribeToEvent } from "../events";
import { activitySchema } from "../schemas";
import { workspaceAccess } from "../utils/workspace-access-middleware";
import createActivity from "./controllers/create-activity";
@@ -96,6 +99,21 @@ const activity = new Hono<{
const { taskId, comment } = c.req.valid("json");
const userId = c.get("userId");
const newComment = await createComment(taskId, userId, comment);
+
+ const [task] = await db
+ .select({ projectId: taskTable.projectId })
+ .from(taskTable)
+ .where(eq(taskTable.id, taskId));
+
+ if (task) {
+ await publishEvent("task.comment_created", {
+ taskId,
+ userId,
+ comment,
+ projectId: task.projectId,
+ });
+ }
+
return c.json(newComment);
},
)
diff --git a/apps/api/src/github-integration/controllers/create-github-integration.ts b/apps/api/src/github-integration/controllers/create-github-integration.ts
index 10e66acd0..e61d6dcb8 100644
--- a/apps/api/src/github-integration/controllers/create-github-integration.ts
+++ b/apps/api/src/github-integration/controllers/create-github-integration.ts
@@ -30,6 +30,32 @@ async function createGithubIntegration({
throw new HTTPException(404, { message: "Project not found" });
}
+ const allGitHubIntegrations = await db.query.integrationTable.findMany({
+ where: eq(integrationTable.type, "github"),
+ });
+
+ for (const integration of allGitHubIntegrations) {
+ if (integration.projectId === projectId) {
+ continue;
+ }
+
+ try {
+ const config = JSON.parse(integration.config);
+ if (
+ config.repositoryOwner === repositoryOwner &&
+ config.repositoryName === repositoryName
+ ) {
+ throw new HTTPException(409, {
+ message: `Repository ${repositoryOwner}/${repositoryName} is already linked to another project`,
+ });
+ }
+ } catch (error) {
+ if (error instanceof HTTPException) {
+ throw error;
+ }
+ }
+ }
+
let installationId: number | null = null;
try {
const { data: installation } =
diff --git a/apps/api/src/plugins/github/events/task-comment-created.ts b/apps/api/src/plugins/github/events/task-comment-created.ts
new file mode 100644
index 000000000..6c51e55c6
--- /dev/null
+++ b/apps/api/src/plugins/github/events/task-comment-created.ts
@@ -0,0 +1,50 @@
+import type { PluginContext, TaskCommentCreatedEvent } from "../../types";
+import type { GitHubConfig } from "../config";
+import { findExternalLinkByTaskAndType } from "../services/link-manager";
+import { getGithubApp, getInstallationIdForRepo } from "../utils/github-app";
+
+export async function handleTaskCommentCreated(
+ event: TaskCommentCreatedEvent,
+ context: PluginContext,
+): Promise<void> {
+ const githubApp = getGithubApp();
+ if (!githubApp) {
+ return;
+ }
+
+ const config = context.config as GitHubConfig;
+ const { repositoryOwner, repositoryName } = config;
+
+ const existingLink = await findExternalLinkByTaskAndType(
+ event.taskId,
+ context.integrationId,
+ "issue",
+ );
+
+ if (!existingLink) {
+ return;
+ }
+
+ try {
+ let installationId = config.installationId;
+ if (!installationId) {
+ installationId = await getInstallationIdForRepo(
+ repositoryOwner,
+ repositoryName,
+ );
+ }
+
+ const octokit = await githubApp.getInstallationOctokit(installationId);
+
+ const issueNumber = Number.parseInt(existingLink.externalId, 10);
+
+ await octokit.rest.issues.createComment({
+ owner: repositoryOwner,
+ repo: repositoryName,
+ issue_number: issueNumber,
+ body: event.comment,
+ });
+ } catch (error) {
+ console.error("Failed to create GitHub comment:", error);
+ }
+}
diff --git a/apps/api/src/plugins/github/index.ts b/apps/api/src/plugins/github/index.ts
index b2afbfe16..56ee433fd 100644
--- a/apps/api/src/plugins/github/index.ts
+++ b/apps/api/src/plugins/github/index.ts
@@ -1,5 +1,6 @@
import type { IntegrationPlugin } from "../types";
import { validateGitHubConfig } from "./config";
+import { handleTaskCommentCreated } from "./events/task-comment-created";
import { handleTaskCreated } from "./events/task-created";
import { handleTaskDescriptionChanged } from "./events/task-description-changed";
import { handleTaskPriorityChanged } from "./events/task-priority-changed";
@@ -15,6 +16,7 @@ export const githubPlugin: IntegrationPlugin = {
onTaskPriorityChanged: handleTaskPriorityChanged,
onTaskTitleChanged: handleTaskTitleChanged,
onTaskDescriptionChanged: handleTaskDescriptionChanged,
+ onTaskCommentCreated: handleTaskCommentCreated,
validateConfig: validateGitHubConfig,
};
diff --git a/apps/api/src/plugins/registry.ts b/apps/api/src/plugins/registry.ts
index 0d5a4b350..640fe6515 100644
--- a/apps/api/src/plugins/registry.ts
+++ b/apps/api/src/plugins/registry.ts
@@ -5,6 +5,7 @@ import { subscribeToEvent } from "../events";
import type {
IntegrationPlugin,
PluginContext,
+ TaskCommentCreatedEvent,
TaskCreatedEvent,
TaskDescriptionChangedEvent,
TaskPriorityChangedEvent,
@@ -118,6 +119,20 @@ export function initializeEventSubscriptions(): void {
});
});
+ subscribeToEvent<{
+ taskId: string;
+ userId: string;
+ comment: string;
+ projectId: string;
+ }>("task.comment_created", async (data) => {
+ await broadcastTaskCommentCreated({
+ taskId: data.taskId,
+ projectId: data.projectId,
+ userId: data.userId,
+ comment: data.comment,
+ });
+ });
+
eventSubscriptionsInitialized = true;
console.log("✓ Plugin event subscriptions initialized");
}
@@ -260,3 +275,25 @@ export async function broadcastTaskDescriptionChanged(
}
}
}
+
+export async function broadcastTaskCommentCreated(
+ event: TaskCommentCreatedEvent,
+): Promise<void> {
+ const integrations = await getActiveIntegrations(event.projectId);
+
+ for (const integration of integrations) {
+ const plugin = getPlugin(integration.type);
+ if (!plugin?.onTaskCommentCreated) continue;
+
+ const context = createContext(integration);
+
+ try {
+ await plugin.onTaskCommentCreated(event, context);
+ } catch (error) {
+ console.error(
+ `Plugin ${plugin.type} error on task.comment_created:`,
+ error,
+ );
+ }
+ }
+}
diff --git a/apps/api/src/plugins/types.ts b/apps/api/src/plugins/types.ts
index 5b3deac96..436ec286f 100644
--- a/apps/api/src/plugins/types.ts
+++ b/apps/api/src/plugins/types.ts
@@ -49,12 +49,20 @@ export type TaskDescriptionChangedEvent = {
newDescription: string | null;
};
+export type TaskCommentCreatedEvent = {
+ taskId: string;
+ projectId: string;
+ userId: string;
+ comment: string;
+};
+
export type TaskEvent =
| TaskCreatedEvent
| TaskStatusChangedEvent
| TaskPriorityChangedEvent
| TaskTitleChangedEvent
- | TaskDescriptionChangedEvent;
+ | TaskDescriptionChangedEvent
+ | TaskCommentCreatedEvent;
export type ExternalMetadata = {
type: "issue" | "pull_request" | "branch";
@@ -94,6 +102,7 @@ export type IntegrationPlugin = {
onTaskPriorityChanged?: TaskEventHandler<TaskPriorityChangedEvent>;
onTaskTitleChanged?: TaskEventHandler<TaskTitleChangedEvent>;
onTaskDescriptionChanged?: TaskEventHandler<TaskDescriptionChangedEvent>;
+ onTaskCommentCreated?: TaskEventHandler<TaskCommentCreatedEvent>;
handleWebhook?: WebhookHandler;
getTaskMetadata?: MetadataProvider;