|
| 1 | +import {readFileSync} from 'node:fs'; |
| 2 | +import {basename, dirname} from 'node:path'; |
| 3 | + |
| 4 | +const MAX_FAILURES = 30; |
| 5 | +const MAX_TRACEBACK_LINES = 50; |
| 6 | +const MAX_BODY_LENGTH = 65_000; |
| 7 | +export const COMMENT_MARKER = '<!-- BACKEND_TEST_FAILURES -->'; |
| 8 | + |
| 9 | +export const commitMarker = sha => `<!-- BACKEND_TEST_FAILURES_COMMIT:${sha} -->`; |
| 10 | + |
| 11 | +export function parseFailures(files, core) { |
| 12 | + return files.flatMap(file => { |
| 13 | + let data; |
| 14 | + try { |
| 15 | + data = JSON.parse(readFileSync(file, 'utf8')); |
| 16 | + } catch (e) { |
| 17 | + core.warning(`Skipping ${file}: ${e.message}`); |
| 18 | + return []; |
| 19 | + } |
| 20 | + if (!Array.isArray(data.tests)) return []; |
| 21 | + |
| 22 | + const artifactDir = basename(dirname(file)); |
| 23 | + return data.tests |
| 24 | + .filter(t => t.outcome === 'failed') |
| 25 | + .map(t => ({ |
| 26 | + nodeid: t.nodeid ?? 'unknown', |
| 27 | + longrepr: (t.call ?? t.setup ?? t.teardown ?? {}).longrepr ?? '', |
| 28 | + artifactDir, |
| 29 | + })); |
| 30 | + }); |
| 31 | +} |
| 32 | + |
| 33 | +// Maps artifact directory names to GitHub Actions job log URLs. |
| 34 | +const JOB_MATCHERS = [ |
| 35 | + { |
| 36 | + // pytest-results-backend-{runId}-{N} → "backend test (N)" |
| 37 | + dir: /^pytest-results-backend-\d+-(?<shard>\d+)$/, |
| 38 | + job: (jobs, {shard}) => |
| 39 | + jobs.find(j => j.name.match(/^backend test \((\d+)\)$/)?.[1] === shard), |
| 40 | + }, |
| 41 | + { |
| 42 | + // pytest-results-migration-{runId} → "backend migration tests" |
| 43 | + dir: /^pytest-results-migration-\d+$/, |
| 44 | + job: jobs => jobs.find(j => j.name.includes('backend migration tests')), |
| 45 | + }, |
| 46 | + { |
| 47 | + // pytest-results-monolith-dbs-{runId} → "monolith-dbs test" |
| 48 | + dir: /^pytest-results-monolith-dbs-\d+$/, |
| 49 | + job: jobs => jobs.find(j => j.name.includes('monolith-dbs test')), |
| 50 | + }, |
| 51 | +]; |
| 52 | + |
| 53 | +async function getJobUrls(failures, github, context) { |
| 54 | + const uniqueDirs = [...new Set(failures.map(f => f.artifactDir))]; |
| 55 | + if (uniqueDirs.length === 0) return {}; |
| 56 | + |
| 57 | + const {owner, repo} = context.repo; |
| 58 | + const jobs = await github.paginate(github.rest.actions.listJobsForWorkflowRun, { |
| 59 | + owner, |
| 60 | + repo, |
| 61 | + run_id: context.runId, |
| 62 | + }); |
| 63 | + |
| 64 | + const findJobUrl = dir => { |
| 65 | + for (const {dir: pattern, job: findJob} of JOB_MATCHERS) { |
| 66 | + const m = dir.match(pattern); |
| 67 | + if (m) return findJob(jobs, m.groups ?? {})?.html_url; |
| 68 | + } |
| 69 | + return undefined; |
| 70 | + }; |
| 71 | + |
| 72 | + return Object.fromEntries( |
| 73 | + uniqueDirs.map(dir => [dir, findJobUrl(dir)]).filter(([, url]) => url) |
| 74 | + ); |
| 75 | +} |
| 76 | + |
| 77 | +// Returns the set of test nodeids already present in a comment body. |
| 78 | +export function extractNodeids(body) { |
| 79 | + if (!body) return new Set(); |
| 80 | + // Iterator.prototype.map() — available since Node 22. |
| 81 | + return new Set(body.matchAll(/<code>([^<]+)<\/code>/g).map(m => m[1])); |
| 82 | +} |
| 83 | + |
| 84 | +function truncateBody(body) { |
| 85 | + if (body.length <= MAX_BODY_LENGTH) return body; |
| 86 | + return ( |
| 87 | + body.slice(0, MAX_BODY_LENGTH - 100) + |
| 88 | + '\n\n... (truncated due to GitHub comment size limit)\n' |
| 89 | + ); |
| 90 | +} |
| 91 | + |
| 92 | +// Renders <details> blocks for each failure (no header). |
| 93 | +export function buildFailureBlocks(failures) { |
| 94 | + return failures |
| 95 | + .map(({nodeid, longrepr, jobUrl}) => { |
| 96 | + let tb = longrepr; |
| 97 | + if (tb) { |
| 98 | + const lines = tb.split('\n'); |
| 99 | + if (lines.length > MAX_TRACEBACK_LINES) { |
| 100 | + tb = |
| 101 | + lines.slice(0, MAX_TRACEBACK_LINES).join('\n') + |
| 102 | + `\n... (${lines.length - MAX_TRACEBACK_LINES} more lines)`; |
| 103 | + } |
| 104 | + } |
| 105 | + const logLink = jobUrl ? ` — <a href="${jobUrl}">log</a>` : ''; |
| 106 | + return ( |
| 107 | + `<details><summary><code>${nodeid}</code>${logLink}</summary>\n\n` + |
| 108 | + `\`\`\`\n${tb || 'No traceback available'}\n\`\`\`\n\n</details>\n\n` |
| 109 | + ); |
| 110 | + }) |
| 111 | + .join(''); |
| 112 | +} |
| 113 | + |
| 114 | +// Builds a full comment body (header + blocks). Used when creating a new comment. |
| 115 | +export function buildCommentBody(failures, {runUrl, sha, repoUrl}) { |
| 116 | + const capped = failures.slice(0, MAX_FAILURES); |
| 117 | + const shortSha = sha.slice(0, 7); |
| 118 | + const commitUrl = `${repoUrl}/commit/${sha}`; |
| 119 | + |
| 120 | + let body = `${COMMENT_MARKER}\n${commitMarker(sha)}\n## Backend Test Failures\n\nFailures on [\`${shortSha}\`](${commitUrl}) in [this run](${runUrl}):\n\n`; |
| 121 | + body += buildFailureBlocks(capped); |
| 122 | + |
| 123 | + if (failures.length > MAX_FAILURES) { |
| 124 | + body += `... and ${failures.length - MAX_FAILURES} more failures.\n`; |
| 125 | + } |
| 126 | + |
| 127 | + return truncateBody(body); |
| 128 | +} |
| 129 | + |
| 130 | +// Called from within each test shard. Reads the shard's own pytest.json from the |
| 131 | +// filesystem, appends any new failures to the PR comment, creating it if needed. |
| 132 | +export async function reportShard({github, context, core}) { |
| 133 | + const jsonPath = process.env.PYTEST_JSON_PATH; |
| 134 | + const artifactDir = process.env.PYTEST_ARTIFACT_DIR; |
| 135 | + |
| 136 | + if (!jsonPath) { |
| 137 | + core.warning('PYTEST_JSON_PATH not set — skipping.'); |
| 138 | + return; |
| 139 | + } |
| 140 | + |
| 141 | + const rawFailures = parseFailures([jsonPath], core); |
| 142 | + if (rawFailures.length === 0) { |
| 143 | + core.info('No failures in this shard — skipping.'); |
| 144 | + return; |
| 145 | + } |
| 146 | + |
| 147 | + const shardFailures = artifactDir |
| 148 | + ? rawFailures.map(f => ({...f, artifactDir})) |
| 149 | + : rawFailures; |
| 150 | + |
| 151 | + let jobUrls = {}; |
| 152 | + try { |
| 153 | + jobUrls = await getJobUrls(shardFailures, github, context); |
| 154 | + } catch (e) { |
| 155 | + core.warning(`Could not fetch job URLs: ${e.message}`); |
| 156 | + } |
| 157 | + const failures = shardFailures.map(f => ({...f, jobUrl: jobUrls[f.artifactDir]})); |
| 158 | + |
| 159 | + const {owner, repo} = context.repo; |
| 160 | + const prNumber = context.payload.pull_request.number; |
| 161 | + const {sha} = context; |
| 162 | + const marker = commitMarker(sha); |
| 163 | + |
| 164 | + const comments = await github.paginate(github.rest.issues.listComments, { |
| 165 | + owner, |
| 166 | + repo, |
| 167 | + issue_number: prNumber, |
| 168 | + }); |
| 169 | + // Only match comments for the same commit — a new push gets a fresh comment. |
| 170 | + const existing = comments.find(c => c.body?.includes(marker)); |
| 171 | + |
| 172 | + // Append-only: skip failures whose nodeid is already in the comment. |
| 173 | + const seen = extractNodeids(existing?.body); |
| 174 | + const newFailures = failures.filter(f => !seen.has(f.nodeid)).slice(0, MAX_FAILURES); |
| 175 | + |
| 176 | + if (newFailures.length === 0) { |
| 177 | + core.info('All failures already reported — skipping.'); |
| 178 | + return; |
| 179 | + } |
| 180 | + |
| 181 | + if (existing) { |
| 182 | + await github.rest.issues.updateComment({ |
| 183 | + owner, |
| 184 | + repo, |
| 185 | + comment_id: existing.id, |
| 186 | + body: truncateBody(existing.body + buildFailureBlocks(newFailures)), |
| 187 | + }); |
| 188 | + core.info(`Appended ${newFailures.length} failure(s) to comment.`); |
| 189 | + } else { |
| 190 | + const repoUrl = `https://github.com/${owner}/${repo}`; |
| 191 | + const runUrl = `${repoUrl}/actions/runs/${context.runId}`; |
| 192 | + await github.rest.issues.createComment({ |
| 193 | + owner, |
| 194 | + repo, |
| 195 | + issue_number: prNumber, |
| 196 | + body: buildCommentBody(failures, {runUrl, sha, repoUrl}), |
| 197 | + }); |
| 198 | + core.info( |
| 199 | + `Created failure comment with ${Math.min(failures.length, MAX_FAILURES)} failure(s).` |
| 200 | + ); |
| 201 | + } |
| 202 | +} |
0 commit comments