|
| 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