UNPKG

playwright-gh-action-reporter

Version:

A tool to post Playwright test results as comments on GitHub PRs.

96 lines (85 loc) 3.06 kB
const fs = require('fs'); const axios = require('axios'); const reportPath = 'playwright-report/pw_report.json'; const githubToken = process.env.GITHUB_TOKEN; const repo = process.env.GITHUB_REPOSITORY; const deploymentSha = process.env.DEPLOYMENT_SHA; const runId = process.env.RUN_ID; if (!githubToken || !repo || !deploymentSha || !runId) { console.error('Required environment variables are missing.'); process.exit(1); } // Read and parse the JSON report let report; try { report = JSON.parse(fs.readFileSync(reportPath, 'utf8')); } catch (error) { console.error('Error reading or parsing the report file:', error); process.exit(1); } // Extract the test results const { stats } = report; const { expected: passCount, unexpected: failCount, flaky } = stats; const totalPass = passCount + flaky; const totaltest = passCount + failCount + flaky; const duration = (stats.duration / 1000).toFixed(2); // in seconds // Construct the artifact link const artifactLink = `https://github.com/${repo}/actions/runs/${runId}#artifacts`; // Prepare the results summary let resultsSummary = `## Playwright Test Results: ${failCount > 0 ? '❌ Some tests failed' : '🎉 All tests passed'}\n\n`; resultsSummary += `- **Total Duration:** ${duration} seconds\n`; resultsSummary += `- **Total Test(s):** ${totaltest}\n`; resultsSummary += `- **Passed Test(s) ✅:** ${totalPass}\n`; resultsSummary += `- **Failed Test(s) ❌:** ${failCount}\n`; resultsSummary += `- **Flaky Test(s) ⚠️:** ${flaky}\n\n`; resultsSummary += `You can download the detailed [Playwright Report here](${artifactLink}).\n`; // Get the PR number using the deployment SHA const getPRNumber = async () => { try { const response = await axios.get(`https://api.github.com/repos/${repo}/commits/${deploymentSha}/pulls`, { headers: { Authorization: `token ${githubToken}`, }, }); return response.data[0] ? response.data[0].number : null; } catch (error) { console.error('Error getting PR number:', error); throw error; } }; // Post comment to the PR const commentOnPR = async (prNumber, body) => { try { await axios.post( `https://api.github.com/repos/${repo}/issues/${prNumber}/comments`, { body }, { headers: { Authorization: `token ${githubToken}`, Accept: 'application/vnd.github.v3+json', }, }, ); } catch (error) { console.error('Error posting comment:', error); throw error; } }; const main = async () => { try { const prNumber = await getPRNumber(); if (prNumber) { await commentOnPR(prNumber, resultsSummary); console.log(`Comment posted on PR #${prNumber}`); } else { console.log('No PR found for the commit.'); } if (failCount > 0) { process.exit(1); // Fail GitHub Actions if there are failed tests } } catch (error) { console.error('Error:', error); process.exit(1); } }; main();