#!/usr/bin/env -S npx --yes tsx
// =============================================================================
// list_prs_ci_failing.ts — Open PRs with failing CI, any review status
//
// Part of: .opencode/skills/auto-agents-system/scripts/
//
// Delegates to list_prs.ts with hard-coded filters.
//
// Returns all OPEN pull requests that:
//   • have failing CI                   (ci_status == 'failing')
//     i.e. at least one commit status check has reported failure, error,
//     or warning for the PR's head SHA.
//
// No approval filter is applied — this bucket intentionally spans all review
// states (zero approvals, needs review, already approved) because a CI failure
// is an actionable signal regardless of where a PR sits in the review pipeline.
//
// PRs with pending CI (still running), passing CI (all checks succeeded), or
// unknown CI (no checks reported yet) are excluded.  Only 'failing' is returned.
//
// Why this bucket exists:
//   CI failures block every downstream step — approved PRs cannot be merged,
//   stale PRs cannot be rebased and merged, and pending-review PRs should not
//   be reviewed while they are red.  This script surfaces the full set of
//   failing PRs so an agent or supervisor can triage and repair them before
//   other pipeline steps can proceed.
//
// Hard-coded filters (not configurable):
//   state:    'open'        only open PRs are considered
//   ciStatus: ['failing']   only PRs with at least one failing/error/warning check
//
// USAGE:
//   list_prs_ci_failing.ts --url URL --pat TOKEN --owner OWNER --repo REPO
//
// OUTPUT (stdout): JSON array — same schema as list_prs.ts
// DEPENDENCIES: Node.js >= 18 (native fetch), npx tsx (for TypeScript execution)
// =============================================================================
import { listPRs, parseConnectionArgs } from './list_prs.ts';

async function main(): Promise<void> {
  const config = parseConnectionArgs(process.argv);
  const prs    = await listPRs(config, {
    state:    'open',
    ciStatus: ['failing'],
  });
  process.stdout.write(JSON.stringify(prs, null, 2) + '\n');
}

main().catch((err: unknown) => {
  process.stderr.write(`ERROR: ${err instanceof Error ? err.message : String(err)}\n`);
  process.exit(1);
});
