ci: require PR model and agent declarations

This commit is contained in:
Nwflower committed 2026-12-03 02:54:13 -07:00
1 parent 5565bb71ee
commit 5e9621b7dd
6 files changed
+145

No files matched your search

+58
View File
@@ -0,0 +1,58 @@
import { readFile } from "node:fs/promises";
import { pathToFileURL } from "node:url";
export function missingDeclarations(body) {
const missing = new Set(["model", "agent"]);
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 if (/^ {0,3}model:[ \t]+[\p{L}\p{N}_.-]+\/[\p{L}\p{N}_.-]+[ \t]*$/u.test(line)) {
missing.delete("model");
} else if (/^ {0,3}agent:[ \t]+[\p{L}\p{N}][\p{L}\p{N}_.+() /-]*$/u.test(line.trimEnd())) {
missing.delete("agent");
}
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 agent used (format examples: model: deepseek/deepseek-R1 and agent: WorkBuddy).`);
process.exitCode = 1;
}
}