forked from keystonejs/keystone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprepare-release.js
261 lines (218 loc) · 8.12 KB
/
prepare-release.js
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
const { spawnSync } = require('child_process')
const { readFileSync, writeFileSync } = require('fs')
// eslint-disable-next-line import/no-extraneous-dependencies
const { default: getReleasePlan } = require('@changesets/get-release-plan')
// eslint-disable-next-line import/no-extraneous-dependencies
const { getInfo } = require('@changesets/get-github-info')
// TODO: move this to CI linting
const verbs = new Set(['Adds', 'Changes', 'Fixes', 'Moves', 'Removes', 'Updates', 'Upgrade'])
// TODO: derived?
const publicPackages = [
'@keystone-6/auth',
'@keystone-6/cloudinary',
'@keystone-6/core',
'@keystone-6/document-renderer',
'@keystone-6/fields-document',
]
const cves = [
// {
// id: 'CVE-2023-23936',
// href: 'https://github.com/advisories/GHSA-5r9g-qh6m-jxff',
// upstream: true,
// description: `
// An upstream transitive dependency \`undici\` is vulnerable to a HTTP header CRLF injection vulnerability.
// We have upgraded to a version of \`@prisma/core\` that uses a fixed \`undici\`.
// `,
// },
]
function gitCommitsSince (tag) {
const { stdout } = spawnSync('git', ['rev-list', `^${tag}`, 'HEAD'])
return stdout
.toString('utf-8')
.split('\n')
.filter(x => x)
}
function gitCommitsFor (path) {
const { stdout } = spawnSync('git', [
'log',
'--pretty=format:%H',
'--follow',
'HEAD',
'--',
path,
])
return stdout
.toString('utf-8')
.split('\n')
.map(x => x.replace(/[^A-Za-z0-9]/g, '').slice(0, 40))
}
function gitCommitDescription (commit) {
const { stdout } = spawnSync('git', ['log', '--oneline', commit])
return stdout.toString('utf-8').split('\n', 1).pop().slice(10)
}
async function fetchData (tag) {
const { changesets, releases } = await getReleasePlan('.')
// find the commits since the tag
const revs = gitCommitsSince(tag)
console.error(`${revs.length} commits since ${tag}`)
if (revs.length === 0) throw new Error('No commits')
if (revs.length > 200) throw new Error('Too many commits')
// tag changesets with their commits
for (const changeset of changesets) {
const commits = gitCommitsFor(`.changeset/${changeset.id}.md`)
const commit = commits.slice(-1).pop()
console.error(
`changeset ${changeset.id} has ${commits.length} commits, the first commit is ${commit}`
)
if (!revs.includes(commit)) throw new Error(`Unexpected commit ${changeset.commit}`)
changeset.commit = commit
}
// list all the contributors
const previousContributors = JSON.parse(
readFileSync('.changeset/contributors.json').toString('utf-8')
)
const githubCommits = {}
for (const commit of revs) {
let { user, pull } = await getInfo({ repo: 'keystonejs/keystone', commit })
pull = pull || gitCommitDescription(commit).match(/#([0-9]+)/)?.[1]
console.error(`commit ${commit}, user ${user}, pull #${pull}`)
const first = !previousContributors.includes(user)
githubCommits[commit] = { commit, user, pull, first }
}
// augment changesets with git information
const changes = []
for (const changeset of changesets) {
const { releases, summary, commit } = changeset
// TODO: move this to CI linting
const describedType = summary.split(' ')[0]
if (!verbs.has(describedType)) {
console.warn(` Verb '${describedType}' is non-standard for a changeset`)
}
// poor semver precedence
let type
for (const release of releases) {
if (['minor', 'patch', undefined].includes(type) && release.type === 'major') type = 'major'
if (['patch', undefined].includes(type) && release.type === 'minor') type = 'minor'
if ([undefined].includes(type) && release.type === 'patch') type = 'patch'
}
if (!type) throw new Error('Unknown type')
// only public packages, then strip the namespace
const packages = releases
.filter(x => publicPackages.includes(x.name))
.map(x => x.name.replace('@keystone-6/', ''))
.sort()
const githubCommit = githubCommits[commit]
changes.push({
...githubCommit,
summary,
type,
packages,
})
}
// if no changeset was associated with a commit, we still want to acknowledge the work
for (const [commit, githubCommit] of Object.entries(githubCommits)) {
if (changes.find(x => x.commit === commit)) continue
changes.push(githubCommit)
}
// find the set of our contributors
const contributors = [...new Set([...previousContributors, ...changes.map(x => x.user)])]
// only public packages
const packages = releases
.filter(x => publicPackages.includes(x.name))
.filter(x => x.type !== 'none')
.map(x => `${x.name}@${x.newVersion}`)
.sort()
return { packages, changes, contributors }
}
function formatPackagesChanged (packages) {
return `The following packages have been updated
%%%
${packages.join('\n')}
%%%`.replace(/%/g, '`')
}
function formatChange ({ packages, summary, pull, user }) {
return `- \`[${packages.join(', ')}]\` ${summary} (#${pull}) @${user}`
}
function formatCVE ({ id, href, upstream, description }) {
description = description.replace(/\n/g, ' ').replace(/\s+/g, ' ').trim()
return `- [\`${id}\`](${href}) - ${description}`
}
function formatLink (pull) {
return `[#${pull}](https://github.com/keystonejs/keystone/pull/${pull})`
}
function sortByCommit (a, b) {
return a.commit.localeCompare(b.commit)
}
function groupPullsByUser (list) {
const result = {}
for (const item of list) {
if (!item.pull) continue
result[item.user] ||= []
result[item.user].push(item.pull)
}
return Object.entries(result)
.map(([user, pulls]) => ({ user, pulls }))
.sort((a, b) => a.user.localeCompare(b.user))
}
async function generateGitHubReleaseText (previousTag) {
if (!previousTag) throw new Error('Missing tag')
const date = new Date().toISOString().slice(0, 10).replace(/\-/g, '_')
const { packages, changes, contributors } = await fetchData(previousTag)
// writeFileSync(`./CHANGELOG-${date}.json`, JSON.stringify({ packages, changes, contributors }, null, 2))
// return process.exit(0)
// const { packages, changes, contributors } = JSON.parse(readFileSync(`./CHANGELOG-${date}.json`))
const output = []
output.push(formatPackagesChanged(packages))
output.push('')
const breaking = changes.filter(x => x.type === 'major').sort(sortByCommit)
const features = changes.filter(x => x.type === 'minor').sort(sortByCommit)
const fixes = changes.filter(x => x.type === 'patch').sort(sortByCommit)
if (breaking.length) {
output.push(...[`#### Breaking Changes`, ...breaking.map(formatChange), ``])
}
if (features.length) {
output.push(...[`#### New Features`, ...features.map(formatChange), ``])
}
if (fixes.length) {
output.push(...[`#### Bug Fixes`, ...fixes.map(formatChange), ``])
}
if (cves.length) {
output.push(
...[
`#### :rotating_light: Security Updates`,
`We have identified and fixed ${cves.length}${
cves.some(x => x.upstream) ? ' upstream' : ''
} security vulnerabilities`,
...cves.map(formatCVE),
``,
]
)
}
const first = changes.filter(x => x.first)
const unattributed = changes.filter(x => !x.type && !x.first)
if (first.length) {
const listf = groupPullsByUser(first)
output.push(`#### :seedling: New Contributors`)
output.push(
`Thanks to the following developers for making their first contributions to the project!`
)
output.push(
...listf.map(({ user, pulls }) => `- @${user} (${pulls.map(formatLink).join(',')})`)
)
output.push(``)
}
if (unattributed.length) {
const listu = groupPullsByUser(unattributed)
output.push(`#### :blue_heart: Acknowledgements `)
output.push(
`Lastly, thanks to ${listu
.map(({ user, pulls }) => `@${user} (${pulls.map(formatLink).join(',')})`)
.join(', ')} for changes not shown above, but none-the-less appreciated.`
)
output.push(``)
}
writeFileSync('./.changeset/contributors.json', JSON.stringify(contributors.sort(), null, 2))
writeFileSync(`./RELEASE-${date}.md`, output.join('\n'))
console.error('files written')
}
generateGitHubReleaseText(process.argv[2])