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(webhooks): persist generic webhook delivery health

Tin211280ea1e16
diff --git a/apps/api/src/plugins/generic-webhook/config.ts b/apps/api/src/plugins/generic-webhook/config.ts
index 0d482af1d..dfdab886a 100644
--- a/apps/api/src/plugins/generic-webhook/config.ts
+++ b/apps/api/src/plugins/generic-webhook/config.ts
@@ -14,6 +14,22 @@ export type GenericWebhookEventKey = (typeof genericWebhookEventKeys)[number];
export const genericWebhookConfigSchema = v.object({
webhookUrl: v.pipe(v.string(), v.url()),
secret: v.optional(v.string()),
+ health: v.optional(
+ v.object({
+ lastSuccessAt: v.optional(v.string()),
+ lastFailureAt: v.optional(v.string()),
+ lastFailureMessage: v.optional(v.string()),
+ failureCount: v.optional(v.number()),
+ lastAttempt: v.optional(
+ v.object({
+ eventName: v.string(),
+ taskId: v.string(),
+ projectId: v.string(),
+ webhookUrl: v.string(),
+ }),
+ ),
+ }),
+ ),
events: v.optional(
v.object({
taskCreated: v.optional(v.boolean()),
@@ -48,6 +64,12 @@ export function normalizeGenericWebhookConfig(
return {
...config,
secret: config.secret?.trim() || undefined,
+ health: config.health
+ ? {
+ ...config.health,
+ failureCount: config.health.failureCount ?? 0,
+ }
+ : undefined,
events: {
...defaultGenericWebhookEvents,
...(config.events ?? {}),
diff --git a/apps/api/src/plugins/generic-webhook/events.ts b/apps/api/src/plugins/generic-webhook/events.ts
index 1414262ed..646e47c28 100644
--- a/apps/api/src/plugins/generic-webhook/events.ts
+++ b/apps/api/src/plugins/generic-webhook/events.ts
@@ -1,6 +1,7 @@
import { and, eq } from "drizzle-orm";
import db from "../../database";
import {
+ integrationTable,
projectTable,
taskTable,
userTable,
@@ -94,6 +95,41 @@ async function getActor(userId: string | null): Promise<{
};
}
+async function persistWebhookHealth(
+ projectId: string,
+ update: (config: GenericWebhookConfig) => GenericWebhookConfig,
+): Promise<void> {
+ try {
+ const integration = await db.query.integrationTable.findFirst({
+ where: and(
+ eq(integrationTable.projectId, projectId),
+ eq(integrationTable.type, "generic-webhook"),
+ ),
+ });
+
+ if (!integration) {
+ return;
+ }
+
+ const currentConfig = normalizeGenericWebhookConfig(
+ JSON.parse(integration.config) as GenericWebhookConfig,
+ );
+
+ await db
+ .update(integrationTable)
+ .set({
+ config: JSON.stringify(update(currentConfig)),
+ updatedAt: new Date(),
+ })
+ .where(eq(integrationTable.id, integration.id));
+ } catch (error) {
+ console.error("persistWebhookHealth failed", {
+ error,
+ projectId,
+ });
+ }
+}
+
async function sendEvent(
config: GenericWebhookConfig,
eventName: string,
@@ -106,6 +142,12 @@ async function sendEvent(
if (!task) return;
const actor = await getActor(userId);
+ const attempt = {
+ eventName,
+ taskId,
+ projectId,
+ webhookUrl: config.webhookUrl,
+ };
try {
await postToGenericWebhook(
@@ -134,7 +176,31 @@ async function sendEvent(
},
config.secret,
);
+
+ void persistWebhookHealth(projectId, (currentConfig) => ({
+ ...currentConfig,
+ health: {
+ ...currentConfig.health,
+ lastSuccessAt: new Date().toISOString(),
+ lastFailureMessage: undefined,
+ lastAttempt: attempt,
+ },
+ }));
} catch (error) {
+ const message =
+ error instanceof Error ? (error.stack ?? error.message) : String(error);
+
+ void persistWebhookHealth(projectId, (currentConfig) => ({
+ ...currentConfig,
+ health: {
+ ...currentConfig.health,
+ lastFailureAt: new Date().toISOString(),
+ lastFailureMessage: message,
+ failureCount: (currentConfig.health?.failureCount ?? 0) + 1,
+ lastAttempt: attempt,
+ },
+ }));
+
console.error("sendEvent postToGenericWebhook failed", {
error,
eventName,