import crypto from "node:crypto"; import fs from "node:fs"; const apiRoot = (process.env.GITEA_API_URL || "https://git.dumas.ddns.net/api/v1").replace(/\/$/, ""); const repository = process.env.GITEA_REPOSITORY || "chris.dumas/ComfyUiPortalNext"; const token = process.env.GITEA_API_TOKEN; const environment = process.env.E2E_ENVIRONMENT || "unknown"; const runId = process.env.GITEA_RUN_ID || "local"; const sha = process.env.GITEA_SHA || "unknown"; const mode = process.argv[2] || "report"; const resultPath = process.argv[3] || "e2e/test-results.json"; const serverRoot = apiRoot.replace(/\/api\/v1$/, ""); function fail(message) { throw new Error(message); } function headers() { return { Accept: "application/json", "Content-Type": "application/json", Authorization: "token " + token }; } async function request(path, options = {}) { const response = await fetch(apiRoot + path, { ...options, headers: { ...headers(), ...(options.headers || {}) } }); const body = response.status === 204 ? null : await response.json().catch(() => null); if (!response.ok) throw new Error("Gitea API " + response.status + ": " + (body?.message || response.statusText)); return body; } function labels(issue) { return (issue.labels || []).map((item) => typeof item === "string" ? item : item.name); } function keyFor(name) { return "ui-test:" + environment + ":" + name; } function marker(key) { return ""; } function fingerprint(key) { return crypto.createHash("sha256").update(key).digest("hex").slice(0, 16); } function issueTitle(name) { return "[" + environment + "] UI test failure: " + name; } function flattenSuites(suites, output = []) { for (const suite of suites || []) { for (const spec of suite.specs || []) { const results = (spec.tests || []).flatMap((item) => item.results || []); const failed = results.find((result) => result.status === "failed" || result.error); output.push({ name: [suite.title, spec.title].filter(Boolean).join(" › "), passed: Boolean(spec.ok) && !failed, durationMs: results.reduce((total, result) => total + (result.duration || 0), 0), error: failed?.error?.message || failed?.error?.stack || (failed ? "Browser test status: " + failed.status : ""), }); } flattenSuites(suite.suites, output); } return output; } function readResults() { if (!fs.existsSync(resultPath)) return []; try { return flattenSuites(JSON.parse(fs.readFileSync(resultPath, "utf8")).suites); } catch { return []; } } async function allIssues() { const result = []; for (let page = 1; page < 20; page += 1) { const batch = await request("/repos/" + repository + "/issues?state=all&limit=50&page=" + page); result.push(...batch); if (batch.length < 50) break; } return result.filter((issue) => labels(issue).includes("automated") && labels(issue).includes("ui-test")); } async function ensureLabel(name, color) { const existing = await request("/repos/" + repository + "/labels?limit=50"); const found = existing.find((label) => label.name === name); if (found) return found.id; return (await request("/repos/" + repository + "/labels", { method: "POST", body: JSON.stringify({ name, color }) })).id; } async function comment(issue, body) { const comments = await request("/repos/" + repository + "/issues/" + issue.number + "/comments?limit=50"); if (comments.some((item) => item.body?.includes("qa-run: " + runId))) return; await request("/repos/" + repository + "/issues/" + issue.number + "/comments", { method: "POST", body: JSON.stringify({ body: body + "\n\nqa-run: " + runId }) }); } async function closeResolved(issue, name) { if (!issue || issue.state !== "open") return; await comment(issue, marker(keyFor(name)) + "\nAutomated UI test passed on commit " + sha + "; closing this blocker."); await request("/repos/" + repository + "/issues/" + issue.number, { method: "PATCH", body: JSON.stringify({ state: "closed" }) }); } async function reportFailure(issue, test, labelIds) { const key = keyFor(test.name); const body = [ marker(key), "**Automated UI failure**", "", "- Environment: " + environment, "- Test: " + test.name, "- Commit: " + sha, "- Run: [" + runId + "](" + serverRoot + "/" + repository + "/actions/runs/" + runId + ")", "- Fingerprint: " + fingerprint(key), "- Duration: " + test.durationMs + " ms", "", "Failure details:", test.error || "The browser test failed without a serialized error.", ].join("\n"); if (!issue) issue = await request("/repos/" + repository + "/issues", { method: "POST", body: JSON.stringify({ title: issueTitle(test.name), body, labels: labelIds }) }); else { if (issue.state === "closed") await request("/repos/" + repository + "/issues/" + issue.number, { method: "PATCH", body: JSON.stringify({ state: "open" }) }); await comment(issue, body); } console.log("Tracked UI failure in issue #" + issue.number); } async function main() { if (!token) fail("GITEA_API_TOKEN is required for QA issue automation"); const issues = await allIssues(); if (mode === "gate") { const blockers = issues.filter((issue) => issue.state === "open" && labels(issue).includes("qa-blocker")); if (blockers.length) { console.error("Open automated QA blockers prevent this build:"); blockers.forEach((issue) => console.error("- #" + issue.number + ": " + issue.title)); process.exitCode = 1; } else console.log("No open automated QA blockers."); return; } const tests = readResults(); if (!tests.length && process.env.BROWSER_TEST_OUTCOME === "failure") tests.push({ name: "browser-suite", passed: false, durationMs: 0, error: "Browser suite failed before producing parseable results." }); const labelIds = [await ensureLabel("automated", "5319e7"), await ensureLabel("ui-test", "1d76db"), await ensureLabel("qa-blocker", "d73a4a")]; for (const test of tests) { const issue = issues.find((item) => item.body?.includes(marker(keyFor(test.name)))); if (test.passed) await closeResolved(issue, test.name); else await reportFailure(issue, test, labelIds); } if (tests.some((test) => !test.passed)) process.exitCode = 1; } main().catch((error) => { console.error(error.message); process.exitCode = 1; });