Skip to content

Commit 14285c8

Browse files
author
chanzachary
committed
新增飞书机器人通知工作流,支持推送、拉取请求、分支创建和删除事件的通知功能。
1 parent 24e78cc commit 14285c8

File tree

1 file changed

+126
-0
lines changed

1 file changed

+126
-0
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
name: 飞书机器人通知
2+
3+
on:
4+
push:
5+
branches: [ main, master, develop, feature/*, hotfix/*, release/* ]
6+
pull_request:
7+
branches: [ main, master, develop ]
8+
create:
9+
delete:
10+
fork:
11+
gollum:
12+
issues:
13+
issue_comment:
14+
watch:
15+
release:
16+
deployment:
17+
deployment_status:
18+
public:
19+
repository_dispatch:
20+
21+
jobs:
22+
notify-feishu:
23+
runs-on: ubuntu-latest
24+
steps:
25+
- name: 获取事件信息
26+
id: event_info
27+
run: |
28+
echo "event_name=${{ github.event_name }}" >> $GITHUB_OUTPUT
29+
echo "ref=${{ github.ref }}" >> $GITHUB_OUTPUT
30+
echo "ref_name=${{ github.ref_name }}" >> $GITHUB_OUTPUT
31+
echo "sha=${{ github.sha }}" >> $GITHUB_OUTPUT
32+
echo "repository=${{ github.repository }}" >> $GITHUB_OUTPUT
33+
echo "actor=${{ github.actor }}" >> $GITHUB_OUTPUT
34+
echo "workflow=${{ github.workflow }}" >> $GITHUB_OUTPUT
35+
36+
# 获取提交信息 - 使用GitHub环境变量
37+
if [ "${{ github.event_name }}" = "push" ]; then
38+
echo "commit_message=${{ github.event.head_commit.message }}" >> $GITHUB_OUTPUT
39+
echo "commit_author=${{ github.event.head_commit.author.name }}" >> $GITHUB_OUTPUT
40+
echo "commit_date=${{ github.event.head_commit.timestamp }}" >> $GITHUB_OUTPUT
41+
fi
42+
43+
# 获取PR信息
44+
if [ "${{ github.event_name }}" = "pull_request" ]; then
45+
echo "pr_title=${{ github.event.pull_request.title }}" >> $GITHUB_OUTPUT
46+
echo "pr_body=${{ github.event.pull_request.body }}" >> $GITHUB_OUTPUT
47+
echo "pr_author=${{ github.event.pull_request.user.login }}" >> $GITHUB_OUTPUT
48+
fi
49+
50+
- name: 发送飞书通知
51+
run: |
52+
# 构建消息内容
53+
PROJECT_NAME="design-server"
54+
EVENT_NAME="${{ steps.event_info.outputs.event_name }}"
55+
BRANCH="${{ steps.event_info.outputs.ref_name }}"
56+
REPOSITORY="${{ steps.event_info.outputs.repository }}"
57+
ACTOR="${{ steps.event_info.outputs.actor }}"
58+
SHA="${{ steps.event_info.outputs.sha }}"
59+
60+
# 根据事件类型构建不同的消息
61+
case $EVENT_NAME in
62+
"push")
63+
COMMIT_MSG="${{ steps.event_info.outputs.commit_message }}"
64+
COMMIT_AUTHOR="${{ steps.event_info.outputs.commit_author }}"
65+
COMMIT_DATE="${{ steps.event_info.outputs.commit_date }}"
66+
67+
# 如果提交信息为空,使用备用方案
68+
if [ -z "$COMMIT_MSG" ]; then
69+
COMMIT_MSG="无提交信息"
70+
fi
71+
if [ -z "$COMMIT_AUTHOR" ]; then
72+
COMMIT_AUTHOR="$ACTOR"
73+
fi
74+
if [ -z "$COMMIT_DATE" ]; then
75+
COMMIT_DATE="未知时间"
76+
fi
77+
78+
MESSAGE="🚀 代码推送通知\n\n📁 项目: $PROJECT_NAME\n🌿 分支: $BRANCH\n👤 提交者: $ACTOR\n📝 提交信息: $COMMIT_MSG\n👨‍💻 作者: $COMMIT_AUTHOR\n📅 提交时间: $COMMIT_DATE\n🔗 仓库: https://github.com/$REPOSITORY\n🔗 提交链接: https://github.com/$REPOSITORY/commit/$SHA"
79+
;;
80+
81+
"pull_request")
82+
PR_TITLE="${{ steps.event_info.outputs.pr_title }}"
83+
PR_BODY="${{ steps.event_info.outputs.pr_body }}"
84+
PR_AUTHOR="${{ steps.event_info.outputs.pr_author }}"
85+
86+
MESSAGE="🔀 Pull Request 通知\n\n📁 项目: $PROJECT_NAME\n🌿 分支: $BRANCH\n👤 创建者: $PR_AUTHOR\n📝 PR标题: $PR_TITLE\n📄 PR描述: $PR_BODY\n🔗 仓库: https://github.com/$REPOSITORY\n🔗 PR链接: ${{ github.event.pull_request.html_url }}"
87+
;;
88+
89+
"create")
90+
MESSAGE="✨ 分支创建通知\n\n📁 项目: $PROJECT_NAME\n🌿 分支: $BRANCH\n👤 创建者: $ACTOR\n🔗 仓库: https://github.com/$REPOSITORY"
91+
;;
92+
93+
"delete")
94+
MESSAGE="🗑️ 分支删除通知\n\n📁 项目: $PROJECT_NAME\n🌿 分支: $BRANCH\n👤 删除者: $ACTOR\n🔗 仓库: https://github.com/$REPOSITORY"
95+
;;
96+
97+
*)
98+
MESSAGE="📢 Git事件通知\n\n📁 项目: $PROJECT_NAME\n🌿 分支: $BRANCH\n👤 操作者: $ACTOR\n📋 事件类型: $EVENT_NAME\n🔗 仓库: https://github.com/$REPOSITORY"
99+
;;
100+
esac
101+
102+
# 发送到飞书机器人 - 使用text格式
103+
RESPONSE=$(curl -s -w "%{http_code}" -X POST \
104+
-H "Content-Type: application/json" \
105+
-d "{
106+
\"msg_type\": \"text\",
107+
\"content\": {
108+
\"text\": \"$MESSAGE\"
109+
}
110+
}" \
111+
"https://open.feishu.cn/open-apis/bot/v2/hook/143ed889-eace-42df-ba97-d8b2bac28e6d")
112+
113+
# 检查响应
114+
HTTP_CODE="${RESPONSE: -3}"
115+
RESPONSE_BODY="${RESPONSE%???}"
116+
117+
echo "HTTP Code: $HTTP_CODE"
118+
echo "Response: $RESPONSE_BODY"
119+
120+
if [ "$HTTP_CODE" != "200" ]; then
121+
echo "❌ 飞书通知发送失败,HTTP状态码: $HTTP_CODE"
122+
echo "响应内容: $RESPONSE_BODY"
123+
exit 1
124+
else
125+
echo "✅ 飞书通知发送成功"
126+
fi

0 commit comments

Comments
 (0)