feat: generate blocksuite changelog (#5724)

This commit is contained in:
LongYinan
2024-01-29 07:28:02 +00:00
parent 25e8b8306f
commit 5bd339bed7
3 changed files with 395 additions and 0 deletions

View File

@@ -0,0 +1,146 @@
import { execSync } from 'node:child_process';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Clipboard } from '@napi-rs/clipboard';
import { Repository, Sort } from '@napi-rs/simple-git';
import corePackage from '../../packages/frontend/core/package.json' assert { type: 'json' };
const clipboard = new Clipboard();
const oldHash = corePackage.dependencies['@blocksuite/block-std']
.split('-')
.pop();
const info = await fetch(
'https://registry.npmjs.org/@blocksuite/block-std'
).then(res => res.json());
const latestVersion = info['dist-tags'].nightly;
const latestHash = latestVersion.split('-').pop();
console.info(`Upgrade blocksuite from ${oldHash} -> ${latestHash}`);
const blockSuiteDeps = execSync(`yarn info -A --name-only --json`, {
encoding: 'utf8',
});
const blocksuiteDepsList = blockSuiteDeps
.split('\n')
.map(s => s.trim())
.filter(Boolean)
.map(s => s.substring(1, s.length - 1))
.filter(
s => s.startsWith('@blocksuite') && !s.startsWith('@blocksuite/icons')
)
.map(s => s.split('@npm').at(0));
for (const pkg of blocksuiteDepsList) {
const command = `yarn up ${pkg}@${latestVersion}`;
console.info(`Executing ${command}`);
execSync(command, {
stdio: 'inherit',
});
}
console.info(`Upgrade complete`);
const repo = new Repository(
join(fileURLToPath(import.meta.url), '..', '..', '..', '..', 'BlockSuite')
);
repo.remote('origin').fetch(['master'], progress => {
if (progress.totalDeltas && progress.totalObjects) {
console.log(
`${(
(progress.receivedObjects / progress.totalObjects) * 50 +
(progress.indexedDeltas / progress.totalDeltas) * 50
).toFixed(2)}%`
);
}
});
const latest = repo.findCommit(latestHash);
const commits = {
Features: [],
Bugfix: [],
Refactor: [],
Misc: [],
};
for (const oid of repo
.revWalk()
.push(latest.id())
.setSorting(Sort.Time & Sort.Topological)) {
const commit = repo.findCommit(oid);
const summary = commit.summary();
if (summary.startsWith('feat')) {
commits.Features.push(commit);
} else if (summary.startsWith('fix')) {
commits.Bugfix.push(commit);
} else if (summary.startsWith('refactor')) {
commits.Refactor.push(commit);
} else {
commits.Misc.push(commit);
}
if (oid.startsWith(oldHash)) {
break;
}
}
clipboard.setText(await formatCommits(commits));
console.info(`Changelog copied to clipboard`);
async function formatCommits(commits) {
return `## Features
${await Promise.all(commits.Features.map(format)).then(commits =>
commits.join('\n')
)}
## Bugfix
${await Promise.all(commits.Bugfix.map(format)).then(commits =>
commits.join('\n')
)}
## Refactor
${await Promise.all(commits.Refactor.map(format)).then(commits =>
commits.join('\n')
)}
## Misc
${await Promise.all(commits.Misc.map(format)).then(commits =>
commits.join('\n')
)}
`;
/**
* @param {import('./index').Commit} commit
* @returns string
*/
async function format(commit) {
const summary = commit.summary();
const match = summary.match(/\(#(\d+)\)/);
if (match) {
const [_, pull] = match;
const pullInfo = await fetch(
`https://api.github.com/repos/toeverything/BlockSuite/pulls/${pull}`,
{
headers: {
Accept: 'application/vnd.github+json',
Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
'X-GitHub-Api-Version': '2022-11-28',
},
}
)
.then(res => res.json())
.catch(() => ({ user: {} }));
const {
user: { login },
} = pullInfo;
return `- https://github.com/toeverything/BlockSuite/pull/${pull} @${login}`;
}
return `- ${summary}`;
}
}

View File

@@ -0,0 +1,12 @@
{
"name": "@affine/bump-blocksuite",
"version": "0.0.0",
"type": "module",
"main": "index.js",
"private": true,
"description": "Generate changelog from blocksuite version change",
"dependencies": {
"@napi-rs/clipboard": "^1.1.1",
"@napi-rs/simple-git": "^0.1.11"
}
}