Nuxt/scripts/update-changelog.ts

85 lines
2.9 KiB
TypeScript
Raw Permalink Normal View History

2023-04-20 09:40:07 +00:00
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'
2023-04-20 09:40:07 +00:00
async function main () {
const releaseBranch = await getCurrentGitBranch()
2023-04-20 09:40:07 +00:00
const workspace = await loadWorkspace(process.cwd())
const config = await loadChangelogConfig(process.cwd(), {})
2023-04-20 09:40:07 +00:00
const commits = await getLatestCommits().then(commits => commits.filter(
c => config.types[c.type] && !(c.type === 'chore' && c.scope === 'deps' && !c.isBreaking)
))
2023-04-20 09:40:07 +00:00
const bumpType = await determineBumpType()
const newVersion = inc(workspace.find('nuxt').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) {
2023-04-20 09:42:20 +00:00
execSync('git config --global user.email "daniel@roe.dev"')
execSync('git config --global user.name "Daniel Roe"')
2023-04-20 09:40:07 +00:00
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/nuxt/pulls?head=nuxt:v${newVersion}`)
const contributors = await getContributors()
2023-04-20 09:40:07 +00:00
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')
2023-04-20 09:40:07 +00:00
].join('\n')
// Create a PR with release notes if none exists
if (!currentPR) {
return await $fetch('https://api.github.com/repos/nuxt/nuxt/pulls', {
method: 'POST',
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`
},
body: {
title: `v${newVersion}`,
head: `v${newVersion}`,
2023-09-04 08:16:51 +00:00
base: releaseBranch,
2023-04-20 09:40:07 +00:00
body: releaseNotes,
draft: true
}
})
}
// Update release notes if the pull request does exist
await $fetch(`https://api.github.com/repos/nuxt/nuxt/pulls/${currentPR.number}`, {
method: 'PATCH',
headers: {
Authorization: `token ${process.env.GITHUB_TOKEN}`
},
body: {
body: releaseNotes
}
})
}
main().catch((err) => {
consola.error(err)
2023-04-20 09:40:07 +00:00
process.exit(1)
})