| import { actionClient } from "@/lib/action-client"; |
| 4 | import { z } from "zod"; |
| 5 |
| 6 | import { callApi } from "@/lib/api/server"; |
| 7 | import { webhookEvents, webhookOwnerSchema } from "@/lib/webhooks"; |
| 8 |
| 9 | const owned = z.object({ owner: webhookOwnerSchema }); |
| 10 | const webhook = owned.extend({ webhookId: z.string() }); |
| 11 | const events = z |
| 12 | .array(z.enum(webhookEvents)) |
| 13 | .min(1, "Choose at least one event."); |
| 14 | const url = z.string().trim().url("Enter a full URL."); |
| 15 |
| 16 | export const createWebhook = actionClient |
| 17 | .inputSchema(owned.extend({ url, events })) |
| 18 | .action(async ({ parsedInput: { owner, ...body } }) => { |
| 19 | const data = await callApi((client) => |
| 20 | owner.kind === "repository" |
| 21 | ? client.POST("/api/repositories/{username}/{repo}/webhooks", { |
| 22 | params: { path: owner }, |
| 23 | body, |
| 24 | }) |
| 25 | : client.POST("/api/organizations/{slug}/webhooks", { |
| 26 | params: { path: owner }, |
| 27 | body, |
| 28 | }), |
| 29 | ); |
| 30 | return data; |
| 31 | }); |
| 32 |
| 33 | export const updateWebhook = actionClient |
| 34 | .inputSchema( |
| 35 | webhook.extend({ |
| 36 | url: url.optional(), |
| 37 | events: events.optional(), |
| 38 | active: z.boolean().optional(), |
| 39 | }), |
| 40 | ) |
| 41 | .action(async ({ parsedInput: { owner, webhookId, ...body } }) => { |
| 42 | await callApi((client) => |
| 43 | owner.kind === "repository" |
| 44 | ? client.PATCH( |
| 45 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}", |
| 46 | { |
| 47 | params: { path: { ...owner, webhookId } }, |
| 48 | body, |
| 49 | }, |
| 50 | ) |
| 51 | : client.PATCH("/api/organizations/{slug}/webhooks/{webhookId}", { |
| 52 | params: { path: { ...owner, webhookId } }, |
| 53 | body, |
| 54 | }), |
| 55 | ); |
| 56 | }); |
| 57 |
| 58 | export const deleteWebhook = actionClient |
| 59 | .inputSchema(webhook) |
| 60 | .action(async ({ parsedInput: { owner, webhookId } }) => { |
| 61 | await callApi((client) => |
| 62 | owner.kind === "repository" |
| 63 | ? client.DELETE( |
| 64 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}", |
| 65 | { |
| 66 | params: { path: { ...owner, webhookId } }, |
| 67 | }, |
| 68 | ) |
| 69 | : client.DELETE("/api/organizations/{slug}/webhooks/{webhookId}", { |
| 70 | params: { path: { ...owner, webhookId } }, |
| 71 | }), |
| 72 | ); |
| 73 | }); |
| 74 |
| 75 | export const pingWebhook = actionClient |
| 76 | .inputSchema(webhook) |
| 77 | .action(async ({ parsedInput: { owner, webhookId } }) => { |
| 78 | await callApi((client) => |
| 79 | owner.kind === "repository" |
| 80 | ? client.POST( |
| 81 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}/pings", |
| 82 | { |
| 83 | params: { path: { ...owner, webhookId } }, |
| 84 | }, |
| 85 | ) |
| 86 | : client.POST("/api/organizations/{slug}/webhooks/{webhookId}/pings", { |
| 87 | params: { path: { ...owner, webhookId } }, |
| 88 | }), |
| 89 | ); |
| 90 | }); |
| 91 |
| 92 | export const rollWebhookSecret = actionClient |
| 93 | .inputSchema(webhook) |
| 94 | .action(async ({ parsedInput: { owner, webhookId } }) => { |
| 95 | const data = await callApi((client) => |
| 96 | owner.kind === "repository" |
| 97 | ? client.POST( |
| 98 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}/secret", |
| 99 | { |
| 100 | params: { path: { ...owner, webhookId } }, |
| 101 | }, |
| 102 | ) |
| 103 | : client.POST("/api/organizations/{slug}/webhooks/{webhookId}/secret", { |
| 104 | params: { path: { ...owner, webhookId } }, |
| 105 | }), |
| 106 | ); |
| 107 | return data; |
| 108 | }); |
| 109 |
| 110 | export const redeliverWebhook = actionClient |
| 111 | .inputSchema(webhook.extend({ deliveryId: z.string() })) |
| 112 | .action(async ({ parsedInput: { owner, webhookId, deliveryId } }) => { |
| 113 | await callApi((client) => |
| 114 | owner.kind === "repository" |
| 115 | ? client.POST( |
| 116 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliveries", |
| 117 | { |
| 118 | params: { path: { ...owner, webhookId, deliveryId } }, |
| 119 | }, |
| 120 | ) |
| 121 | : client.POST( |
| 122 | "/api/organizations/{slug}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliveries", |
| 123 | { |
| 124 | params: { path: { ...owner, webhookId, deliveryId } }, |
| 125 | }, |
| 126 | ), |
| 127 | ); |
| 128 | }); |