(argv = process.argv.slice(2))
| 61 | } |
| 62 | |
| 63 | export async function main(argv = process.argv.slice(2)): Promise<void> { |
| 64 | const options = parse(argv); |
| 65 | if (!options) return; |
| 66 | const host = required("GH_EVAL_HOST"); |
| 67 | if (host !== "github.com") throw new Error("The event preview smoke supports github.com only"); |
| 68 | const org = required("GH_EVAL_ORG"); |
| 69 | const token = required("GH_EVAL_TOKEN"); |
| 70 | const actorToken = required("GH_EVAL_ACTOR_TOKEN"); |
| 71 | if (token === actorToken) throw new Error("GH_EVAL_TOKEN and GH_EVAL_ACTOR_TOKEN must represent different identities"); |
| 72 | const gh = await realpath(options.gh); |
| 73 | await mkdir(options.output, { recursive: true, mode: 0o700 }); |
| 74 | const journalPath = join(options.output, "resources.json"); |
| 75 | const home = await mkdtemp(join(tmpdir(), "gh-smoke-home-")); |
| 76 | const workDir = await mkdtemp(join(tmpdir(), "gh-smoke-work-")); |
| 77 | const request: typeof fetch = fetch; |
| 78 | let journal: Journal | undefined; |
| 79 | let stream: ChildProcess | undefined; |
| 80 | let streamExit: Promise<{ code: number | null; signal: NodeJS.Signals | null }> | undefined; |
| 81 | let primary: unknown; |
| 82 | let completed: Awaited<ReturnType<typeof runSmokeProtocol>> | undefined; |
| 83 | let interrupted: Error | undefined; |
| 84 | const interrupt = (signal: string) => { |
| 85 | interrupted ??= new Error(`Smoke interrupted by ${signal}`); |
| 86 | if (stream && stream.exitCode === null && stream.signalCode === null) stream.kill("SIGTERM"); |
| 87 | }; |
| 88 | const onInterrupt = () => interrupt("SIGINT"); |
| 89 | const onTerminate = () => interrupt("SIGTERM"); |
| 90 | process.once("SIGINT", onInterrupt); |
| 91 | process.once("SIGTERM", onTerminate); |
| 92 | const checkInterrupted = () => { if (interrupted) throw interrupted; }; |
| 93 | const fixture = new EventsGitHub(process.env.GH_EVAL_FIXTURE_TOKEN?.trim() || token, request); |
| 94 | const subscriber = new EventsGitHub(token, request); |
| 95 | const reviewer = new EventsGitHub(actorToken, request); |
| 96 | try { |
| 97 | const env = await prepareHome(home, { |
| 98 | gh, token, modelToken: "unused-by-smoke", observe: false, |
| 99 | }); |
| 100 | console.error("smoke: creating sandbox fixture"); |
| 101 | const created = await provision(fixture, org, "review-loop", workDir, env, journalPath, subscriber); |
| 102 | journal = created.journal; |
| 103 | const pr = created.fixture.pr; |
| 104 | if (!pr) throw new Error("Smoke fixture did not create its PR"); |
| 105 | const collector = new ReviewEventCollector(); |
| 106 | const ready = new StreamReadySignal(); |
| 107 | let stderr = ""; |
| 108 | stream = spawn(join(home, "bin/gh"), [ |
| 109 | "event", "listen", "--resource", pr.html_url, "--follow", "--timeout", "2m", |
| 110 | "--event", "com.github.pull_request.review.submitted.v1,com.github.pull_request.state.changed.v1", |
| 111 | ], { cwd: workDir, env, stdio: ["ignore", "pipe", "pipe"] }); |
| 112 | stream.stdout!.setEncoding("utf8"); |
| 113 | stream.stderr!.setEncoding("utf8"); |
| 114 | stream.stdout!.on("data", chunk => collector.write(String(chunk))); |
| 115 | stream.stderr!.on("data", chunk => { stderr += String(chunk); ready.write(String(chunk)); }); |
| 116 | streamExit = waitForExit(stream); |
| 117 | await Promise.race([ |
| 118 | ready.wait(20_000), |
| 119 | streamExit.then(result => Promise.reject(new Error( |
| 120 | `Event stream exited before attachment: code=${result.code} signal=${result.signal} ${stderr}`, |
no test coverage detected