forked from nuxt/image
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupdate-changelog.ts
84 lines (73 loc) · 2.96 KB
/
update-changelog.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
import { execSync } from 'node:child_process'
import { $fetch } from 'ofetch'
import { inc } from 'semver'
import { generateMarkDown, getCurrentGitBranch, loadChangelogConfig } from 'changelogen'
import { consola } from 'consola'
import { determineBumpType, getContributors, getLatestCommits, loadWorkspace } from './_utils'
async function main() {
const releaseBranch = await getCurrentGitBranch()
const workspace = await loadWorkspace(process.cwd())
const config = await loadChangelogConfig(process.cwd(), {})
const commits = await getLatestCommits().then(commits => commits.filter(
c => config.types[c.type] && !(c.type === 'chore' && c.scope === 'deps' && !c.isBreaking),
))
const bumpType = await determineBumpType()
const newVersion = inc(workspace.find('@nuxt/image').data.version, bumpType || 'patch')
const changelog = await generateMarkDown(commits, config)
// Create and push a branch with bumped versions if it has not already been created
const branchExists = execSync(`git ls-remote --heads origin v${newVersion}`).toString().trim().length > 0
if (!branchExists) {
execSync('git config --global user.email "[email protected]"')
execSync('git config --global user.name "Daniel Roe"')
execSync(`git checkout -b v${newVersion}`)
for (const pkg of workspace.packages.filter(p => !p.data.private)) {
workspace.setVersion(pkg.data.name, newVersion!)
}
await workspace.save()
execSync(`git commit -am v${newVersion}`)
execSync(`git push -u origin v${newVersion}`)
}
// Get the current PR for this release, if it exists
const [currentPR] = await $fetch(`https://api.github.com/repos/nuxt/image/pulls?head=nuxt:v${newVersion}`)
const contributors = await getContributors()
const releaseNotes = [
currentPR?.body.replace(/## 👉 Changelog[\s\S]*$/, '') || `> ${newVersion} is the next ${bumpType} release.\n>\n> **Timetable**: to be announced.`,
'## 👉 Changelog',
changelog
.replace(/^## v.*\n/, '')
.replace(`...${releaseBranch}`, `...v${newVersion}`)
.replace(/### ❤️ Contributors[\s\S]*$/, ''),
'### ❤️ Contributors',
contributors.map(c => `- ${c.name} (@${c.username})`).join('\n'),
].join('\n')
// Create a PR with release notes if none exists
if (!currentPR) {
return await $fetch('https://api.github.com/repos/nuxt/image/pulls', {
method: 'POST',
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
body: {
title: `v${newVersion}`,
head: `v${newVersion}`,
base: releaseBranch,
body: releaseNotes,
draft: true,
},
})
}
// Update release notes if the pull request does exist
await $fetch(`https://api.github.com/repos/nuxt/image/pulls/${currentPR.number}`, {
method: 'PATCH',
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`,
},
body: {
body: releaseNotes,
},
})
}
main().catch((err) => {
consola.error(err)
process.exit(1)
})