-
Notifications
You must be signed in to change notification settings - Fork 102
/
Copy pathgenerate-release-notes.ts
192 lines (151 loc) · 5.5 KB
/
generate-release-notes.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// This script is used to generate release notes for a given release.
// It correctly ignores cherry-picked commits from previous patch releases.
import chalk from 'chalk'
import shell from 'shelljs'
import yargs from 'yargs'
import { hideBin } from 'yargs/helpers'
interface Commit {
hash: string
type: string
message: string
}
const argv = yargs(hideBin(process.argv))
.option('lastTag', {
type: 'string',
description: 'The last release tag (optional)',
})
.option('toRef', {
type: 'string',
description: 'The reference to generate release notes up to',
default: 'HEAD',
})
.option('verbose', {
type: 'boolean',
description: 'Enable verbose logging',
default: true,
})
.parseSync()
function log(message: string) {
if (argv.verbose) {
console.error(message)
}
}
function getLastTag(): string {
log(chalk.blue('Fetching the last tag...'))
// Get all tags sorted by version, then filter for those that look like semantic version tags
// Note: We can't use `git describe --tags --abbrev=0` because that returns the latest tag that is a direct ancestor of the current branch
// But patch releases are not direct ancestors
const result = shell.exec(
'git tag --sort=-v:refname | grep -E "^.*[0-9]+\\.[0-9]+\\.[0-9]+" | head -n 1',
{
silent: true,
}
)
if (result.code !== 0) {
console.error(chalk.red('Error fetching last tag:', result.stderr))
process.exit(1)
}
return result.stdout.trim()
}
function getTagPrefix(tag: string): string {
const match = tag.match(/^(.*)v?\d+\.\d+\.\d+$/)
return match ? match[1] : ''
}
function getCommits(lastTag: string, toRef: string): Commit[] {
log(chalk.blue(`Fetching commits between ${lastTag} and ${toRef}...`))
const range = `${lastTag}..${toRef}`
// Assume GNU toolchain, unless on darwin then assume BSD toolchain.
let reverseCommand = 'tac'
if (process.platform === 'darwin') {
reverseCommand = 'tail -r'
}
const result = shell.exec(`git rev-list ${range} --oneline | ${reverseCommand}`, { silent: true })
if (result.code !== 0) {
console.error(chalk.red('Error fetching git commits:', result.stderr))
process.exit(1)
}
return result.stdout
.trim()
.split('\n')
.map((line) => {
const [hash, ...messageParts] = line.split(' ')
const message = messageParts.join(' ')
const match = message.match(/^(\w+)(\(.*?\))?:/)
const type = match ? match[1] : 'other'
return { hash, type, message }
})
}
function generateMainReleaseNotes(commits: Commit[], version: string): string {
const features = commits.filter((c) => c.type === 'feat')
const fixes = commits.filter((c) => c.type === 'fix')
const others = commits.filter((c) => !['feat', 'fix'].includes(c.type))
let notes = `# Summary
We've updated the app to fix bugs, enhance our features, and improve overall performance.
`
if (features.length > 0) {
notes += `## Features
${features.map((c) => `${c.hash} ${c.message}`).join('\n')}
`
}
if (fixes.length > 0) {
notes += `## Bug Fixes
${fixes.map((c) => `${c.hash} ${c.message}`).join('\n')}
`
}
if (others.length > 0) {
notes += `## Other
${others.map((c) => `${c.hash} ${c.message}`).join('\n')}
`
}
return notes.trim()
}
function generatePatchReleaseNotes(commits: Commit[], version: string): string {
const [major, minor, patch] = version.split('.')
const previousVersion = `${major}.${minor}.${parseInt(patch) - 1}`
return `# Summary
This release is a patch on top of v${previousVersion}, with additional commits.
## Commits included
${commits.map((c) => `${c.hash} ${c.message}`).join('\n')}
`
}
function main() {
const { lastTag: userProvidedLastTag, toRef } = argv
const packageJson = require('../package.json')
const currentVersion = packageJson.version
log(chalk.blue(`Generating release notes for ${currentVersion}...`))
const lastTag = userProvidedLastTag || getLastTag()
log(chalk.green(`Using last tag: ${lastTag}`))
const tagPrefix = getTagPrefix(lastTag)
log(chalk.green(`Detected tag prefix: "${tagPrefix}"`))
let commits = getCommits(lastTag, toRef)
log(chalk.green(`Found ${commits.length} commits`))
// Determine if it's a patch release
const isPatchRelease = currentVersion.split('.')[2] !== '0'
if (!isPatchRelease) {
// For main releases, remove cherry-picked commits from the previous main release
const [major, minor] = currentVersion.split('.')
const lastMainTag = `${tagPrefix}${major}.${parseInt(minor) - 1}.0`
if (lastMainTag !== lastTag) {
const possibleCherryPickedCommits = getCommits(lastMainTag, lastTag)
const possibleCherryPickedMessages = new Set(
possibleCherryPickedCommits.map((commit) => commit.message)
)
const cherryPickedCommits = commits.filter((commit) =>
possibleCherryPickedMessages.has(commit.message)
)
commits = commits.filter((commit) => !possibleCherryPickedMessages.has(commit.message))
log(chalk.yellow(`Possible cherry-picked commits: ${possibleCherryPickedCommits.length}`))
log(chalk.yellow(`Skipping ${cherryPickedCommits.length} cherry-picked commits`))
cherryPickedCommits.forEach((commit) => {
log(chalk.yellow(` ${commit.hash} ${commit.message}`))
})
}
}
const releaseNotes = isPatchRelease
? generatePatchReleaseNotes(commits, currentVersion)
: generateMainReleaseNotes(commits, currentVersion)
// Output release notes to stdout
console.log(releaseNotes)
log(chalk.green('Release notes generated successfully'))
}
main()