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.

fix: emit task.status_changed for git webhook status updates (#1263)

Tin Sever89c8207052ce
diff --git a/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts b/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts
index f76652ff4..9e7fc9f57 100644
--- a/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts
+++ b/apps/api/src/plugins/gitea/webhooks/pull-request-closed.ts
@@ -1,6 +1,7 @@
import { and, eq } from "drizzle-orm";
import db from "../../../database";
import { externalLinkTable } from "../../../database/schema";
+import { publishEvent } from "../../../events";
import { updateExternalLink } from "../../github/services/link-manager";
import {
findTaskById,
@@ -101,7 +102,22 @@ export async function handleGiteaPullRequestClosed(payload: PRClosedPayload) {
"pr_merged",
config.statusTransitions?.onPRMerge || "done",
);
- await updateTaskStatus(task.id, targetStatus);
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
}
}
diff --git a/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts b/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts
index 3806150fb..e436ebc24 100644
--- a/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts
+++ b/apps/api/src/plugins/gitea/webhooks/pull-request-opened.ts
@@ -1,3 +1,4 @@
+import { publishEvent } from "../../../events";
import {
createExternalLink,
findExternalLink,
@@ -122,7 +123,22 @@ export async function handleGiteaPullRequestOpened(payload: PROpenedPayload) {
const isTaskFinal = await isTaskInFinalState(task);
if (task.status !== targetStatus && !isTaskFinal) {
- await updateTaskStatus(task.id, targetStatus);
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
}
return;
diff --git a/apps/api/src/plugins/gitea/webhooks/push.ts b/apps/api/src/plugins/gitea/webhooks/push.ts
index 755b7f1b6..b798f9940 100644
--- a/apps/api/src/plugins/gitea/webhooks/push.ts
+++ b/apps/api/src/plugins/gitea/webhooks/push.ts
@@ -1,3 +1,4 @@
+import { publishEvent } from "../../../events";
import { createOrUpdateExternalLink } from "../../github/services/link-manager";
import {
findTaskByNumber,
@@ -140,7 +141,22 @@ export async function handleGiteaPush(payload: PushPayload) {
const isTaskFinal = await isTaskInFinalState(task);
if (task.status !== targetStatus && !isTaskFinal) {
- await updateTaskStatus(task.id, targetStatus);
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
}
}
}
diff --git a/apps/api/src/plugins/github/webhooks/issue-closed.ts b/apps/api/src/plugins/github/webhooks/issue-closed.ts
index f63f11f5e..10e622595 100644
--- a/apps/api/src/plugins/github/webhooks/issue-closed.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-closed.ts
@@ -1,6 +1,7 @@
import { and, eq } from "drizzle-orm";
import db from "../../../database";
import { externalLinkTable, taskTable } from "../../../database/schema";
+import { publishEvent } from "../../../events";
import { updateExternalLink } from "../services/link-manager";
import {
findAllIntegrationsByRepo,
@@ -66,7 +67,22 @@ export async function handleIssueClosed(payload: IssueClosedPayload) {
"done",
);
- await updateTaskStatus(task.id, targetStatus);
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
await updateExternalLink(externalLink.id, {
metadata: {
diff --git a/apps/api/src/plugins/github/webhooks/issue-labeled.ts b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
index 264c0feb1..877e2966c 100644
--- a/apps/api/src/plugins/github/webhooks/issue-labeled.ts
+++ b/apps/api/src/plugins/github/webhooks/issue-labeled.ts
@@ -1,6 +1,7 @@
import { eq } from "drizzle-orm";
import db from "../../../database";
import { labelTable, taskTable } from "../../../database/schema";
+import { publishEvent } from "../../../events";
import { findExternalLink } from "../services/link-manager";
import {
findAllIntegrationsByRepo,
@@ -57,7 +58,22 @@ export async function handleIssueLabeled(payload: IssueLabeledPayload) {
}
if (status) {
- await updateTaskStatus(existingLink.taskId, status);
+ const statusResult = await updateTaskStatus(existingLink.taskId, status);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
}
if (!addedLabel) {
diff --git a/apps/api/src/plugins/github/webhooks/pull-request-closed.ts b/apps/api/src/plugins/github/webhooks/pull-request-closed.ts
index 471a24b72..fc674e573 100644
--- a/apps/api/src/plugins/github/webhooks/pull-request-closed.ts
+++ b/apps/api/src/plugins/github/webhooks/pull-request-closed.ts
@@ -1,6 +1,7 @@
import { and, eq } from "drizzle-orm";
import db from "../../../database";
import { externalLinkTable } from "../../../database/schema";
+import { publishEvent } from "../../../events";
import type { GitHubConfig } from "../config";
import { updateExternalLink } from "../services/link-manager";
import {
@@ -91,7 +92,22 @@ export async function handlePullRequestClosed(payload: PRClosedPayload) {
"pr_merged",
config.statusTransitions?.onPRMerge || "done",
);
- await updateTaskStatus(task.id, targetStatus);
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
}
}
diff --git a/apps/api/src/plugins/github/webhooks/pull-request-opened.ts b/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
index 34d5313dd..6918c1137 100644
--- a/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
+++ b/apps/api/src/plugins/github/webhooks/pull-request-opened.ts
@@ -1,3 +1,4 @@
+import { publishEvent } from "../../../events";
import type { GitHubConfig } from "../config";
import { createExternalLink, findExternalLink } from "../services/link-manager";
import {
@@ -100,7 +101,22 @@ export async function handlePullRequestOpened(payload: PROpenedPayload) {
const isTaskFinal = await isTaskInFinalState(task);
if (task.status !== targetStatus && !isTaskFinal) {
- await updateTaskStatus(task.id, targetStatus);
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
}
return;
diff --git a/apps/api/src/plugins/github/webhooks/push.ts b/apps/api/src/plugins/github/webhooks/push.ts
index bb0053205..a6c0e6e11 100644
--- a/apps/api/src/plugins/github/webhooks/push.ts
+++ b/apps/api/src/plugins/github/webhooks/push.ts
@@ -1,3 +1,4 @@
+import { publishEvent } from "../../../events";
import type { GitHubConfig } from "../config";
import { createOrUpdateExternalLink } from "../services/link-manager";
import {
@@ -134,7 +135,22 @@ export async function handlePush(payload: PushPayload) {
console.log(
`[Push] Updating task ${task.id} status from ${task.status} to ${targetStatus}`,
);
- await updateTaskStatus(task.id, targetStatus);
+ const statusResult = await updateTaskStatus(task.id, targetStatus);
+ if (
+ statusResult.applied &&
+ statusResult.before.status !== statusResult.after.status
+ ) {
+ await publishEvent("task.status_changed", {
+ taskId: statusResult.after.id,
+ projectId: statusResult.after.projectId,
+ userId: null,
+ oldStatus: statusResult.before.status,
+ newStatus: statusResult.after.status,
+ title: statusResult.after.title,
+ assigneeId: statusResult.after.userId,
+ type: "status_changed",
+ });
+ }
} else {
console.log(`[Push] Skipping status update - already ${task.status}`);
}