ci: send slack message to channel after deploy (#7889)

This commit is contained in:
Brooooooklyn
2024-08-27 03:01:54 +00:00
parent e44a9483c2
commit 67c9c7bcc5
8 changed files with 710 additions and 33 deletions

View File

@@ -7,7 +7,7 @@
"description": "Generate changelog from blocksuite version change",
"dependencies": {
"@napi-rs/clipboard": "^1.1.2",
"@napi-rs/simple-git": "^0.1.16",
"@napi-rs/simple-git": "^0.1.18",
"chalk": "^5.3.0"
}
}

140
tools/changelog/index.js Normal file
View File

@@ -0,0 +1,140 @@
import { readFile } from 'node:fs/promises';
import { join } from 'node:path';
import { fileURLToPath } from 'node:url';
import { Repository, Sort } from '@napi-rs/simple-git';
import { WebClient } from '@slack/web-api';
import {
generateMarkdown,
parseCommits,
resolveAuthors,
resolveConfig,
} from 'changelogithub';
import { render } from './markdown.js';
const {
DEPLOYED_URL,
NAMESPACE,
CHANNEL_ID,
SLACK_BOT_TOKEN,
PREV_VERSION,
DEPLOYMENT,
FLAVOR,
BLOCKSUITE_REPO_PATH,
} = process.env;
const slack = new WebClient(SLACK_BOT_TOKEN);
const rootDir = join(fileURLToPath(import.meta.url), '..', '..', '..');
const repo = new Repository(rootDir);
/**
* @param {import('@napi-rs/simple-git').Repository} repo
* @param {string} previousCommit
* @param {string | undefined} currentCommit
* @returns {Promise<string>}
*/
async function getChangeLog(repo, previousCommit, currentCommit) {
const prevCommit = repo.findCommit(previousCommit);
if (!prevCommit) {
console.log(
`Previous commit ${previousCommit} in ${repo.path()} not found`
);
return '';
}
/** @type {typeof import('changelogithub')['parseCommits'] extends (commit: infer C, ...args: any[]) => any ? C : any} */
const commits = [];
const revWalk = repo.revWalk();
if (currentCommit) {
const commit = repo.findCommit(currentCommit);
revWalk.push(commit.id());
} else {
revWalk.pushHead();
}
for (const commitId of revWalk.setSorting(Sort.Time & Sort.Topological)) {
const commit = repo.findCommit(commitId);
commits.push({
message: commit.message(),
body: commit.body() ?? '',
shortHash: commit.id().substring(0, 8),
author: {
name: commit.author().name(),
email: commit.author().email(),
},
});
if (commitId.startsWith(previousCommit)) {
break;
}
}
const parseConfig = await resolveConfig({
token: process.env.GITHUB_TOKEN,
});
const parsedCommits = parseCommits(commits, parseConfig);
await resolveAuthors(parsedCommits, parseConfig);
return generateMarkdown(parsedCommits, parseConfig)
.replaceAll('&nbsp;', ' ')
.replaceAll('<samp>', '')
.replaceAll('</samp>', '');
}
let blockSuiteChangelog = '';
const pkgJsonPath = 'packages/frontend/core/package.json';
const content = await readFile(join(rootDir, pkgJsonPath), 'utf8');
const { dependencies } = JSON.parse(content);
const blocksuiteVersion = dependencies['@blocksuite/block-std'];
const previousPkgJsonBlob = repo
.findCommit(PREV_VERSION)
.tree()
.getPath(pkgJsonPath)
.toObject(repo)
.peelToBlob();
const previousPkgJson = JSON.parse(
Buffer.from(previousPkgJsonBlob.content()).toString('utf8')
);
const previousBlocksuiteVersion =
previousPkgJson.dependencies['@blocksuite/block-std'];
if (blocksuiteVersion !== previousBlocksuiteVersion) {
const current = blocksuiteVersion.split('-').pop();
const previous = previousBlocksuiteVersion.split('-').pop();
const blockSuiteRepo = new Repository(
BLOCKSUITE_REPO_PATH ?? join(rootDir, '..', 'blocksuite')
);
console.log(`Blocksuite ${previous} -> ${current}`);
blockSuiteChangelog = await getChangeLog(blockSuiteRepo, previous, current);
}
const messageHead =
DEPLOYMENT === 'server'
? `# Server deployed in ${NAMESPACE}
- [${DEPLOYED_URL}](${DEPLOYED_URL})
`
: `# AFFiNE Client ${FLAVOR} released`;
let changelogMessage = `${messageHead}
${await getChangeLog(repo, PREV_VERSION)}
`;
if (blockSuiteChangelog) {
changelogMessage += `
# Blocksuite Changelog
${blockSuiteChangelog}`;
}
const { ok } = await slack.chat.postMessage({
channel: CHANNEL_ID,
text: `Server deployed`,
blocks: render(changelogMessage),
});
console.assert(ok, 'Failed to send a message to Slack');

View File

@@ -0,0 +1,25 @@
import { jsxslack } from 'jsx-slack';
import { marked, Renderer } from 'marked';
export const render = markdown => {
const rendered = marked(markdown, {
renderer: new (class CustomRenderer extends Renderer {
heading({ tokens }) {
return `
<Fragment>
<Section><b>${tokens[0].text}</b></Section>
<Divider />
</Fragment>`;
}
list(token) {
return `<Section>${super.list(token)}</Section>`;
}
hr() {
return `<Divider />`;
}
})(),
});
return jsxslack([`<Blocks>${rendered}</Blocks>`]);
};

View File

@@ -0,0 +1,19 @@
{
"name": "@affine/changelog",
"version": "0.16.0",
"type": "module",
"main": "index.js",
"private": true,
"description": "Generate changelog from version changes",
"dependencies": {
"@napi-rs/simple-git": "^0.1.18",
"@slack/web-api": "^7.3.4",
"chalk": "^5.3.0",
"changelogithub": "^0.13.9",
"jsx-slack": "^6.1.1",
"marked": "^14.0.0"
},
"devDependencies": {
"@types/node": "^20.14.12"
}
}