|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { readFileSync } from 'node:fs' |
| 4 | +import { resolve } from 'node:path' |
| 5 | +import { execSync } from 'node:child_process' |
| 6 | + |
| 7 | +interface PublishedPackage { |
| 8 | + name: string |
| 9 | + version: string |
| 10 | +} |
| 11 | + |
| 12 | +interface PRInfo { |
| 13 | + number: number |
| 14 | + packages: Array<{ name: string; pkgPath: string; version: string }> |
| 15 | +} |
| 16 | + |
| 17 | +/** |
| 18 | + * Parse CHANGELOG.md to extract PR numbers from the latest version entry |
| 19 | + */ |
| 20 | +function extractPRsFromChangelog( |
| 21 | + changelogPath: string, |
| 22 | + version: string, |
| 23 | +): Array<number> { |
| 24 | + try { |
| 25 | + const content = readFileSync(changelogPath, 'utf-8') |
| 26 | + const lines = content.split('\n') |
| 27 | + |
| 28 | + let inTargetVersion = false |
| 29 | + let foundVersion = false |
| 30 | + const prNumbers = new Set<number>() |
| 31 | + |
| 32 | + for (let i = 0; i < lines.length; i++) { |
| 33 | + const line = lines[i] |
| 34 | + |
| 35 | + // Check for version header (e.g., "## 0.21.0") |
| 36 | + if (line.startsWith('## ')) { |
| 37 | + const versionMatch = line.match(/^## (\d+\.\d+\.\d+)/) |
| 38 | + if (versionMatch) { |
| 39 | + if (versionMatch[1] === version) { |
| 40 | + inTargetVersion = true |
| 41 | + foundVersion = true |
| 42 | + } else if (inTargetVersion) { |
| 43 | + // We've moved to the next version, stop processing |
| 44 | + break |
| 45 | + } |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + // Extract PR numbers from links like [#302](https://github.com/TanStack/config/pull/302) |
| 50 | + if (inTargetVersion) { |
| 51 | + const prMatches = line.matchAll( |
| 52 | + /\[#(\d+)\]\(https:\/\/github\.com\/[^/]+\/[^/]+\/pull\/\d+\)/g, |
| 53 | + ) |
| 54 | + for (const match of prMatches) { |
| 55 | + prNumbers.add(parseInt(match[1], 10)) |
| 56 | + } |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + if (!foundVersion) { |
| 61 | + console.warn( |
| 62 | + `Warning: Could not find version ${version} in ${changelogPath}`, |
| 63 | + ) |
| 64 | + } |
| 65 | + |
| 66 | + return Array.from(prNumbers) |
| 67 | + } catch (error) { |
| 68 | + console.error(`Error reading changelog at ${changelogPath}:`, error) |
| 69 | + return [] |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/** |
| 74 | + * Group PRs by their numbers and collect all packages they contributed to |
| 75 | + */ |
| 76 | +function groupPRsByNumber( |
| 77 | + publishedPackages: Array<PublishedPackage>, |
| 78 | +): Map<number, PRInfo> { |
| 79 | + const prMap = new Map<number, PRInfo>() |
| 80 | + |
| 81 | + for (const pkg of publishedPackages) { |
| 82 | + const pkgPath = `packages/${pkg.name.replace('@tanstack/', '')}` |
| 83 | + const changelogPath = resolve(process.cwd(), pkgPath, 'CHANGELOG.md') |
| 84 | + |
| 85 | + const prNumbers = extractPRsFromChangelog(changelogPath, pkg.version) |
| 86 | + |
| 87 | + for (const prNumber of prNumbers) { |
| 88 | + if (!prMap.has(prNumber)) { |
| 89 | + prMap.set(prNumber, { number: prNumber, packages: [] }) |
| 90 | + } |
| 91 | + prMap.get(prNumber)!.packages.push({ |
| 92 | + name: pkg.name, |
| 93 | + pkgPath: pkgPath, |
| 94 | + version: pkg.version, |
| 95 | + }) |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + return prMap |
| 100 | +} |
| 101 | + |
| 102 | +/** |
| 103 | + * Post a comment on a GitHub PR using gh CLI |
| 104 | + */ |
| 105 | +async function commentOnPR(pr: PRInfo, repository: string): Promise<void> { |
| 106 | + const { number, packages } = pr |
| 107 | + |
| 108 | + // Build the comment body |
| 109 | + let comment = `🎉 This PR has been released!\n\n` |
| 110 | + |
| 111 | + for (const pkg of packages) { |
| 112 | + // Link to the package's changelog and version anchor |
| 113 | + const changelogUrl = `https://github.com/${repository}/blob/main/${pkg.pkgPath}/CHANGELOG.md#${pkg.version.replaceAll('.', '')}` |
| 114 | + comment += `- [${pkg.name}@${pkg.version}](${changelogUrl})\n` |
| 115 | + } |
| 116 | + |
| 117 | + comment += `\nThank you for your contribution!` |
| 118 | + |
| 119 | + try { |
| 120 | + // Use gh CLI to post the comment |
| 121 | + execSync(`gh pr comment ${number} --body '${comment.replace(/'/g, '"')}'`, { |
| 122 | + stdio: 'inherit', |
| 123 | + }) |
| 124 | + console.log(`✓ Commented on PR #${number}`) |
| 125 | + } catch (error) { |
| 126 | + console.error(`✗ Failed to comment on PR #${number}:`, error) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +/** |
| 131 | + * Main function |
| 132 | + */ |
| 133 | +async function main() { |
| 134 | + // Read published packages from environment variable (set by GitHub Actions) |
| 135 | + const publishedPackagesJson = process.env.PUBLISHED_PACKAGES |
| 136 | + const repository = process.env.REPOSITORY |
| 137 | + |
| 138 | + if (!publishedPackagesJson) { |
| 139 | + console.log('No packages were published. Skipping PR comments.') |
| 140 | + return |
| 141 | + } |
| 142 | + |
| 143 | + if (!repository) { |
| 144 | + console.log('Repository is missing. Skipping PR comments.') |
| 145 | + return |
| 146 | + } |
| 147 | + |
| 148 | + let publishedPackages: Array<PublishedPackage> |
| 149 | + try { |
| 150 | + publishedPackages = JSON.parse(publishedPackagesJson) |
| 151 | + } catch (error) { |
| 152 | + console.error('Failed to parse PUBLISHED_PACKAGES:', error) |
| 153 | + process.exit(1) |
| 154 | + } |
| 155 | + |
| 156 | + if (publishedPackages.length === 0) { |
| 157 | + console.log('No packages were published. Skipping PR comments.') |
| 158 | + return |
| 159 | + } |
| 160 | + |
| 161 | + console.log(`Processing ${publishedPackages.length} published package(s)...`) |
| 162 | + |
| 163 | + // Group PRs by number |
| 164 | + const prMap = groupPRsByNumber(publishedPackages) |
| 165 | + |
| 166 | + if (prMap.size === 0) { |
| 167 | + console.log('No PRs found in CHANGELOGs. Nothing to comment on.') |
| 168 | + return |
| 169 | + } |
| 170 | + |
| 171 | + console.log(`Found ${prMap.size} PR(s) to comment on...`) |
| 172 | + |
| 173 | + // Comment on each PR |
| 174 | + for (const pr of prMap.values()) { |
| 175 | + await commentOnPR(pr, repository) |
| 176 | + } |
| 177 | + |
| 178 | + console.log('✓ Done!') |
| 179 | +} |
| 180 | + |
| 181 | +main().catch((error) => { |
| 182 | + console.error('Fatal error:', error) |
| 183 | + process.exit(1) |
| 184 | +}) |
0 commit comments