import { readFileSync, writeFileSync } from 'node:fs'; import { esc, extractHistory, renderHistory } from './bench-utils.mjs'; const shortSha = process.env.COMMIT_SHA.slice(0, 7); const commitMsg = process.env.COMMIT_MSG; const runUrl = process.env.WORKFLOW_RUN_URL; const oldBody = readFileSync(`${process.env.RUNNER_TEMP}/old-comment.txt`, 'utf8'); let history = extractHistory(oldBody); // If previous comment had completed results, archive them into history if (oldBody.includes('\u2705 Benchmark complete!')) { // Extract only the main result table (before any
block) const mainSection = oldBody.split('
')[0] || ''; const commitMatch = mainSection.match( /Latest commit:<\/strong><\/td>(?:)?([a-f0-9]+)(?:<\/code>)?\s*(.*?)<\/td>/, ); const prevSha = commitMatch ? commitMatch[1] : ''; const prevMsg = commitMatch ? commitMatch[2] : ''; if (prevSha) { const rowRe = /(Ubuntu|macOS|Windows):<\/strong><\/td>(.*?)<\/td><\/tr>/g; const entry = { sha: prevSha, msg: prevMsg }; for (let m = rowRe.exec(mainSection); m !== null; m = rowRe.exec(mainSection)) { entry[m[1].toLowerCase()] = m[2]; } if (prevSha !== shortSha && !history.some((h) => h.sha === prevSha)) { history.unshift(entry); } } } // Keep max 50 entries history = history.slice(0, 50); const jsonComment = ``; let body = `\n${jsonComment}\n`; body += '## \u26a1 Performance Benchmark\n\n'; body += `\n`; body += '
Latest commit:${shortSha} ${esc(commitMsg)}
Status:\u26a1 Benchmark in progress...
\n\n'; body += `[Workflow run](${runUrl})`; const historyHtml = renderHistory(history); if (historyHtml) { body += `\n\n
\nHistory\n\n${historyHtml}\n\n
`; } writeFileSync(`${process.env.RUNNER_TEMP}/new-comment.md`, body);