#!/usr/bin/env node
// Render the sticky PR shard-triage comment from $MATRIX + the select log (argv[2]) to stdout.
const fs = require("fs");
function readLog(path) {
if (!path) return "";
try {
return fs.readFileSync(path, "utf8").trim();
} catch {
return "";
}
}
const marker = process.env.MARKER || "";
const smokeActive = process.env.SMOKE_ACTIVE === "true";
const headSha = (process.env.HEAD_SHA || "").slice(0, 9);
const attempt = process.env.ATTEMPT || "1";
const log = readLog(process.argv[2]);
let matrix;
try {
matrix = JSON.parse(process.env.MATRIX || "{}");
} catch {
matrix = {};
}
const shards = Array.isArray(matrix.include) ? matrix.include : [];
// smoke_active only says selection ran; a global_paths hit still returns the full
// matrix, so infer the real mode from the log rather than the flag alone.
const fellBackToFull = /falling back to full matrix|full matrix:/.test(log);
const mode = smokeActive && !fellBackToFull ? "smoke (changed-path)" : "full matrix";
const specsOf = (s) => (Array.isArray(s.run_files) ? s.run_files.length : 0);
const totalSpecs = shards.reduce((n, s) => n + specsOf(s), 0);
const shardList = shards
.map((s) => `${s.testfolder} (${specsOf(s)})`)
.join(" 路 ");
const lines = [
marker,
"### 馃幆 Playwright shard selection",
`Mode: **${mode}** 路 **${shards.length} shard${shards.length === 1 ? "" : "s"}** 路 **${totalSpecs} spec${totalSpecs === 1 ? "" : "s"}**`,
"",
shardList || "_no shards selected_",
];
if (log) {
lines.push(
"",
"Why these shards
",
"",
"```",
log,
"```",
"",
" "
);
}
lines.push(
"",
`Updated for \`${headSha}\` 路 attempt ${attempt} 路 updates in place on every push.`
);
process.stdout.write(lines.join("\n") + "\n");