|
| 1 | +// Publishes a dedicated GitHub check-run that summarizes tests which only |
| 2 | +// passed after retrying, so flaky passes are visible on the PR without |
| 3 | +// changing the outcome of the main test job. |
| 4 | +const https = require('https'); |
| 5 | + |
| 6 | +const DEFAULT_API_URL = 'https://api.github.com'; |
| 7 | + |
| 8 | +function parseFlakyCount(rawCount) { |
| 9 | + if (!rawCount) { |
| 10 | + return 0; |
| 11 | + } |
| 12 | + |
| 13 | + const parsedCount = Number.parseInt(rawCount, 10); |
| 14 | + if (!Number.isFinite(parsedCount) || parsedCount < 0) { |
| 15 | + return 0; |
| 16 | + } |
| 17 | + |
| 18 | + return parsedCount; |
| 19 | +} |
| 20 | + |
| 21 | +function normalizeSummary(rawSummary) { |
| 22 | + if (typeof rawSummary !== 'string') { |
| 23 | + return ''; |
| 24 | + } |
| 25 | + |
| 26 | + return rawSummary.trim(); |
| 27 | +} |
| 28 | + |
| 29 | +function buildCheckOutput(flakyCount, flakySummary, testStepConclusion) { |
| 30 | + if (testStepConclusion === 'failure') { |
| 31 | + const summaryParts = [ |
| 32 | + 'The primary test step failed. See the job logs for the failing assertion details.', |
| 33 | + '', |
| 34 | + 'This flaky check is informational only.' |
| 35 | + ]; |
| 36 | + |
| 37 | + if (flakyCount > 0) { |
| 38 | + summaryParts.push(''); |
| 39 | + summaryParts.push( |
| 40 | + `Detected ${flakyCount} test(s) that passed after retrying before the job failed:` |
| 41 | + ); |
| 42 | + |
| 43 | + if (flakySummary) { |
| 44 | + summaryParts.push(flakySummary); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + return { |
| 49 | + conclusion: 'neutral', // This is done intentionally to not clutter the failed jobs list. |
| 50 | + title: 'Test job failed', |
| 51 | + summary: summaryParts.join('\n') |
| 52 | + }; |
| 53 | + } |
| 54 | + |
| 55 | + if (flakyCount === 0) { |
| 56 | + return { |
| 57 | + conclusion: 'success', |
| 58 | + title: 'No flaky tests detected', |
| 59 | + summary: 'All tests passed on their first attempt.' |
| 60 | + }; |
| 61 | + } |
| 62 | + |
| 63 | + return { |
| 64 | + conclusion: 'neutral', |
| 65 | + title: `${flakyCount} test(s) passed after retry`, |
| 66 | + summary: [ |
| 67 | + 'The primary test job succeeded, but these tests only passed after retrying:', |
| 68 | + '', |
| 69 | + flakySummary || `Detected ${flakyCount} flaky test(s).` |
| 70 | + ].join('\n') |
| 71 | + }; |
| 72 | +} |
| 73 | + |
| 74 | +function createCheckRun({ |
| 75 | + apiUrl, |
| 76 | + token, |
| 77 | + repository, |
| 78 | + sha, |
| 79 | + name, |
| 80 | + detailsUrl, |
| 81 | + output |
| 82 | +}) { |
| 83 | + return new Promise((resolve, reject) => { |
| 84 | + const requestUrl = new URL(`/repos/${repository}/check-runs`, apiUrl); |
| 85 | + const payload = JSON.stringify({ |
| 86 | + name, |
| 87 | + head_sha: sha, |
| 88 | + status: 'completed', |
| 89 | + conclusion: output.conclusion, |
| 90 | + details_url: detailsUrl, |
| 91 | + output: { |
| 92 | + title: output.title, |
| 93 | + summary: output.summary |
| 94 | + } |
| 95 | + }); |
| 96 | + |
| 97 | + const request = https.request( |
| 98 | + requestUrl, |
| 99 | + { |
| 100 | + method: 'POST', |
| 101 | + headers: { |
| 102 | + 'accept': 'application/vnd.github+json', |
| 103 | + 'authorization': `Bearer ${token}`, |
| 104 | + 'content-type': 'application/json', |
| 105 | + 'content-length': Buffer.byteLength(payload), |
| 106 | + 'user-agent': 'obs-studio-node-flaky-check-reporter', |
| 107 | + 'x-github-api-version': '2022-11-28' |
| 108 | + } |
| 109 | + }, |
| 110 | + response => { |
| 111 | + let responseBody = ''; |
| 112 | + response.setEncoding('utf8'); |
| 113 | + response.on('data', chunk => { |
| 114 | + responseBody += chunk; |
| 115 | + }); |
| 116 | + response.on('end', () => { |
| 117 | + if (response.statusCode >= 200 && response.statusCode < 300) { |
| 118 | + resolve(); |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + reject( |
| 123 | + new Error( |
| 124 | + `GitHub API request failed (${response.statusCode}): ${responseBody}` |
| 125 | + ) |
| 126 | + ); |
| 127 | + }); |
| 128 | + } |
| 129 | + ); |
| 130 | + |
| 131 | + request.on('error', reject); |
| 132 | + request.write(payload); |
| 133 | + request.end(); |
| 134 | + }); |
| 135 | +} |
| 136 | + |
| 137 | +async function main() { |
| 138 | + const checkName = process.env.FLAKY_CHECK_NAME; |
| 139 | + const token = process.env.GITHUB_TOKEN; |
| 140 | + const repository = process.env.GITHUB_REPOSITORY; |
| 141 | + // pull_request workflows run against a synthetic merge commit; using the PR head |
| 142 | + // SHA keeps the custom check navigable from the PR Checks tab. |
| 143 | + const sha = process.env.FLAKY_CHECK_SHA || process.env.GITHUB_SHA; |
| 144 | + const apiUrl = process.env.GITHUB_API_URL || DEFAULT_API_URL; |
| 145 | + const serverUrl = process.env.GITHUB_SERVER_URL || 'https://github.com'; |
| 146 | + const runId = process.env.GITHUB_RUN_ID; |
| 147 | + const testStepConclusion = process.env.TEST_STEP_CONCLUSION || 'success'; |
| 148 | + // The reporter publishes bounded flaky metadata through GITHUB_OUTPUT so this |
| 149 | + // step does not need to read raw test artifacts before calling the Checks API. |
| 150 | + const flakyCount = parseFlakyCount(process.env.FLAKY_TEST_COUNT); |
| 151 | + const flakySummary = normalizeSummary(process.env.FLAKY_TEST_SUMMARY); |
| 152 | + |
| 153 | + if (!token) { |
| 154 | + throw new Error('GITHUB_TOKEN is required to publish the flaky test check.'); |
| 155 | + } |
| 156 | + |
| 157 | + if (!repository || !sha || !checkName) { |
| 158 | + throw new Error('GITHUB_REPOSITORY, FLAKY_CHECK_SHA or GITHUB_SHA, and FLAKY_CHECK_NAME are required.'); |
| 159 | + } |
| 160 | + |
| 161 | + const output = buildCheckOutput(flakyCount, flakySummary, testStepConclusion); |
| 162 | + const detailsUrl = runId |
| 163 | + ? `${serverUrl}/${repository}/actions/runs/${runId}` |
| 164 | + : undefined; |
| 165 | + |
| 166 | + await createCheckRun({ |
| 167 | + apiUrl, |
| 168 | + token, |
| 169 | + repository, |
| 170 | + sha, |
| 171 | + name: checkName, |
| 172 | + detailsUrl, |
| 173 | + output |
| 174 | + }); |
| 175 | + |
| 176 | + console.log( |
| 177 | + `Published "${checkName}" with conclusion "${output.conclusion}"` + |
| 178 | + ` from ${flakyCount} flaky test(s).` |
| 179 | + ); |
| 180 | +} |
| 181 | + |
| 182 | +main().catch(error => { |
| 183 | + console.error(error.message); |
| 184 | + process.exitCode = 1; |
| 185 | +}); |
0 commit comments