feat: add webhook versioning (#23861)

* add webhook version schema

* version the code

* update version from numeric to date val

* migration

* update schema and build factory

* update string

* move version picker

* tooltip instead of infobadge

* --

* fix type

* --

* fix type

* fix type

* --

* fix messed up merge

* improvements to payloadfactory

* extract version off of DB and instead keep it in IWebhookRepository

* fix webhookform

* fix type safety and routing ambiguity

* scalable with easier factory extensions and base definition

* fix types

* --

* --

* clean up prisma/client type imports

* fix

* type fix

* type fix

* cleanup

* add tests and registry changes

* unintended file inclusion

* type-fix

* select in repo

* --

* explicit return type

* --

* fix type

* fixes

* feedback 1

* feedback 2

* use enum instead of string

* fixes
This commit is contained in:
Syed Ali Shahbaz authored and GitHub committed 2025-12-18 09:36:22 +02:00
1 parent 42bd4b4057
commit 056922b6ee
71 files changed
+3319 -1002

No files matched your search

@@ -1,3 +1,5 @@
import type { Webhook } from "@calcom/features/webhooks/lib/dto/types";
import { router } from "../../../trpc";
import { ZCreateInputSchema } from "./create.schema";
import { ZDeleteInputSchema } from "./delete.schema";
@@ -22,7 +24,7 @@ const UNSTABLE_HANDLER_CACHE: WebhookRouterHandlerCache = {};
export const webhookRouter = router({
list: createWebhookPbacProcedure("webhook.read")
.input(ZListInputSchema)
.query(async ({ ctx, input }) => {
.query(async ({ ctx, input }): Promise<Webhook[]> => {
if (!UNSTABLE_HANDLER_CACHE.list) {
UNSTABLE_HANDLER_CACHE.list = await import("./list.handler").then((mod) => mod.listHandler);
}
@@ -131,7 +133,7 @@ export const webhookRouter = router({
}),
getByViewer: createWebhookPbacProcedure("webhook.read", ["ADMIN", "OWNER", "MEMBER"]).query(
async ({ ctx }) => {
async ({ ctx }): Promise<import("./getByViewer.handler").WebhooksByViewer> => {
if (!UNSTABLE_HANDLER_CACHE.getByViewer) {
UNSTABLE_HANDLER_CACHE.getByViewer = await import("./getByViewer.handler").then(
(mod) => mod.getByViewerHandler
@@ -2,6 +2,7 @@ import { z } from "zod";
import { TIME_UNIT } from "@calcom/features/ee/workflows/lib/constants";
import { WEBHOOK_TRIGGER_EVENTS } from "@calcom/features/webhooks/lib/constants";
import { WebhookVersion } from "@calcom/features/webhooks/lib/interface/IWebhookRepository";
import { webhookIdAndEventTypeIdSchema } from "./types";
@@ -17,6 +18,7 @@ export const ZCreateInputSchema = webhookIdAndEventTypeIdSchema.extend({
platform: z.boolean().optional(),
time: z.number().nullable().optional(),
timeUnit: z.enum(TIME_UNIT).nullable().optional(),
version: z.nativeEnum(WebhookVersion).optional(),
});
export type TCreateInputSchema = z.infer<typeof ZCreateInputSchema>;
@@ -2,6 +2,7 @@ import { z } from "zod";
import { TIME_UNIT } from "@calcom/features/ee/workflows/lib/constants";
import { WEBHOOK_TRIGGER_EVENTS } from "@calcom/features/webhooks/lib/constants";
import { WebhookVersion } from "@calcom/features/webhooks/lib/interface/IWebhookRepository";
import { webhookIdAndEventTypeIdSchema } from "./types";
@@ -16,6 +17,7 @@ export const ZEditInputSchema = webhookIdAndEventTypeIdSchema.extend({
secret: z.string().optional().nullable(),
time: z.number().nullable().optional(),
timeUnit: z.enum(TIME_UNIT).nullable().optional(),
version: z.nativeEnum(WebhookVersion).optional(),
});
export type TEditInputSchema = z.infer<typeof ZEditInputSchema>;
@@ -1,5 +1,5 @@
import type { WebhookGroup } from "@calcom/features/webhooks/lib/dto/types";
import { WebhookRepository } from "@calcom/features/webhooks/lib/repository/WebhookRepository";
import type { Webhook } from "@calcom/prisma/client";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
type GetByViewerOptions = {
@@ -8,19 +8,6 @@ type GetByViewerOptions = {
};
};
type WebhookGroup = {
teamId?: number | null;
profile: {
slug: string | null;
name: string | null;
image?: string;
};
metadata?: {
readOnly: boolean;
};
webhooks: Webhook[];
};
export type WebhooksByViewer = {
webhookGroups: WebhookGroup[];
profiles: {
@@ -32,7 +19,7 @@ export type WebhooksByViewer = {
}[];
};
export const getByViewerHandler = async ({ ctx }: GetByViewerOptions) => {
export const getByViewerHandler = async ({ ctx }: GetByViewerOptions): Promise<WebhooksByViewer> => {
// Use the singleton instance to avoid creating new instances repeatedly
const webhookRepository = WebhookRepository.getInstance();
return await webhookRepository.getFilteredWebhooksForUser({
@@ -1,7 +1,5 @@
import { PermissionCheckService } from "@calcom/features/pbac/services/permission-check.service";
import { prisma } from "@calcom/prisma";
import type { Prisma } from "@calcom/prisma/client";
import { MembershipRole } from "@calcom/prisma/enums";
import type { Webhook } from "@calcom/features/webhooks/lib/dto/types";
import { WebhookRepository } from "@calcom/features/webhooks/lib/repository/WebhookRepository";
import type { TrpcSessionUser } from "@calcom/trpc/server/types";
import type { TListInputSchema } from "./list.schema";
@@ -13,72 +11,13 @@ type ListOptions = {
input: TListInputSchema;
};
export const listHandler = async ({ ctx, input }: ListOptions) => {
const where: Prisma.WebhookWhereInput = {
/* Don't mixup zapier webhooks with normal ones */
AND: [{ appId: !input?.appId ? null : input.appId }],
};
export const listHandler = async ({ ctx, input }: ListOptions): Promise<Webhook[]> => {
const repository = WebhookRepository.getInstance();
const user = await prisma.user.findUnique({
where: {
id: ctx.user.id,
},
select: {
teams: true,
},
});
if (Array.isArray(where.AND)) {
if (input?.eventTypeId) {
const managedParentEvt = await prisma.eventType.findFirst({
where: {
id: input.eventTypeId,
parentId: {
not: null,
},
},
select: {
parentId: true,
},
});
if (managedParentEvt?.parentId) {
where.AND?.push({
OR: [{ eventTypeId: input.eventTypeId }, { eventTypeId: managedParentEvt.parentId, active: true }],
});
} else {
where.AND?.push({ eventTypeId: input.eventTypeId });
}
} else {
const permissionService = new PermissionCheckService();
const teamIds = user?.teams?.map((m) => m.teamId) ?? [];
const allowedTeamIds = (
await Promise.all(
teamIds.map(async (teamId) => {
const ok = await permissionService.checkPermission({
userId: ctx.user.id,
teamId,
permission: "webhook.read",
fallbackRoles: [MembershipRole.ADMIN, MembershipRole.OWNER],
});
return ok ? teamId : null;
})
)
).filter((x): x is number => x !== null);
console.log("Allowed Team IDs:", allowedTeamIds);
where.AND?.push({
OR: [{ userId: ctx.user.id }, ...(allowedTeamIds.length ? [{ teamId: { in: allowedTeamIds } }] : [])],
});
}
if (input?.eventTriggers) {
where.AND?.push({ eventTriggers: { hasEvery: input.eventTriggers } });
}
}
return await prisma.webhook.findMany({
where,
return repository.listWebhooks({
userId: ctx.user.id,
appId: input?.appId,
eventTypeId: input?.eventTypeId,
eventTriggers: input?.eventTriggers,
});
};