148 lines · 5.6 KB
import { env } from "cloudflare:workers";
import { all, chunks, first, placeholders } from "../db";
import { repoDb } from "../scoped-db";
import { NOT_TAKEN_DOWN_SQL } from "../repos";

const PACKAGES_PER_QUERY = 80;

type IndexedAdvisory = {
  id: string;
  repo_id: string;
  ghsa_id: string;
  cve_id: string | null;
  title: string;
  severity: string;
  state: string;
  ecosystem: string | null;
  package_name: string | null;
  affected_versions: string | null;
  patched_versions: string | null;
  published_at: number | null;
  updated_at: number;
};

export function packageKey(ecosystem: string, name: string): string {
  return `${ecosystem}\0${name}`;
}

function rowStatements(advisory: IndexedAdvisory): D1PreparedStatement[] {
  const remove = env.ADVISORY_DB.prepare("DELETE FROM published_advisory_packages WHERE advisory_id = ?").bind(
    advisory.id,
  );
  if (advisory.state !== "published" || !advisory.ecosystem || !advisory.package_name) return [remove];
  return [
    remove,
    env.ADVISORY_DB.prepare(
      `INSERT INTO published_advisory_packages (advisory_id, repo_id, ghsa_id, cve_id, title, severity, ecosystem,
         package_name, affected_versions, patched_versions, published_at, updated_at)
       VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`,
    ).bind(
      advisory.id,