65 lines · 1.7 KB
import { env } from "cloudflare:workers";
import { randomId } from "../crypto";
import { run } from "../db";

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";
  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;

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);
}

export async function emitNotification(message: Omit<NotifyMessage, "kind">): Promise<void> {