( integration: Linear )
| 167 | }); |
| 168 | |
| 169 | export function createWebhookEventSource( |
| 170 | integration: Linear |
| 171 | ): ExternalSource<Linear, TriggerParams, "HTTP", {}> { |
| 172 | return new ExternalSource("HTTP", { |
| 173 | id: "linear.webhook", |
| 174 | schema: z.object({ |
| 175 | teamId: z.string().optional(), |
| 176 | }), |
| 177 | version: "0.1.0", |
| 178 | integration, |
| 179 | key: (params) => `${params.teamId ? params.teamId : "all"}`, |
| 180 | handler: webhookHandler, |
| 181 | register: async (event, io, ctx) => { |
| 182 | const { params, source: httpSource, options } = event; |
| 183 | |
| 184 | // (key-specific) stored data, undefined if not registered yet |
| 185 | const webhookData = WebhookRegistrationDataSchema.safeParse(httpSource.data); |
| 186 | |
| 187 | // set of events to register |
| 188 | const allEvents = Array.from(new Set([...options.event.desired, ...options.event.missing])); |
| 189 | const registeredOptions = { |
| 190 | event: allEvents, |
| 191 | }; |
| 192 | |
| 193 | // easily identify webhooks on linear |
| 194 | const label = `trigger.${params.teamId ? params.teamId : "all"}`; |
| 195 | |
| 196 | if (httpSource.active && webhookData.success) { |
| 197 | const hasMissingOptions = Object.values(options).some( |
| 198 | (option) => option.missing.length > 0 |
| 199 | ); |
| 200 | if (!hasMissingOptions) return; |
| 201 | |
| 202 | const updatedWebhook = await io.integration.updateWebhook("update-webhook", { |
| 203 | id: webhookData.data.webhook.id, |
| 204 | input: { |
| 205 | label, |
| 206 | resourceTypes: allEvents, |
| 207 | secret: httpSource.secret, |
| 208 | url: httpSource.url, |
| 209 | }, |
| 210 | }); |
| 211 | |
| 212 | return { |
| 213 | data: WebhookRegistrationDataSchema.parse(updatedWebhook), |
| 214 | options: registeredOptions, |
| 215 | }; |
| 216 | } |
| 217 | |
| 218 | // check for existing hooks that match url |
| 219 | const listResponse = await io.integration.webhooks("list-webhooks"); |
| 220 | const existingWebhook = listResponse.find((w) => w.url === httpSource.url); |
| 221 | |
| 222 | if (existingWebhook) { |
| 223 | const updatedWebhook = await io.integration.updateWebhook("update-webhook", { |
| 224 | id: existingWebhook.id, |
| 225 | input: { |
| 226 | label, |
no test coverage detected
searching dependent graphs…