import { env } from "cloudflare:workers";
import { randomId } from "../crypto";
import { run } from "../db";
import type { CodeIndexMessage } from "../code/search-index";
export type { WebhookEvent } from "~/lib/webhook-events";
import type { WebhookEvent } from "~/lib/webhook-events";
export type NotifyMessage = {
kind: "notify";
repoId: string;
issueId: string | null;
actorId: string | null;
reason: "comment" | "mention" | "review_requested" | "assigned" | "state_change" | "ci" | "review" | "release";
title: string;
url: string;
mentionText: string | null;
directRecipients: string[];
};
export type QueueMessage =
| { kind: "webhook.dispatch"; repoId: string; event: WebhookEvent; payload: Record<string, unknown> }
| {
kind: "webhook.deliver";
webhookId: string;
event: WebhookEvent;
deliveryId: string;
payload: Record<string, unknown>;
}
| NotifyMessage
| CodeIndexMessage
| { kind: "email.send"; to: string; subject: string; text: string; html: string };
export async function emitWebhook(
repoId: string,
event: WebhookEvent,
payload: Record<string, unknown>,
): Promise<void> {
const message: QueueMessage = { kind: "webhook.dispatch", repoId, event, payload };
await env.EVENTS.send(message);