Skip to content

Commit 67517dd

Browse files
committed
chore: Add changelog auto generation
1 parent 6ed2c95 commit 67517dd

File tree

2 files changed

+120
-0
lines changed

2 files changed

+120
-0
lines changed

.github/workflows/changelog.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
name: changelog
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
permissions:
9+
pull-requests: write
10+
contents: write
11+
12+
concurrency:
13+
group: ${{ github.workflow }}-${{ github.event.number || github.sha }}
14+
cancel-in-progress: ${{ github.event_name != 'push' }}
15+
16+
jobs:
17+
update:
18+
if: github.repository_owner == 'nuxt' && !contains(github.event.head_commit.message, 'v1.')
19+
runs-on: ubuntu-latest
20+
21+
steps:
22+
- uses: actions/checkout@0ad4b8fadaa221de15dcec353f45205ec38ea70b # v4.1.4
23+
with:
24+
fetch-depth: 0
25+
- run: corepack enable
26+
- uses: actions/setup-node@60edb5dd545a775178f52524783378180af0d1f8 # v4.0.2
27+
with:
28+
node-version: 20
29+
cache: "pnpm"
30+
31+
- name: Install dependencies
32+
run: pnpm install
33+
34+
- run: pnpm jiti ./scripts/update-changelog.ts
35+
env:
36+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

scripts/update-changelog.ts

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
import { execSync } from 'node:child_process'
2+
import { $fetch } from 'ofetch'
3+
import { inc } from 'semver'
4+
import { generateMarkDown, getCurrentGitBranch, loadChangelogConfig } from 'changelogen'
5+
import { consola } from 'consola'
6+
import { determineBumpType, getContributors, getLatestCommits, loadWorkspace } from './_utils'
7+
8+
async function main () {
9+
const releaseBranch = await getCurrentGitBranch()
10+
const workspace = await loadWorkspace(process.cwd())
11+
const config = await loadChangelogConfig(process.cwd(), {})
12+
13+
const commits = await getLatestCommits().then(commits => commits.filter(
14+
c => config.types[c.type] && !(c.type === 'chore' && c.scope === 'deps' && !c.isBreaking),
15+
))
16+
const bumpType = await determineBumpType()
17+
18+
const newVersion = inc(workspace.find('nuxt').data.version, bumpType || 'patch')
19+
const changelog = await generateMarkDown(commits, config)
20+
21+
// Create and push a branch with bumped versions if it has not already been created
22+
const branchExists = execSync(`git ls-remote --heads origin v${newVersion}`).toString().trim().length > 0
23+
if (!branchExists) {
24+
execSync('git config --global user.email "gregor@codedredd.de"')
25+
execSync('git config --global user.name "Gregor Becker"')
26+
execSync(`git checkout -b v${newVersion}`)
27+
28+
for (const pkg of workspace.packages.filter(p => !p.data.private)) {
29+
workspace.setVersion(pkg.data.name, newVersion!)
30+
}
31+
await workspace.save()
32+
33+
execSync(`git commit -am v${newVersion}`)
34+
execSync(`git push -u origin v${newVersion}`)
35+
}
36+
37+
// Get the current PR for this release, if it exists
38+
const [currentPR] = await $fetch(`https://api.github.com/repos/CodeDredd/pinia-orm/pulls?head=nuxt:v${newVersion}`)
39+
const contributors = await getContributors()
40+
41+
const releaseNotes = [
42+
currentPR?.body.replace(/## 👉 Changelog[\s\S]*$/, '') || `> ${newVersion} is the next ${bumpType} release.\n>\n> **Timetable**: to be announced.`,
43+
'## 👉 Changelog',
44+
changelog
45+
.replace(/^## v.*?\n/, '')
46+
.replace(`...${releaseBranch}`, `...v${newVersion}`)
47+
.replace(/### Contributors[\s\S]*$/, ''),
48+
'### ❤️ Contributors',
49+
contributors.map(c => `- ${c.name} (@${c.username})`).join('\n'),
50+
].join('\n')
51+
52+
// Create a PR with release notes if none exists
53+
if (!currentPR) {
54+
return await $fetch('https://api.github.com/repos/CodeDredd/pinia-orm/pulls', {
55+
method: 'POST',
56+
headers: {
57+
Authorization: `token ${process.env.GITHUB_TOKEN}`,
58+
},
59+
body: {
60+
title: `v${newVersion}`,
61+
head: `v${newVersion}`,
62+
base: releaseBranch,
63+
body: releaseNotes,
64+
draft: true,
65+
},
66+
})
67+
}
68+
69+
// Update release notes if the pull request does exist
70+
await $fetch(`https://api.github.com/repos/CodeDredd/pinia-orm/pulls/${currentPR.number}`, {
71+
method: 'PATCH',
72+
headers: {
73+
Authorization: `token ${process.env.GITHUB_TOKEN}`,
74+
},
75+
body: {
76+
body: releaseNotes,
77+
},
78+
})
79+
}
80+
81+
main().catch((err) => {
82+
consola.error(err)
83+
process.exit(1)
84+
})

0 commit comments

Comments
 (0)