import { readFile } from "node:fs/promises"; import { pathToFileURL } from "node:url"; const MODEL_LINE_RE = /^ {0,3}model:[ \t]+[\p{L}\p{N}][\p{L}\p{N}_.+() -]*\/[\p{L}\p{N}][\p{L}\p{N}_.+() -]*$/u; const TOOL_LINE_RE = /^ {0,3}(?:tool|agent):[ \t]+[\p{L}\p{N}][\p{L}\p{N}_.+() /-]*$/u; export function missingDeclarations(body) { const missing = new Set(["model", "tool"]); if (typeof body !== "string") return [...missing]; let fence = null; for (const line of body.replace(/|$)/g, "").split(/\r?\n/)) { const delimiter = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line); if (fence) { if (delimiter && delimiter[1][0] === fence[0] && delimiter[1].length >= fence.length && !delimiter[2].trim()) fence = null; } else if (delimiter) { fence = delimiter[1]; } else { const trimmed = line.trimEnd(); if (MODEL_LINE_RE.test(trimmed)) { missing.delete("model"); } else if (TOOL_LINE_RE.test(trimmed)) { missing.delete("tool"); } } if (missing.size === 0) break; } return [...missing]; } export async function checkPRModel(event, { apiUrl, repository, token, request = fetch }) { if (event.action !== "opened" || event.pull_request?.base?.ref !== "main") return "skipped"; const number = event.pull_request.number; if (!Number.isSafeInteger(number) || number < 1) throw new Error("Invalid pull request number"); const url = `${apiUrl}/repos/${repository}/pulls/${number}`; const headers = { Authorization: `Bearer ${token}`, Accept: "application/vnd.github+json", "Content-Type": "application/json", }; const read = await request(url, { headers, redirect: "error" }); if (!read.ok) throw new Error(`Could not read PR #${number} (HTTP ${read.status})`); const current = await read.json(); if (current.state !== "open" || current.base?.ref !== "main") return "skipped"; const missing = missingDeclarations(current.body); if (!missing.length) return "accepted"; const close = await request(url, { method: "PATCH", headers, redirect: "error", body: JSON.stringify({ state: "closed" }), }); if (!close.ok) throw new Error(`Could not close PR #${number} (HTTP ${close.status})`); return { status: "closed", missing }; } if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { const event = JSON.parse(await readFile(process.env.GITHUB_EVENT_PATH, "utf8")); const result = await checkPRModel(event, { apiUrl: process.env.GITHUB_API_URL, repository: process.env.GITHUB_REPOSITORY, token: process.env.GITHUB_TOKEN, }); if (result.status === "closed") { console.error(`::error::PR closed because its description is missing ${result.missing.join(" and ")}. Declare the actual model and coding tool used (format examples: model: deepseek/deepseek-R1 and tool: Cursor).`); process.exitCode = 1; } }