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

@@ -0,0 +1,87 @@
import type { WebhookTriggerEvents, UserPermissionRole } from "@calcom/prisma/enums";
import type { Webhook, WebhookSubscriber, WebhookGroup } from "../dto/types";
/**
* Webhook Version enum - defines the payload format versions.
*
* This is a TypeScript-only enum (not Prisma).
* DB operations go through the repository which enforces these values.
*/
export const WebhookVersion = {
V_2021_10_20: "2021-10-20",
} as const;
export type WebhookVersion = (typeof WebhookVersion)[keyof typeof WebhookVersion];
/**
* Default webhook version - used for new webhooks and as fallback
*/
export const DEFAULT_WEBHOOK_VERSION = WebhookVersion.V_2021_10_20;
const VALID_WEBHOOK_VERSIONS = new Set<string>(Object.values(WebhookVersion));
export function isValidWebhookVersion(value: string): value is WebhookVersion {
return VALID_WEBHOOK_VERSIONS.has(value);
}
/**
* Parse and validate a webhook version string.
* Throws if the version is invalid.
*/
export function parseWebhookVersion(value: string): WebhookVersion {
if (!isValidWebhookVersion(value)) {
throw new Error(
`Invalid webhook version: "${value}". Valid versions are: ${Object.values(WebhookVersion).join(", ")}`
);
}
return value;
}
export interface GetSubscribersOptions {
userId?: number | null;
eventTypeId?: number | null;
triggerEvent: WebhookTriggerEvents;
teamId?: number | number[] | null;
orgId?: number | null;
oAuthClientId?: string | null;
}
export interface ListWebhooksOptions {
userId: number;
appId?: string | null;
eventTypeId?: number | null;
eventTriggers?: WebhookTriggerEvents[];
}
export interface IWebhookRepository {
getSubscribers(options: GetSubscribersOptions): Promise<WebhookSubscriber[]>;
getWebhookById(id: string): Promise<WebhookSubscriber | null>;
findByWebhookId(webhookId?: string): Promise<{
id: string;
subscriberUrl: string;
payloadTemplate: string | null;
active: boolean;
eventTriggers: WebhookTriggerEvents[];
secret: string | null;
teamId: number | null;
userId: number | null;
platform: boolean;
time: number | null;
timeUnit: string | null;
version: WebhookVersion;
}>;
getFilteredWebhooksForUser(options: { userId: number; userRole?: UserPermissionRole }): Promise<{
webhookGroups: WebhookGroup[];
profiles: {
teamId: number | null | undefined;
slug: string | null;
name: string | null;
image?: string | undefined;
canModify?: boolean;
canDelete?: boolean;
}[];
}>;
listWebhooks(options: ListWebhooksOptions): Promise<Webhook[]>;
}
@@ -1,56 +0,0 @@
import type { Webhook } from "@calcom/prisma/client";
import type { WebhookTriggerEvents, UserPermissionRole } from "@calcom/prisma/enums";
import type { WebhookSubscriber } from "../dto/types";
export interface GetSubscribersOptions {
userId?: number | null;
eventTypeId?: number | null;
triggerEvent: WebhookTriggerEvents;
teamId?: number | number[] | null;
orgId?: number | null;
oAuthClientId?: string | null;
}
type WebhookGroup = {
teamId?: number | null;
profile: {
slug: string | null;
name: string | null;
image?: string;
};
metadata?: {
canModify: boolean;
canDelete: boolean;
};
webhooks: Webhook[];
};
export interface IWebhookRepository {
getSubscribers(options: GetSubscribersOptions): Promise<WebhookSubscriber[]>;
getWebhookById(id: string): Promise<WebhookSubscriber | null>;
findByWebhookId(webhookId?: string): Promise<{
id: string;
subscriberUrl: string;
payloadTemplate: string | null;
active: boolean;
eventTriggers: WebhookTriggerEvents[];
secret: string | null;
teamId: number | null;
userId: number | null;
platform: boolean;
time: number | null;
timeUnit: string | null;
}>;
getFilteredWebhooksForUser(options: { userId: number; userRole?: UserPermissionRole }): Promise<{
webhookGroups: WebhookGroup[];
profiles: {
teamId: number | null | undefined;
slug: string | null;
name: string | null;
image?: string | undefined;
canModify?: boolean;
canDelete?: boolean;
}[];
}>;
}