|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { execSync } from "child_process"; |
| 4 | +import fs from "fs"; |
| 5 | + |
| 6 | +function getCommitsSinceLastTag() { |
| 7 | + try { |
| 8 | + // Get the last tag |
| 9 | + const lastTag = execSync("git describe --tags --abbrev=0 2>/dev/null", { |
| 10 | + encoding: "utf8", |
| 11 | + }).trim(); |
| 12 | + |
| 13 | + // Get commits since last tag |
| 14 | + const commits = execSync(`git log ${lastTag}..HEAD --pretty=format:"%s"`, { |
| 15 | + encoding: "utf8", |
| 16 | + }).trim(); |
| 17 | + |
| 18 | + return commits ? commits.split("\n") : []; |
| 19 | + } catch (error) { |
| 20 | + // If no tags exist, get all commits from the beginning |
| 21 | + try { |
| 22 | + const commits = execSync('git log --pretty=format:"%s"', { |
| 23 | + encoding: "utf8", |
| 24 | + }).trim(); |
| 25 | + |
| 26 | + return commits ? commits.split("\n") : []; |
| 27 | + } catch (e) { |
| 28 | + return []; |
| 29 | + } |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +function categorizeCommits(commits) { |
| 34 | + const categories = { |
| 35 | + features: [], |
| 36 | + fixes: [], |
| 37 | + improvements: [], |
| 38 | + chores: [], |
| 39 | + breaking: [], |
| 40 | + other: [] |
| 41 | + }; |
| 42 | + |
| 43 | + commits.forEach(commit => { |
| 44 | + // Skip auto-increment commits |
| 45 | + if (commit.includes("🤖 Auto-increment version")) { |
| 46 | + return; |
| 47 | + } |
| 48 | + |
| 49 | + const lowerCommit = commit.toLowerCase(); |
| 50 | + |
| 51 | + if (lowerCommit.includes("breaking") || lowerCommit.includes("breaking change")) { |
| 52 | + categories.breaking.push(commit); |
| 53 | + } else if (lowerCommit.startsWith("feat:") || lowerCommit.startsWith("feature:") || lowerCommit.includes("add ")) { |
| 54 | + categories.features.push(commit); |
| 55 | + } else if (lowerCommit.startsWith("fix:") || lowerCommit.startsWith("bug:") || lowerCommit.includes("fix ")) { |
| 56 | + categories.fixes.push(commit); |
| 57 | + } else if (lowerCommit.startsWith("improve:") || lowerCommit.startsWith("enhancement:") || lowerCommit.includes("improve ")) { |
| 58 | + categories.improvements.push(commit); |
| 59 | + } else if (lowerCommit.startsWith("chore:") || lowerCommit.startsWith("ci:") || lowerCommit.startsWith("docs:")) { |
| 60 | + categories.chores.push(commit); |
| 61 | + } else { |
| 62 | + categories.other.push(commit); |
| 63 | + } |
| 64 | + }); |
| 65 | + |
| 66 | + return categories; |
| 67 | +} |
| 68 | + |
| 69 | +function generateReleaseNotes(version, categories) { |
| 70 | + let notes = `# Release ${version}\n\n`; |
| 71 | + |
| 72 | + // Add date |
| 73 | + const date = new Date().toISOString().split('T')[0]; |
| 74 | + notes += `**Release Date:** ${date}\n\n`; |
| 75 | + |
| 76 | + // Breaking changes (if any) |
| 77 | + if (categories.breaking.length > 0) { |
| 78 | + notes += "## ⚠️ Breaking Changes\n\n"; |
| 79 | + categories.breaking.forEach(commit => { |
| 80 | + notes += `- ${commit}\n`; |
| 81 | + }); |
| 82 | + notes += "\n"; |
| 83 | + } |
| 84 | + |
| 85 | + // Features |
| 86 | + if (categories.features.length > 0) { |
| 87 | + notes += "## ✨ New Features\n\n"; |
| 88 | + categories.features.forEach(commit => { |
| 89 | + notes += `- ${commit}\n`; |
| 90 | + }); |
| 91 | + notes += "\n"; |
| 92 | + } |
| 93 | + |
| 94 | + // Bug fixes |
| 95 | + if (categories.fixes.length > 0) { |
| 96 | + notes += "## 🐛 Bug Fixes\n\n"; |
| 97 | + categories.fixes.forEach(commit => { |
| 98 | + notes += `- ${commit}\n`; |
| 99 | + }); |
| 100 | + notes += "\n"; |
| 101 | + } |
| 102 | + |
| 103 | + // Improvements |
| 104 | + if (categories.improvements.length > 0) { |
| 105 | + notes += "## 🚀 Improvements\n\n"; |
| 106 | + categories.improvements.forEach(commit => { |
| 107 | + notes += `- ${commit}\n`; |
| 108 | + }); |
| 109 | + notes += "\n"; |
| 110 | + } |
| 111 | + |
| 112 | + // Other changes |
| 113 | + if (categories.other.length > 0) { |
| 114 | + notes += "## 📝 Other Changes\n\n"; |
| 115 | + categories.other.forEach(commit => { |
| 116 | + notes += `- ${commit}\n`; |
| 117 | + }); |
| 118 | + notes += "\n"; |
| 119 | + } |
| 120 | + |
| 121 | + // Chores (optional, usually less important) |
| 122 | + if (categories.chores.length > 0) { |
| 123 | + notes += "## 🔧 Maintenance\n\n"; |
| 124 | + categories.chores.forEach(commit => { |
| 125 | + notes += `- ${commit}\n`; |
| 126 | + }); |
| 127 | + notes += "\n"; |
| 128 | + } |
| 129 | + |
| 130 | + // Add footer |
| 131 | + notes += "---\n\n"; |
| 132 | + notes += `**Full Changelog:** https://github.com/${process.env.GITHUB_REPOSITORY}/compare/...${version}\n\n`; |
| 133 | + |
| 134 | + return notes.trim(); |
| 135 | +} |
| 136 | + |
| 137 | +function main() { |
| 138 | + const version = process.argv[2]; |
| 139 | + |
| 140 | + if (!version) { |
| 141 | + console.error("Usage: node generate-release-notes.js <version>"); |
| 142 | + process.exit(1); |
| 143 | + } |
| 144 | + |
| 145 | + console.log(`Generating release notes for version ${version}...`); |
| 146 | + |
| 147 | + const commits = getCommitsSinceLastTag(); |
| 148 | + console.log(`Found ${commits.length} commits since last release`); |
| 149 | + |
| 150 | + const categories = categorizeCommits(commits); |
| 151 | + const releaseNotes = generateReleaseNotes(version, categories); |
| 152 | + |
| 153 | + // Output for GitHub Actions |
| 154 | + if (process.env.GITHUB_OUTPUT) { |
| 155 | + // Escape newlines for GitHub Actions |
| 156 | + const escapedNotes = releaseNotes.replace(/\n/g, '%0A').replace(/\r/g, '%0D'); |
| 157 | + fs.appendFileSync(process.env.GITHUB_OUTPUT, `release_notes=${escapedNotes}\n`); |
| 158 | + |
| 159 | + // Also write to a file for easier debugging |
| 160 | + fs.writeFileSync('release-notes.md', releaseNotes); |
| 161 | + console.log("Release notes written to release-notes.md"); |
| 162 | + } else { |
| 163 | + // For local testing |
| 164 | + console.log("\n" + "=".repeat(50)); |
| 165 | + console.log("RELEASE NOTES:"); |
| 166 | + console.log("=".repeat(50)); |
| 167 | + console.log(releaseNotes); |
| 168 | + } |
| 169 | +} |
| 170 | + |
| 171 | +// Run if this script is executed directly |
| 172 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 173 | + main(); |
| 174 | +} |
| 175 | + |
| 176 | +export { generateReleaseNotes, categorizeCommits, getCommitsSinceLastTag }; |
0 commit comments