File
Blob: src/contracts/webhook.ts
| 1 | import { eg, type TypeFromCodec } from "@cloudflare/util-en-garde"; |
| 2 | |
| 3 | import { BranchName, CommitSha, IsoDateTime, ProjectId, RunId, WebhookId, WebhookProvider } from "@/contracts/common"; |
| 4 | |
| 5 | export const WebhookProviderConfig = eg.exactStrict( |
| 6 | eg.object({ |
| 7 | instanceUrl: eg.string, |
| 8 | }), |
| 9 | ); |
| 10 | export type WebhookProviderConfig = TypeFromCodec<typeof WebhookProviderConfig>; |
| 11 | |
| 12 | export const WebhookEventKind = eg.union([eg.literal("push"), eg.literal("ping"), eg.literal("other")]); |
| 13 | export type WebhookEventKind = TypeFromCodec<typeof WebhookEventKind>; |
| 14 | |
| 15 | export const WebhookDeliveryOutcome = eg.union([ |
| 16 | eg.literal("accepted"), |
| 17 | eg.literal("ignored_ping"), |
| 18 | eg.literal("ignored_event"), |
| 19 | eg.literal("ignored_branch"), |
| 20 | eg.literal("queue_full"), |
| 21 | ]); |
| 22 | export type WebhookDeliveryOutcome = TypeFromCodec<typeof WebhookDeliveryOutcome>; |
| 23 | |
| 24 | export const WebhookRecentDelivery = eg.exactStrict( |
| 25 | eg.object({ |
| 26 | provider: WebhookProvider, |
| 27 | deliveryId: eg.string, |
| 28 | eventKind: WebhookEventKind, |
| 29 | eventName: eg.string, |
| 30 | outcome: WebhookDeliveryOutcome, |
| 31 | repoUrl: eg.string, |
| 32 | ref: eg.union([eg.string, eg.null]), |
| 33 | branch: eg.union([BranchName, eg.null]), |
| 34 | commitSha: eg.union([CommitSha, eg.null]), |
| 35 | beforeSha: eg.union([CommitSha, eg.null]), |
| 36 | runId: eg.union([RunId, eg.null]), |
| 37 | receivedAt: IsoDateTime, |
| 38 | }), |
| 39 | ); |
| 40 | export type WebhookRecentDelivery = TypeFromCodec<typeof WebhookRecentDelivery>; |
| 41 | |
| 42 | export const WebhookSummary = eg.exactStrict( |
| 43 | eg.object({ |
| 44 | id: WebhookId, |
| 45 | projectId: ProjectId, |
| 46 | provider: WebhookProvider, |
| 47 | enabled: eg.boolean, |
| 48 | config: eg.union([WebhookProviderConfig, eg.null]), |
| 49 | createdAt: IsoDateTime, |
| 50 | updatedAt: IsoDateTime, |
| 51 | recentDeliveries: eg.array(WebhookRecentDelivery), |
| 52 | }), |
| 53 | ); |
| 54 | export type WebhookSummary = TypeFromCodec<typeof WebhookSummary>; |
| 55 | |
| 56 | export const GetProjectWebhooksResponse = eg.exactStrict( |
| 57 | eg.object({ |
| 58 | webhooks: eg.array(WebhookSummary), |
| 59 | }), |
| 60 | ); |
| 61 | export type GetProjectWebhooksResponse = TypeFromCodec<typeof GetProjectWebhooksResponse>; |
| 62 | |
| 63 | export const CreateWebhookRequest = eg.exactStrict( |
| 64 | eg.object({ |
| 65 | enabled: eg.boolean, |
| 66 | config: eg.union([WebhookProviderConfig, eg.null]).optional, |
| 67 | secret: eg.string.optional, |
| 68 | }), |
| 69 | ); |
| 70 | export type CreateWebhookRequest = TypeFromCodec<typeof CreateWebhookRequest>; |
| 71 | |
| 72 | export const UpdateWebhookRequest = eg.exactStrict( |
| 73 | eg.object({ |
| 74 | enabled: eg.boolean, |
| 75 | config: eg.union([WebhookProviderConfig, eg.null]).optional, |
| 76 | }), |
| 77 | ); |
| 78 | export type UpdateWebhookRequest = TypeFromCodec<typeof UpdateWebhookRequest>; |
| 79 | |
| 80 | export const UpsertWebhookResponse = eg.exactStrict( |
| 81 | eg.object({ |
| 82 | webhook: WebhookSummary, |
| 83 | generatedSecret: eg.union([eg.string, eg.null]), |
| 84 | }), |
| 85 | ); |
| 86 | export type UpsertWebhookResponse = TypeFromCodec<typeof UpsertWebhookResponse>; |
| 87 | |
| 88 | export const RotateWebhookSecretResponse = eg.exactStrict( |
| 89 | eg.object({ |
| 90 | secret: eg.string, |
| 91 | }), |
| 92 | ); |
| 93 | export type RotateWebhookSecretResponse = TypeFromCodec<typeof RotateWebhookSecretResponse>; |