| 1 | "use server"; |
| 2 | |
| 3 | import { cookies } from |
| 4 | import { createSafeActionClient } |
| 5 | import { z } |
| 6 | |
| 7 | import { fetchClient } |
| 8 | import { webhookEvents, webhookOwnerSchema |
| 9 | |
| 10 | const actionClient = createSafeActionClient |
| 11 | handleServerError: (error) |
| 12 | }); |
| 13 | |
| 14 | const owned = z |
| 15 | const webhook = owned |
| 16 | const events = z |
| 17 | .array(z.enum |
| 18 | .min(1, " |
| 19 | const url = z |
| 20 | |
| 21 | const headers = async |
| 22 | |
| 23 | export const createWebhook = |
| 24 | .inputSchema(owned.extend |
| 25 | .action(async ( |
| 26 | const { data, |
| 27 | owner.kind === |
| 28 | ? await fetchClient. |
| 29 | "/api/repositories/{username}/{repo}/webhooks", |
| 30 | { |
| 31 | params: { path: owner |
| 32 | body, |
| 33 | headers: await headers |
| 34 | }, |
| 35 | ) |
| 36 | : await fetchClient. |
| 37 | params: { path: owner |
| 38 | body, |
| 39 | headers: await headers |
| 40 | }); |
| 41 | if (error) throw |
| 42 | return data; |
| 43 | }); |
| 44 | |
| 45 | export const updateWebhook = |
| 46 | .inputSchema( |
| 47 | webhook.extend({ |
| 48 | url: url.optional |
| 49 | events: events.optional |
| 50 | active: z.boolean |
| 51 | }), |
| 52 | ) |
| 53 | .action(async ( |
| 54 | const { error |
| 55 | owner.kind === |
| 56 | ? await fetchClient. |
| 57 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}", |
| 58 | { |
| 59 | params: { path: { |
| 60 | body, |
| 61 | headers: await headers |
| 62 | }, |
| 63 | ) |
| 64 | : await fetchClient. |
| 65 | "/api/organizations/{slug}/webhooks/{webhookId}", |
| 66 | { |
| 67 | params: { path: { |
| 68 | body, |
| 69 | headers: await headers |
| 70 | }, |
| 71 | ); |
| 72 | if (error) throw |
| 73 | }); |
| 74 | |
| 75 | export const deleteWebhook = |
| 76 | .inputSchema(webhook) |
| 77 | .action(async ( |
| 78 | const { error |
| 79 | owner.kind === |
| 80 | ? await fetchClient. |
| 81 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}", |
| 82 | { |
| 83 | params: { path: { |
| 84 | headers: await headers |
| 85 | }, |
| 86 | ) |
| 87 | : await fetchClient. |
| 88 | "/api/organizations/{slug}/webhooks/{webhookId}", |
| 89 | { |
| 90 | params: { path: { |
| 91 | headers: await headers |
| 92 | }, |
| 93 | ); |
| 94 | if (error) throw |
| 95 | }); |
| 96 | |
| 97 | export const pingWebhook = |
| 98 | .inputSchema(webhook) |
| 99 | .action(async ( |
| 100 | const { error |
| 101 | owner.kind === |
| 102 | ? await fetchClient. |
| 103 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}/pings", |
| 104 | { |
| 105 | params: { path: { |
| 106 | headers: await headers |
| 107 | }, |
| 108 | ) |
| 109 | : await fetchClient. |
| 110 | "/api/organizations/{slug}/webhooks/{webhookId}/pings", |
| 111 | { |
| 112 | params: { path: { |
| 113 | headers: await headers |
| 114 | }, |
| 115 | ); |
| 116 | if (error) throw |
| 117 | }); |
| 118 | |
| 119 | export const redeliverWebhook = |
| 120 | .inputSchema(webhook.extend |
| 121 | .action(async ( |
| 122 | const { error |
| 123 | owner.kind === |
| 124 | ? await fetchClient. |
| 125 | "/api/repositories/{username}/{repo}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliveries", |
| 126 | { |
| 127 | params: { path: { |
| 128 | headers: await headers |
| 129 | }, |
| 130 | ) |
| 131 | : await fetchClient. |
| 132 | "/api/organizations/{slug}/webhooks/{webhookId}/deliveries/{deliveryId}/redeliveries", |
| 133 | { |
| 134 | params: { path: { |
| 135 | headers: await headers |
| 136 | }, |
| 137 | ); |
| 138 | if (error) throw |
| 139 | }); |