|
| 1 | +import { appendFileSync, readFileSync } from 'node:fs'; |
| 2 | +import { pathToFileURL } from 'node:url'; |
| 3 | + |
| 4 | +const SMALL_PR_FILES = 10; |
| 5 | +const SMALL_PR_LINES = 200; |
| 6 | +const requiredSections = ['## Description', '## Type of Change', '## How Has This Been Tested?', '## Checklist']; |
| 7 | +const checklistItems = [ |
| 8 | + 'My code follows the style guidelines of this project', |
| 9 | + 'I have performed a self-review of the code', |
| 10 | + 'I have commented my code, particularly in hard-to-understand areas', |
| 11 | + 'I have made corresponding changes to the documentation', |
| 12 | + 'My changes generate no new warnings', |
| 13 | + 'Any dependent changes have been merged and published in downstream modules', |
| 14 | + 'I have checked my code and corrected any misspellings', |
| 15 | +]; |
| 16 | + |
| 17 | +function isAddedOrModified(file) { |
| 18 | + return ['added', 'modified', 'renamed', 'copied'].includes(file.status); |
| 19 | +} |
| 20 | + |
| 21 | +export function reviewPullRequest(pullRequest, files) { |
| 22 | + const failures = []; |
| 23 | + const warnings = []; |
| 24 | + const messages = []; |
| 25 | + const body = pullRequest.body ?? ''; |
| 26 | + |
| 27 | + if (!body) { |
| 28 | + failures.push( |
| 29 | + 'Missing Summary - Add a `## Description` section explaining the motivation, changes, related issue, and required dependencies.', |
| 30 | + ); |
| 31 | + } |
| 32 | + if (!pullRequest.title) failures.push('Missing PR Title - Add a relevant pull request title.'); |
| 33 | + |
| 34 | + for (const section of requiredSections) { |
| 35 | + if (!body.includes(section)) failures.push(`Missing section: ${section}`); |
| 36 | + } |
| 37 | + for (const item of checklistItems) { |
| 38 | + if (!body.includes(`- [x] ${item}`)) warnings.push(`Unchecked checklist item: ${item}`); |
| 39 | + } |
| 40 | + |
| 41 | + const additions = files.reduce((total, file) => total + (file.additions ?? 0), 0); |
| 42 | + const deletions = files.reduce((total, file) => total + (file.deletions ?? 0), 0); |
| 43 | + const changedLines = additions + deletions; |
| 44 | + |
| 45 | + if (additions < deletions) messages.push('Thanks for removing more lines than you add.'); |
| 46 | + if (changedLines <= SMALL_PR_LINES && files.length <= SMALL_PR_FILES) { |
| 47 | + messages.push('Thanks for keeping this pull request small.'); |
| 48 | + } |
| 49 | + if (changedLines > SMALL_PR_LINES) warnings.push(`This PR changes more than ${SMALL_PR_LINES} lines.`); |
| 50 | + if (files.length > SMALL_PR_FILES) warnings.push(`This PR changes more than ${SMALL_PR_FILES} files.`); |
| 51 | + |
| 52 | + if (files.some((file) => isAddedOrModified(file) && /test.*\.[jt]sx?$/.test(file.filename))) { |
| 53 | + messages.push('Thanks for updating tests! Only you can prevent production fires.'); |
| 54 | + } |
| 55 | + if (files.some((file) => isAddedOrModified(file) && /(^|\/).*\.md$/.test(file.filename))) { |
| 56 | + messages.push('Thanks for updating documentation.'); |
| 57 | + } |
| 58 | + if (files.some((file) => file.status === 'modified' && /(^|\/)package\.json$/.test(file.filename))) { |
| 59 | + warnings.push('package.json changed. Ensure applicable lock files are updated.'); |
| 60 | + } |
| 61 | + |
| 62 | + return { failures, warnings, messages, additions, deletions, changedLines, changedFiles: files.length }; |
| 63 | +} |
| 64 | + |
| 65 | +async function fetchChangedFiles(repository, pullNumber, token) { |
| 66 | + const files = []; |
| 67 | + for (let page = 1; ; page += 1) { |
| 68 | + const response = await fetch( |
| 69 | + `https://api.github.com/repos/${repository}/pulls/${pullNumber}/files?per_page=100&page=${page}`, |
| 70 | + { |
| 71 | + headers: { |
| 72 | + Accept: 'application/vnd.github+json', |
| 73 | + Authorization: `Bearer ${token}`, |
| 74 | + 'X-GitHub-Api-Version': '2022-11-28', |
| 75 | + }, |
| 76 | + }, |
| 77 | + ); |
| 78 | + if (!response.ok) throw new Error(`GitHub returned HTTP ${response.status} while listing pull request files.`); |
| 79 | + const pageFiles = await response.json(); |
| 80 | + files.push(...pageFiles); |
| 81 | + if (pageFiles.length < 100) return files; |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +function renderSummary(review) { |
| 86 | + const sections = [ |
| 87 | + '# Pull request review', |
| 88 | + `Files: ${review.changedFiles} · Lines added: ${review.additions} · Lines removed: ${review.deletions}`, |
| 89 | + ]; |
| 90 | + if (review.failures.length) sections.push('## Required fixes', ...review.failures.map((item) => `- ${item}`)); |
| 91 | + if (review.warnings.length) sections.push('## Warnings', ...review.warnings.map((item) => `- ${item}`)); |
| 92 | + if (review.messages.length) sections.push('## Notes', ...review.messages.map((item) => `- ${item}`)); |
| 93 | + return `${sections.join('\n')}\n`; |
| 94 | +} |
| 95 | + |
| 96 | +async function main() { |
| 97 | + const event = JSON.parse(readFileSync(process.env.GITHUB_EVENT_PATH, 'utf8')); |
| 98 | + const pullRequest = event.pull_request; |
| 99 | + if (!pullRequest?.number) throw new Error('This workflow requires a pull_request event.'); |
| 100 | + |
| 101 | + const files = await fetchChangedFiles(process.env.GITHUB_REPOSITORY, pullRequest.number, process.env.GITHUB_TOKEN); |
| 102 | + const review = reviewPullRequest(pullRequest, files); |
| 103 | + const summary = renderSummary(review); |
| 104 | + if (process.env.GITHUB_STEP_SUMMARY) appendFileSync(process.env.GITHUB_STEP_SUMMARY, summary); |
| 105 | + process.stdout.write(summary); |
| 106 | + for (const failure of review.failures) process.stdout.write(`::error::${failure}\n`); |
| 107 | + for (const warning of review.warnings) process.stdout.write(`::warning::${warning}\n`); |
| 108 | + for (const message of review.messages) process.stdout.write(`::notice::${message}\n`); |
| 109 | + if (review.failures.length) process.exitCode = 1; |
| 110 | +} |
| 111 | + |
| 112 | +if (process.argv[1] && import.meta.url === pathToFileURL(process.argv[1]).href) { |
| 113 | + main().catch((error) => { |
| 114 | + process.stderr.write(`${error.message}\n`); |
| 115 | + process.exitCode = 1; |
| 116 | + }); |
| 117 | +} |
0 commit comments