|
| 1 | +import { readFile, writeFile } from 'fs/promises' |
| 2 | +import { existsSync } from 'fs' |
| 3 | +import path from 'path' |
| 4 | + |
| 5 | +import { allVersions } from '@/versions/lib/all-versions' |
| 6 | + |
| 7 | +const REST_API_DESCRIPTION_ROOT = 'rest-api-description' |
| 8 | +const OUTPUT_PATH = 'data/reusables/rest-api/breaking-changes-changelog.md' |
| 9 | + |
| 10 | +interface VersionMapping { |
| 11 | + sourceDir: string |
| 12 | + ifversionExpr: string |
| 13 | +} |
| 14 | + |
| 15 | +interface VersionSection { |
| 16 | + version: string |
| 17 | + content: string |
| 18 | +} |
| 19 | + |
| 20 | +// Build a list of { sourceDir, ifversionExpr } tuples from allVersions. |
| 21 | +// For example: |
| 22 | +// fpt → source dir "api.github.com", ifversion "fpt" |
| 23 | +// ghec → source dir "ghec", ifversion "ghec" |
| 24 | +// ghes-3.14 → source dir "ghes-3.14", ifversion "ghes = 3.14" |
| 25 | +function buildVersionMappings(versionNames: Record<string, string>): VersionMapping[] { |
| 26 | + // Build reverse lookup: docs short name → source directory name |
| 27 | + // e.g. "fpt" → "api.github.com", "ghec" → "ghec" |
| 28 | + const reverseMapping: Record<string, string> = {} |
| 29 | + for (const [sourceDir, docsName] of Object.entries(versionNames)) { |
| 30 | + reverseMapping[docsName] = sourceDir |
| 31 | + } |
| 32 | + |
| 33 | + const mappings: VersionMapping[] = [] |
| 34 | + const seen = new Set<string>() |
| 35 | + |
| 36 | + for (const versionObj of Object.values(allVersions)) { |
| 37 | + const key = versionObj.openApiVersionName |
| 38 | + if (seen.has(key)) continue |
| 39 | + seen.add(key) |
| 40 | + |
| 41 | + let sourceDir: string |
| 42 | + let ifversionExpr: string |
| 43 | + |
| 44 | + if (versionObj.shortName === 'ghes') { |
| 45 | + // GHES versions: source dir is like "ghes-3.14", ifversion is "ghes = 3.14" |
| 46 | + sourceDir = `ghes-${versionObj.currentRelease}` |
| 47 | + ifversionExpr = `ghes = ${versionObj.currentRelease}` |
| 48 | + } else { |
| 49 | + // Non-GHES: look up source dir from reverse mapping |
| 50 | + sourceDir = reverseMapping[versionObj.shortName] || versionObj.shortName |
| 51 | + ifversionExpr = versionObj.shortName |
| 52 | + } |
| 53 | + |
| 54 | + mappings.push({ sourceDir, ifversionExpr }) |
| 55 | + } |
| 56 | + |
| 57 | + return mappings |
| 58 | +} |
| 59 | + |
| 60 | +// Resolve the changelog file path based on whether we're using |
| 61 | +// rest-api-description or the local github repo. |
| 62 | +export function getChangelogPath(sourceRepoDir: string, releaseDir: string): string { |
| 63 | + if (sourceRepoDir === REST_API_DESCRIPTION_ROOT) { |
| 64 | + return path.join(REST_API_DESCRIPTION_ROOT, 'descriptions-next', releaseDir, 'CHANGELOG.md') |
| 65 | + } |
| 66 | + // Local github repo dev workflow |
| 67 | + return path.join( |
| 68 | + sourceRepoDir, |
| 69 | + 'app', |
| 70 | + 'api', |
| 71 | + 'description', |
| 72 | + 'changelogs', |
| 73 | + releaseDir, |
| 74 | + 'CHANGELOG.md', |
| 75 | + ) |
| 76 | +} |
| 77 | + |
| 78 | +// Parse a CHANGELOG.md into an array of { version, content } objects |
| 79 | +// by splitting on `## Version YYYY-MM-DD` headings. |
| 80 | +// Strips the top-level `# REST API Breaking Changes for ...` title and intro paragraph. |
| 81 | +export function parseVersionSections(markdown: string): VersionSection[] { |
| 82 | + const lines = markdown.split('\n') |
| 83 | + const sections: VersionSection[] = [] |
| 84 | + let currentVersion: string | null = null |
| 85 | + let currentLines: string[] = [] |
| 86 | + let pastHeader = false |
| 87 | + |
| 88 | + for (const line of lines) { |
| 89 | + // Skip the top-level title (# REST API Breaking Changes ...) |
| 90 | + if (!pastHeader && line.startsWith('# ')) { |
| 91 | + pastHeader = true |
| 92 | + continue |
| 93 | + } |
| 94 | + |
| 95 | + // Skip intro paragraph lines before the first ## Version heading |
| 96 | + const versionMatch = line.match(/^## Version (\d{4}-\d{2}-\d{2})/) |
| 97 | + if (versionMatch) { |
| 98 | + // Save previous section if any |
| 99 | + if (currentVersion) { |
| 100 | + sections.push({ |
| 101 | + version: currentVersion, |
| 102 | + content: currentLines.join('\n').trim(), |
| 103 | + }) |
| 104 | + } |
| 105 | + currentVersion = versionMatch[1] |
| 106 | + currentLines = [line] |
| 107 | + pastHeader = true |
| 108 | + continue |
| 109 | + } |
| 110 | + |
| 111 | + if (currentVersion) { |
| 112 | + currentLines.push(line) |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Save last section |
| 117 | + if (currentVersion) { |
| 118 | + sections.push({ |
| 119 | + version: currentVersion, |
| 120 | + content: currentLines.join('\n').trim(), |
| 121 | + }) |
| 122 | + } |
| 123 | + |
| 124 | + return sections |
| 125 | +} |
| 126 | + |
| 127 | +// Main function: reads changelogs from each release directory, wraps them |
| 128 | +// in product-version gating ({% ifversion %}) and API-version filtering |
| 129 | +// ({% if query.apiVersion %}), and writes a combined data file. |
| 130 | +export async function syncChangelogs( |
| 131 | + sourceRepoDir: string, |
| 132 | + versionNames: Record<string, string>, |
| 133 | + outputPath: string = OUTPUT_PATH, |
| 134 | +): Promise<void> { |
| 135 | + console.log(`\n▶️ Generating REST API breaking changes changelog...\n`) |
| 136 | + |
| 137 | + const mappings = buildVersionMappings(versionNames) |
| 138 | + const outputBlocks: string[] = [] |
| 139 | + |
| 140 | + for (const { sourceDir, ifversionExpr } of mappings) { |
| 141 | + const changelogPath = getChangelogPath(sourceRepoDir, sourceDir) |
| 142 | + |
| 143 | + if (!existsSync(changelogPath)) { |
| 144 | + console.log(` ⏭️ No changelog found for ${sourceDir}, skipping.`) |
| 145 | + continue |
| 146 | + } |
| 147 | + |
| 148 | + const markdown = await readFile(changelogPath, 'utf-8') |
| 149 | + const sections = parseVersionSections(markdown) |
| 150 | + |
| 151 | + if (sections.length === 0) { |
| 152 | + console.log(` ⏭️ No version sections found in changelog for ${sourceDir}, skipping.`) |
| 153 | + continue |
| 154 | + } |
| 155 | + |
| 156 | + const sectionBlocks = sections.map(({ version, content }) => { |
| 157 | + return [ |
| 158 | + `{% if query.apiVersion == nil or "${version}" <= query.apiVersion %}`, |
| 159 | + content, |
| 160 | + '', |
| 161 | + '{% endif %}', |
| 162 | + ].join('\n') |
| 163 | + }) |
| 164 | + |
| 165 | + const releaseBlock = [ |
| 166 | + `{% ifversion ${ifversionExpr} %}`, |
| 167 | + sectionBlocks.join('\n'), |
| 168 | + '{% endif %}', |
| 169 | + ].join('\n') |
| 170 | + |
| 171 | + outputBlocks.push(releaseBlock) |
| 172 | + console.log(` ✅ Processed changelog for ${sourceDir} (${sections.length} version sections)`) |
| 173 | + } |
| 174 | + |
| 175 | + if (outputBlocks.length === 0) { |
| 176 | + console.log(' ⚠️ No changelogs found. Skipping changelog generation.') |
| 177 | + return |
| 178 | + } |
| 179 | + |
| 180 | + // The generated Liquid uses quoted date strings in comparisons |
| 181 | + // (e.g., "2022-11-28" <= query.apiVersion) which is valid Liquid but |
| 182 | + // triggers the GHD016 lint rule that flags quoted conditional args. |
| 183 | + // The upstream changelogs may also contain docs.github.com URLs and |
| 184 | + // "deprecated" terminology that trigger docs-domain and GHD046 rules. |
| 185 | + const lintDisable = |
| 186 | + '<!-- markdownlint-disable liquid-quoted-conditional-arg search-replace GHD046 -->\n' |
| 187 | + const output = `${lintDisable + outputBlocks.join('\n\n')}\n` |
| 188 | + await writeFile(outputPath, output) |
| 189 | + console.log(`\n✅ Wrote ${outputPath}`) |
| 190 | +} |
0 commit comments