Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .github/scripts/deno.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"nodeModulesDir": "none"
}
155 changes: 155 additions & 0 deletions .github/scripts/transform-message.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
import { components } from 'npm:@octokit/openapi-types';
import { stdin } from 'npm:zx';

type GitHubSchema = components['schemas'];

type GitHubUser = GitHubSchema['simple-user'];

interface GitHubAction
extends Record<'event_name' | 'actor' | 'server_url' | 'repository', string> {
action?: string;
ref?: string;
ref_name?: string;
event: {
head_commit?: GitHubSchema['git-commit'];
issue?: GitHubSchema['webhook-issues-opened']['issue'];
pull_request?: GitHubSchema['pull-request'];
discussion?: GitHubSchema['discussion'];
comment?: GitHubSchema['issue-comment'];
release?: GitHubSchema['release'];
};
}

// Helper functions
const getActionText = (action?: string) =>
action === 'closed' ? '关闭' : action?.includes('open') ? '打开' : '编辑';

const createLink = (href: string, text = href) => ({ tag: 'a', href, text });

const createText = (text: string) => ({ tag: 'text', text });

// create user link
const createUserLink = (user: GitHubUser) =>
user ? createLink(user.html_url, user.login) : createText('无');

const createContentItem = (label: string, value?: string | { tag: string; text: string }) =>
[
createText(label),
typeof value === 'string' ? createText(value || '无') : value || createText('无'),
] as [object, object];

type EventHandler = (
event: GitHubAction,
actionText: string,
) => {
title: string;
content: [object, object][];
};

// Event handlers
const eventHandlers: Record<string, EventHandler> = {
push: ({ event: { head_commit }, ref, ref_name, server_url, repository, actor }) => ({
title: 'GitHub 代码提交',
content: [
[createText('提交链接:'), createLink(head_commit!.url)],
[createText('代码分支:'), createLink(`${server_url}/${repository}/tree/${ref_name}`, ref)],
[createText('提交作者:'), createLink(`${server_url}/${actor}`, actor)],
[createText('提交信息:'), createText(head_commit!.message)],
],
}),

issues: ({ event: { issue } }, actionText) => ({
title: `GitHub issue ${actionText}:${issue?.title}`,
content: [
[createText('链接:'), createLink(issue!.html_url)],
[createText('作者:'), createLink(issue!.user!.html_url!, issue!.user!.login)],
[
createText('指派:'),
issue?.assignee
? createLink(issue.assignee.html_url!, issue.assignee.login)
: createText('无'),
],
[createText('标签:'), createText(issue?.labels?.map(({ name }) => name).join(', ') || '无')],
[createText('里程碑:'), createText(issue?.milestone?.title || '无')],
[createText('描述:'), createText(issue?.body || '无')],
],
}),

pull_request: ({ event: { pull_request } }, actionText) => ({
title: `GitHub PR ${actionText}:${pull_request?.title}`,
content: [
[createText('链接:'), createLink(pull_request!.html_url)],
[createText('作者:'), createLink(pull_request!.user.html_url, pull_request!.user.login)],
[
createText('指派:'),
pull_request?.assignee
? createLink(pull_request.assignee.html_url, pull_request.assignee.login)
: createText('无'),
],
[
createText('标签:'),
createText(pull_request?.labels?.map(({ name }) => name).join(', ') || '无'),
],
[createText('里程碑:'), createText(pull_request?.milestone?.title || '无')],
[createText('描述:'), createText(pull_request?.body || '无')],
],
}),

discussion: ({ event: { discussion } }, actionText) => ({
title: `GitHub 讨论 ${actionText}:${discussion?.title || '无'}`,
content: [
createContentItem('链接:', discussion?.html_url),
createContentItem('作者:', createUserLink(discussion!.user as GitHubUser)),
createContentItem('描述:', discussion?.body || '无'),
],
}),

issue_comment: ({ event: { comment, issue } }) => ({
title: `GitHub issue 评论:${issue?.title || '未知 issue'}`,
content: [
createContentItem('链接:', comment?.html_url),
createContentItem('作者:', createUserLink(comment!.user!)),
createContentItem('描述:', comment?.body || '无'),
],
}),

discussion_comment: ({ event: { comment, discussion } }) => ({
title: `GitHub 讨论评论:${discussion?.title || '无'}`,
content: [
createContentItem('链接:', comment?.html_url),
createContentItem('作者:', createUserLink(comment!.user!)),
createContentItem('描述:', comment?.body || '无'),
],
}),

release: ({ event: { release } }) => ({
title: `GitHub Release 发布:${release!.name || release!.tag_name}`,
content: [
createContentItem('链接:', release!.html_url),
createContentItem('作者:', createUserLink(release!.author)),
createContentItem('描述:', release!.body!),
],
}),
};

// Main processor
const processEvent = (event: GitHubAction) => {
const { event_name, action } = event;
const actionText = getActionText(action);
const handler = eventHandlers[event_name];

if (!handler) throw new Error(`No handler found for event: ${event_name}`);

try {
return handler(event, actionText);
} catch (cause) {
throw new Error(`Error processing ${event_name} event: ${(cause as Error).message}`, { cause });
}
};

// Main execution:Processing GitHub Events and Outputting Results
const event = JSON.parse((await stdin()) || '{}') as GitHubAction;
const zh_cn = processEvent(event);

if (zh_cn) console.log(JSON.stringify({ post: { zh_cn } }));
else throw new Error(`Unsupported ${event.event_name} event & ${event.action} action`);
42 changes: 42 additions & 0 deletions .github/workflows/Lark-notification.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: Lark notification

# https://docs.github.com/en/actions/writing-workflows/choosing-when-your-workflow-runs/events-that-trigger-workflows
on:
push:
issues:
pull_request:
discussion:
issue_comment:
discussion_comment:
release:
types:
- published

jobs:
send-Lark-message:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- uses: denoland/setup-deno@v2
with:
deno-version: v2.x

- name: Event Message serialization
id: message
run: |
YAML=$(echo '${{ toJSON(github) }}' | deno --allow-all .github/scripts/transform-message.ts)
{
echo 'content<<EOF'
echo $YAML
echo 'EOF'
} >> $GITHUB_OUTPUT

- name: Send message to Lark
if: ${{ contains(steps.message.outputs.content, ':') }}
uses: foxundermoon/feishu-action@v2
with:
url: ${{ secrets.LARK_CHATBOT_HOOK_URL }}
msg_type: post
content: |
${{ steps.message.outputs.content }}
28 changes: 14 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,28 @@
"@mui/lab": "^7.0.0-beta.16",
"@mui/material": "^7.3.1",
"@mui/material-nextjs": "^7.3.0",
"@sentry/nextjs": "^10.5.0",
"@sentry/nextjs": "^10.8.0",
"file-type": "^21.0.0",
"jsonwebtoken": "^9.0.2",
"koa": "^3.0.1",
"koa-jwt": "^4.0.4",
"koajax": "^3.1.2",
"lodash": "^4.17.21",
"marked": "^16.2.0",
"marked": "^16.2.1",
"mime": "^4.0.7",
"mobx": "^6.13.7",
"mobx-github": "^0.3.11",
"mobx-github": "^0.4.0",
"mobx-i18n": "^0.7.1",
"mobx-lark": "^2.4.0",
"mobx-react": "^9.2.0",
"mobx-react-helper": "^0.5.1",
"mobx-restful": "^2.1.0",
"next": "^15.5.0",
"next": "^15.5.2",
"next-pwa": "~5.6.0",
"next-ssr-middleware": "^1.0.2",
"react": "^19.1.1",
"react-dom": "^19.1.1",
"web-utility": "^4.5.1",
"web-utility": "^4.5.3",
"webpack": "^5.101.3"
},
"devDependencies": {
Expand All @@ -47,8 +47,8 @@
"@babel/preset-react": "^7.27.1",
"@cspell/eslint-plugin": "^9.2.0",
"@eslint/compat": "^1.3.2",
"@eslint/js": "^9.33.0",
"@next/eslint-plugin-next": "^15.5.0",
"@eslint/js": "^9.34.0",
"@next/eslint-plugin-next": "^15.5.2",
"@stylistic/eslint-plugin": "^5.2.3",
"@tailwindcss/postcss": "^4.1.12",
"@tailwindcss/typography": "^0.5.16",
Expand All @@ -58,14 +58,14 @@
"@types/koa__router": "^12.0.4",
"@types/lodash": "^4.17.20",
"@types/next-pwa": "^5.6.9",
"@types/node": "^22.17.2",
"@types/react": "^19.1.11",
"eslint": "^9.33.0",
"eslint-config-next": "^15.5.0",
"@types/node": "^22.18.0",
"@types/react": "^19.1.12",
"eslint": "^9.34.0",
"eslint-config-next": "^15.5.2",
"eslint-config-prettier": "^10.1.8",
"eslint-plugin-react": "^7.37.5",
"eslint-plugin-simple-import-sort": "^12.1.1",
"get-git-folder": "^0.1.2",
"git-utility": "^0.2.0",
"globals": "^16.3.0",
"husky": "^9.1.7",
"jiti": "^2.5.1",
Expand All @@ -76,7 +76,7 @@
"prettier-plugin-tailwindcss": "^0.6.14",
"tailwindcss": "^4.1.12",
"typescript": "~5.9.2",
"typescript-eslint": "^8.40.0"
"typescript-eslint": "^8.41.0"
},
"resolutions": {
"next": "$next"
Expand Down Expand Up @@ -105,7 +105,7 @@
},
"scripts": {
"prepare": "husky || true",
"install": "get-git-folder https://github.com/idea2app/key-vault main idea2app.github.io || true",
"install": "xgit download https://github.com/idea2app/key-vault main idea2app.github.io || true",
"dev": "next dev",
"build": "next build",
"export": "next build && next export",
Expand Down
17 changes: 12 additions & 5 deletions pages/wiki/[node_token].tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Block, renderBlocks, WikiNode } from 'mobx-lark';
import { GetStaticPaths, GetStaticProps } from 'next';
import { FC } from 'react';
import { Minute, Second } from 'web-utility';

import { PageHead } from '../../components/PageHead';
import documentStore from '../../models/Document';
Expand All @@ -25,12 +26,18 @@ export const getStaticProps: GetStaticProps = async ({ params }) => {

if (node?.obj_type !== 'docx') return { notFound: true };

const blocks = await documentStore.getOneBlocks(
node.obj_token,
token => `/api/Lark/file/${token}`,
);
try {
const blocks = await documentStore.getOneBlocks(
node.obj_token,
token => `/api/Lark/file/${token}`,
);

return { props: { node, blocks } };
return { props: { node, blocks } };
} catch (error) {
console.error(error);

return { notFound: true, revalidate: Minute / Second };
}
};

interface WikiDocumentPageProps {
Expand Down
Loading