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
48 changes: 25 additions & 23 deletions .github/scripts/transform-message.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,8 @@ const ACTION_TEXT_MAP: Record<string, string> = {
review_requested: "请求审核",
};

const getActionText = (action?: string) => {
if (!action) return "编辑";
return ACTION_TEXT_MAP[action] || action;
};
const getActionText = (action?: string) =>
action ? ACTION_TEXT_MAP[action] || action : "编辑";

const createLink = (href: string, text = href) => `[${text}](${href})`;

Expand Down Expand Up @@ -74,12 +72,17 @@ const sanitizeMarkdown = (text: string): string =>
const createContentItem = (label: string, value?: string) =>
`**${label}** ${value ? sanitizeMarkdown(value) : "无"}`;

interface LarkMessageElement {
tag: string;
content: string | [object, object][];
}

type EventHandler = (
event: GitHubAction,
actionText: string,
actionText: string
) => {
title: string;
elements: any[];
elements: LarkMessageElement[];
};

// Event handlers
Expand All @@ -92,10 +95,10 @@ const eventHandlers: Record<string, EventHandler> = {
repository,
actor,
}) => {
const commitUrl = head_commit?.url ||
`${server_url}/${repository}/tree/${ref_name}`;
const commitMessage = head_commit?.message ||
"Create/Delete/Update Branch (No head commit)";
const commitUrl =
head_commit?.url || `${server_url}/${repository}/tree/${ref_name}`;
const commitMessage =
head_commit?.message || "Create/Delete/Update Branch (No head commit)";

return {
title: "GitHub 代码提交",
Expand All @@ -108,12 +111,12 @@ const eventHandlers: Record<string, EventHandler> = {
"代码分支:",
createLink(
`${server_url}/${repository}/tree/${ref_name}`,
ref_name,
),
ref_name
)
),
createContentItem(
"提交作者:",
createLink(`${server_url}/${actor}`, actor),
createLink(`${server_url}/${actor}`, actor)
),
createContentItem("提交信息:", commitMessage),
].join("\n"),
Expand All @@ -132,11 +135,11 @@ const eventHandlers: Record<string, EventHandler> = {
createContentItem("作者:", createUserLink(issue!.user!)),
createContentItem(
"指派:",
issue?.assignee ? createUserLink(issue.assignee) : "无",
issue?.assignee ? createUserLink(issue.assignee) : "无"
),
createContentItem(
"标签:",
issue?.labels?.map(({ name }) => name).join(", ") || "无",
issue?.labels?.map(({ name }) => name).join(", ") || "无"
),
createContentItem("里程碑:", issue?.milestone?.title || "无"),
createContentItem("描述:", issue?.body || "无"),
Expand All @@ -157,11 +160,11 @@ const eventHandlers: Record<string, EventHandler> = {
"指派:",
pull_request?.assignee
? createUserLink(pull_request.assignee)
: "无",
: "无"
),
createContentItem(
"标签:",
pull_request?.labels?.map(({ name }) => name).join(", ") || "无",
pull_request?.labels?.map(({ name }) => name).join(", ") || "无"
),
createContentItem("里程碑:", pull_request?.milestone?.title || "无"),
createContentItem("描述:", pull_request?.body || "无"),
Expand All @@ -179,7 +182,7 @@ const eventHandlers: Record<string, EventHandler> = {
createContentItem("链接:", createLink(discussion!.html_url)),
createContentItem(
"作者:",
createUserLink(discussion!.user as GitHubUser),
createUserLink(discussion!.user as GitHubUser)
),
createContentItem("描述:", discussion?.body || "无"),
].join("\n"),
Expand Down Expand Up @@ -239,7 +242,7 @@ const eventHandlers: Record<string, EventHandler> = {
createContentItem("作者:", createUserLink(comment!.user!)),
createContentItem(
"PR:",
createLink(pull_request!.html_url, `#${pull_request!.number}`),
createLink(pull_request!.html_url, `#${pull_request!.number}`)
),
createContentItem("评论:", comment?.body || "无"),
].join("\n"),
Expand All @@ -261,7 +264,7 @@ const processEvent = (event: GitHubAction) => {
} catch (cause) {
throw new Error(
`Error processing ${event_name} event: ${(cause as Error).message}`,
{ cause },
{ cause }
);
}
};
Expand All @@ -270,11 +273,10 @@ const processEvent = (event: GitHubAction) => {
const event = JSON.parse((await stdin()) || "{}") as GitHubAction;
const result = processEvent(event);

if (!result) {
if (!result)
throw new Error(
`Unsupported ${event.event_name} event & ${event.action} action`,
`Unsupported ${event.event_name} event & ${event.action} action`
);
}

const card = {
schema: "2.0",
Expand Down
8 changes: 3 additions & 5 deletions .github/workflows/Lark-notification.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,10 @@ jobs:

- name: Event Message serialization
id: message
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: |
YAML=$(
cat <<'JSON' | deno run --allow-all .github/scripts/transform-message.ts
${{ toJSON(github) }}
JSON
)
YAML=$(printf '%s' "$GITHUB_CONTEXT" | deno run --allow-all .github/scripts/transform-message.ts)
{
echo 'content<<EOF'
echo "$YAML"
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@ jobs:
build: # make sure build/ci work properly
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v7
- run: |
npm install
npm run all
test: # make sure the action works on a clean machine without building
if: ${{ github.event_name == 'workflow_dispatch' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: actions/checkout@v7
- uses: ./
with:
url: ${{ secrets.FEISHU_BOT_WEBHOOK_URL }}
Expand Down
37 changes: 32 additions & 5 deletions __tests__/main.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,35 @@
import * as process from 'process'
import * as cp from 'child_process'
import * as fs from 'fs'
import * as path from 'path'
import yaml from 'js-yaml'

// shows how the runner will run a javascript action with env / stdout protocol
test('test runs', () => {
console.log('just test')
type ActionMetadata = {
runs: {
using: string
steps: {
uses?: string
with?: {'node-version'?: string}
shell?: string
run?: string
}[]
}
}

test('action uses setup-node lts in composite wrapper', () => {
const actionPath = path.join(__dirname, '..', 'action.yml')
const action = yaml.load(fs.readFileSync(actionPath, 'utf8'), {
schema: yaml.FAILSAFE_SCHEMA
}) as ActionMetadata

expect(action.runs.using).toBe('composite')
expect(action.runs.steps[0]).toMatchObject({
uses: 'actions/setup-node@v7',
with: {
'node-version': 'lts/*'
}
})
expect(action.runs.steps[1]).toMatchObject({
shell:
"${{ runner.os == 'Windows' && 'pwsh' || ((runner.os == 'Linux' || runner.os == 'macOS') && 'bash') }}",
run: 'node dist/index.js'
})
Comment on lines +23 to +34
})
14 changes: 12 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,18 @@ inputs:
See: https://open.feishu.cn/document/feishu-cards/quick-start/send-message-cards-with-custom-bot

runs:
using: 'node20'
main: 'dist/index.js'
using: 'composite'
steps:
- uses: actions/setup-node@v7
with:
node-version: 'lts/*'
Comment thread
TechQuery marked this conversation as resolved.
Resolved
- shell: ${{ runner.os == 'Windows' && 'pwsh' || ((runner.os == 'Linux' || runner.os == 'macOS') && 'bash') }}
working-directory: ${{ github.action_path }}
env:
INPUT_URL: ${{ inputs.url }}
INPUT_MSG_TYPE: ${{ inputs.msg_type }}
INPUT_CONTENT: ${{ inputs.content }}
run: node dist/index.js
Comment on lines +32 to +38

branding:
color: 'blue'
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"devDependencies": {
"@types/jest": "^29.5.14",
"@types/js-yaml": "^4.0.9",
"@types/node": "^20.17.9",
"@types/node": "^24.13.3",
"@typescript-eslint/eslint-plugin": "^8.17.0",
"@typescript-eslint/parser": "^8.17.0",
"@vercel/ncc": "^0.38.3",
Expand Down
20 changes: 10 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -808,12 +808,12 @@
dependencies:
undici-types "~7.16.0"

"@types/node@^20.17.9":
version "20.19.25"
resolved "https://registry.yarnpkg.com/@types/node/-/node-20.19.25.tgz#467da94a2fd966b57cc39c357247d68047611190"
integrity sha512-ZsJzA5thDQMSQO788d7IocwwQbI8B5OPzmqNvpf3NY/+MHDAS759Wo0gd2WQeXYt5AAAQjzcrTVC6SKCuYgoCQ==
"@types/node@^24.13.3":
version "24.13.3"
resolved "https://registry.yarnpkg.com/@types/node/-/node-24.13.3.tgz#49f18bd3c647866dcda51a0756c145e14590ce16"
integrity sha512-Dh8vAsV36ig5wa9OX4pXvMc9D3Veibfw2wix0CUwYODLD8nkj9UsLjASr49nPg+2eKzxhBV+v7L8pXvT4e639Q==
dependencies:
undici-types "~6.21.0"
undici-types "~7.18.0"

"@types/responselike@^1.0.0":
version "1.0.3"
Expand Down Expand Up @@ -4073,16 +4073,16 @@ unbox-primitive@^1.1.0:
has-symbols "^1.1.0"
which-boxed-primitive "^1.1.1"

undici-types@~6.21.0:
version "6.21.0"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-6.21.0.tgz#691d00af3909be93a7faa13be61b3a5b50ef12cb"
integrity sha512-iwDZqg0QAGrg9Rav5H4n0M64c3mkR59cJ6wQp+7C4nI0gsmExaedaYLNO44eT4AtBBwjbTiGPMlt2Md0T9H9JQ==

undici-types@~7.16.0:
version "7.16.0"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-7.16.0.tgz#ffccdff36aea4884cbfce9a750a0580224f58a46"
integrity sha512-Zz+aZWSj8LE6zoxD+xrjh4VfkIG8Ya6LvYkZqtUQGJPZjYl53ypCaUwWqo7eI0x66KBGeRo+mlBEkMSeSZ38Nw==

undici-types@~7.18.0:
version "7.18.2"
resolved "https://registry.yarnpkg.com/undici-types/-/undici-types-7.18.2.tgz#29357a89e7b7ca4aef3bf0fd3fd0cd73884229e9"
integrity sha512-AsuCzffGHJybSaRrmr5eHr81mwJU3kjw6M+uprWvCXiNeN9SOGwQ3Jn8jb8m3Z6izVgknn1R0FTCEAP2QrLY/w==

undici@^5.25.4:
version "5.29.0"
resolved "https://registry.yarnpkg.com/undici/-/undici-5.29.0.tgz#419595449ae3f2cdcba3580a2e8903399bd1f5a3"
Expand Down