// gitea's events. The tool runtime imports this once the broker is bound. It watches the forge and // emits what appeared. // // Emits (novox/hq ADR 0046/0047): // module.gitea.repo.created — a repository appeared, however it was made (push, web UI, or tool) // // issue.opened and pull.merged are emitted from the tools (tools/index.ts), at the instant the mesh // takes that action — the natural point, and one process only. repo.created belongs here instead: // a repository is usually born from a `git push` or the web UI, which no tool sees, so polling the // repo list is the only way to catch every path — and keeping it out of the create-repo tool means // the fact is never announced twice from two processes. // // The polling is deliberately unhurried: an event a minute late is still an event, whereas hammering // the forge for an immediacy nobody asked for is not. import { emit } from "@novox/mesh-sdk/events"; import { GiteaClient } from "./client.js"; // Without a token there is nothing to watch; log and stay quiet rather than crash the runtime. let gitea: GiteaClient | null = null; try { gitea = GiteaClient.fromEnv(); } catch (err) { console.log(`[gitea] not watching — ${err instanceof Error ? err.message : String(err)}`); } // New repositories, by diffing the repo list. Primed silently on the first look, or a restart would // re-announce every existing repository as freshly created. const seen = new Set(); let primed = false; async function pollRepos(client: GiteaClient): Promise { const repos = await client.listRepos(1, 50); for (const repo of repos) { if (!seen.has(repo.full_name)) { if (primed) { await emit("module.gitea.repo.created", { full_name: repo.full_name, owner: repo.owner, name: repo.name, private: repo.private, html_url: repo.html_url, }); } seen.add(repo.full_name); } } primed = true; } if (gitea) { const client = gitea; const tick = (fn: () => Promise, everyMs: number): void => { const run = (): void => void fn().catch((err) => console.error(`[gitea] ${err}`)); setInterval(run, everyMs); run(); }; tick(() => pollRepos(client), 60_000); console.log("[gitea] watching for new repositories"); }