32 lines · 1.2 KB
import { accountDb } from "../scoped-db";
import { CAPS } from "../limits";
import { busyRunnerCounts, deleteRunnerRow, summarizeRunner } from "./runner-registration";
import type { RunnerRow, RunnerSummary } from "./runner-registration";
import { ownerRepoIds } from "./scope";

/** Owner-wide runners of an account (they serve every repository it owns). */
export async function listOwnerRunners(ownerId: string): Promise<RunnerSummary[]> {
  const rows = await accountDb(ownerId).all<RunnerRow>(
    "SELECT * FROM runners WHERE owner_id = ? ORDER BY name LIMIT ?",
    ownerId,
    CAPS.runnersPerScope,
  );
  const counts = await busyRunnerCounts(
    rows.length > 0 ? await ownerRepoIds(ownerId) : [],
    rows.map((row) => row.id),
  );
  return rows.map((row) => summarizeRunner(row, counts.get(row.id) ?? 0));
}

/** Callers must have checked owner admin. */
export async function removeOwnerRunner(ownerId: string, runnerId: string): Promise<boolean> {
  const row = await accountDb(ownerId).first<{ id: string }>(
    "SELECT id FROM runners WHERE id = ? AND owner_id = ?",
    runnerId,
    ownerId,
  );
  if (!row) return false;
  await deleteRunnerRow(runnerId);
  return true;
}