Skip to content

⭐️⭐️⭐️ Open-source, self-hosted alternative to Linear and Plane. Project management and issue tracking where teams and AI agents work side by side to plan and ship products.

4.5 KB
Raw Blame
import { Elysia, t } from 'elysia';
import { noContent } from '#shared/http';
import { guards, entityGuard } from '#shared/guards';
import { authContext } from '#shared/auth-context';
import { checkPermission } from '#shared/access';
import { HttpError } from '#shared/lib';
import { assertPublicHttpUrl } from '#shared/net';
import { mcpTool } from '#mcp/generate';
import { accessErrors, commonErrors } from '#shared/responses';
import {
  WebhookDeliveryPageResponse,
  WebhookResponse,
  createWebhookBody,
  listDeliveriesQuery,
  updateWebhookBody,
  webhookParams,
} from './model';
import {
  listWebhooks,
  createWebhook,
  getWebhook,
  updateWebhook,
  rotateWebhookSecret,
  deleteWebhook,
  listWebhookDeliveries,
} from './service';

export const webhookRoutes = new Elysia({ name: 'webhooks', detail: { tags: ['Webhooks'] } })
  .use(authContext)
  .use(guards)
  .macro({
    webhook: entityGuard(
      'webhooks',
      'Webhook not found',
      async (p) => (await getWebhook(Number(p.webhookId)))?.projectId ?? null,
    ),
  })
  .get(
    '/projects/:projectKey/webhooks',
    async ({ project, user }) => {
      const rows = await listWebhooks(project.id);
      // The secret lets its holder sign deliveries the receiver trusts, so it goes only to
      // a caller who may create or edit webhooks.
      const canSeeSecret =
        (await checkPermission(project.id, user, 'webhooks', 'edit')) ||
        (await checkPermission(project.id, user, 'webhooks', 'create'));
      return canSeeSecret ? rows : rows.map((row) => ({ ...row, secret: null }));
    },
    {
      permission: ['webhooks', 'read'],
      response: { 200: t.Array(WebhookResponse), ...accessErrors },
      detail: {
        summary: "List a project's webhooks",
        description:
          'The signing secret is returned only to a caller who may create or edit webhooks.',
        ...mcpTool('list_webhooks'),
      },
    },
  )

  .post(
    '/projects/:projectKey/webhooks',
    async ({ project, body, set }) => {
      await assertPublicHttpUrl(body.url);
      set.status = 201;
      return createWebhook({
        projectId: project.id,
        url: body.url,
        events: body.events,
        isActive: body.isActive,
      });
    },
    {
      body: createWebhookBody,
      permission: ['webhooks', 'create'],
      response: { 201: WebhookResponse, ...commonErrors },
      detail: { summary: 'Create a webhook', ...mcpTool('create_webhook') },
    },
  )

  .patch(
    '/webhooks/:webhookId',
    async ({ params, body }) => {
      if (body.url !== undefined) await assertPublicHttpUrl(body.url);
      const updated = await updateWebhook(params.webhookId, body);
      if (!updated) throw new HttpError(404, 'Webhook not found');
      return updated;
    },
    {
      body: updateWebhookBody,
      params: webhookParams,
      webhook: 'edit',
      response: { 200: WebhookResponse, ...commonErrors },
      detail: { summary: 'Update a webhook', ...mcpTool('update_webhook') },
    },
  )

  .post(
    '/webhooks/:webhookId/secret',
    async ({ params }) => {
      const rotated = await rotateWebhookSecret(params.webhookId);
      if (!rotated) throw new HttpError(404, 'Webhook not found');
      return rotated;
    },
    {
      params: webhookParams,
      webhook: 'edit',
      response: { 200: WebhookResponse, ...commonErrors },
      detail: {
        summary: 'Rotate the webhook secret',
        description:
          'Issue a new signing secret. The next delivery is signed with it, so the receiver ' +
          'has to be given it first.',
        // The receiver rejects deliveries until it is given the new secret.
        ...mcpTool('rotate_webhook_secret', { destructiveHint: true }),
      },
    },
  )

  .delete(
    '/webhooks/:webhookId',
    async ({ params }) => {
      await deleteWebhook(params.webhookId);
      return noContent();
    },
    {
      params: webhookParams,
      webhook: 'delete',
      response: { 204: t.Void(), ...commonErrors },
      detail: { summary: 'Delete a webhook', ...mcpTool('delete_webhook') },
    },
  )

  .get(
    '/webhooks/:webhookId/deliveries',
    async ({ params, query }) => {
      return listWebhookDeliveries(params.webhookId, { before: query.before, limit: query.limit });
    },
    {
      params: webhookParams,
      query: listDeliveriesQuery,
      webhook: 'read',
      response: { 200: WebhookDeliveryPageResponse, ...commonErrors },
      detail: {
        summary: 'List webhook deliveries',
        description: "List a webhook's delivery attempts.",
        ...mcpTool('list_webhook_deliveries'),
      },
    },
  );