|
| 1 | +// This script checks that any new changelog entries added in a PR |
| 2 | +// remain in the [Unreleased] section after the PR is merged. |
| 3 | + |
| 4 | +const fs = require('fs'); |
| 5 | + |
| 6 | +if (process.argv.length < 5) { |
| 7 | + console.error( |
| 8 | + 'Usage: node check-changelog-diff.cjs <base-file> <pr-file> <merged-file>', |
| 9 | + ); |
| 10 | + |
| 11 | + // eslint-disable-next-line n/no-process-exit |
| 12 | + process.exit(1); |
| 13 | +} |
| 14 | + |
| 15 | +/* eslint-disable n/no-sync */ |
| 16 | +// The type of these is inferred as `Buffer` when using "utf-8" directly instead |
| 17 | +// of an options object. Even though it's a plain JavaScript file, it's nice to |
| 18 | +// keep the types correct. |
| 19 | +const baseContent = fs.readFileSync(process.argv[2], { |
| 20 | + encoding: 'utf-8', |
| 21 | +}); |
| 22 | + |
| 23 | +const prContent = fs.readFileSync(process.argv[3], { |
| 24 | + encoding: 'utf-8', |
| 25 | +}); |
| 26 | + |
| 27 | +const mergedContent = fs.readFileSync(process.argv[4], { |
| 28 | + encoding: 'utf-8', |
| 29 | +}); |
| 30 | +/* eslint-enable n/no-sync */ |
| 31 | + |
| 32 | +/** |
| 33 | + * Extract the "[Unreleased]" section from the changelog content. |
| 34 | + * |
| 35 | + * This doesn't actually parse the Markdown, it just looks for the section |
| 36 | + * header and collects lines until the next section header. |
| 37 | + * |
| 38 | + * @param {string} content - The changelog content. |
| 39 | + * @returns {Set<string>} The lines in the "[Unreleased]" section as a |
| 40 | + * {@link Set}. |
| 41 | + */ |
| 42 | +function getUnreleasedSection(content) { |
| 43 | + const lines = content.split('\n'); |
| 44 | + |
| 45 | + let inUnreleased = false; |
| 46 | + const sectionLines = new Set(); |
| 47 | + |
| 48 | + for (const line of lines) { |
| 49 | + // Find unreleased header. |
| 50 | + if (line.trim().match(/^##\s+\[Unreleased\]/u)) { |
| 51 | + inUnreleased = true; |
| 52 | + continue; |
| 53 | + } |
| 54 | + |
| 55 | + // Stop if we hit the next version header (## [x.x.x]). |
| 56 | + if (inUnreleased && line.trim().match(/^##\s+\[/u)) { |
| 57 | + break; |
| 58 | + } |
| 59 | + |
| 60 | + // If inside the unreleased header, add lines to the set. |
| 61 | + if (inUnreleased) { |
| 62 | + sectionLines.add(line.trim()); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return sectionLines; |
| 67 | +} |
| 68 | + |
| 69 | +/** |
| 70 | + * Get the lines that were added in the PR content compared to the base content. |
| 71 | + * |
| 72 | + * @param {Set<string>} oldLines - The base changelog content. |
| 73 | + * @param {Set<string>} newLines - The PR changelog content. |
| 74 | + * @returns {string[]} The added lines as an array of strings. |
| 75 | + */ |
| 76 | +function getAddedLines(oldLines, newLines) { |
| 77 | + return Array.from(newLines).filter( |
| 78 | + (line) => line.length > 0 && !oldLines.has(line) && !line.startsWith('#'), |
| 79 | + ); |
| 80 | +} |
| 81 | + |
| 82 | +const mergedUnreleased = getUnreleasedSection(mergedContent); |
| 83 | +const addedLines = getAddedLines( |
| 84 | + getUnreleasedSection(baseContent), |
| 85 | + getUnreleasedSection(prContent), |
| 86 | +); |
| 87 | + |
| 88 | +const missingLines = []; |
| 89 | +for (const line of addedLines) { |
| 90 | + if (!mergedUnreleased.has(line)) { |
| 91 | + missingLines.push(line); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +if (missingLines.length > 0) { |
| 96 | + console.error( |
| 97 | + `The following lines added in the PR are missing from the "Unreleased" section after merge:\n\n ${missingLines.join('\n ')}\n\nPlease update your pull request and ensure that new changelog entries remain in the "Unreleased" section.`, |
| 98 | + ); |
| 99 | + |
| 100 | + process.exitCode = 1; |
| 101 | +} |
0 commit comments