173 lines · 5.5 KB
import { env } from "cloudflare:workers";
import { z } from "zod";
import { all, first, run } from "../db";
import { randomId } from "../crypto";
import { notFoundError } from "../errors";
import { isForgeWebhookUrl } from "../events/consumer";
import {
  assertWebhookTarget,
  deliverWebhook,
  MAX_WEBHOOK_SECRET,
  MIN_WEBHOOK_SECRET,
  replayableBody,
  sealWebhookSecret,
} from "../webhooks/legacy";
import type { WebhookEvent } from "../events/emit";
import { assertBelowCap, CAPS, limitWrite } from "../limits";
import { noContent, readJson, requireScopedRepo } from "./http";
import type { ApiContext } from "./http";
import { WEBHOOK_EVENTS } from "~/lib/webhook-events";

const DELIVERY_LIMIT = 100;

type WebhookRow = {
  id: string;
  url: string;
  secret: string | null;
  events: string;
  active: number;
  created_at: number;
};

function hookJson(hook: WebhookRow) {
  return {
    id: hook.id,
    url: hook.url,
    has_secret: Boolean(hook.secret),
    events: JSON.parse(hook.events) as string[],
    active: hook.active === 1,
    created_at: hook.created_at,
  };