import { env } from "cloudflare:workers";
import { webhookEventOf } from "~/lib/webhook-events";
import { findWebhookDelivery, listWebhookDeliveries } from "../webhook-log";
import { z } from "zod";
import { repoDb } from "../scoped-db";
import { forgetLocator, registerLocator } from "../scoped-db/locators";
import { scopedId } from "../scoped-db/ids";
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, requireScopedAdminWrite, 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),