1
1
name : PR Format Notification
2
2
on :
3
3
pull_request_target :
4
- types : [opened, synchronize ]
4
+ types : [opened]
5
5
6
6
permissions :
7
7
pull-requests : write
@@ -42,11 +42,12 @@ jobs:
42
42
# 构建工作流链接
43
43
branch="${{ github.event.pull_request.head.ref }}"
44
44
fork_repo="${{ github.event.pull_request.head.repo.full_name }}"
45
- workflow_url="https://github.com/${fork_repo}/actions/workflows/clang-format .yml"
45
+ workflow_url="https://github.com/${fork_repo}/actions/workflows/pr_clang_format .yml"
46
46
direct_link="${workflow_url}?branch=${branch}"
47
47
48
48
# 使用数组存储多行消息
49
49
message_lines=(
50
+ "<!-- PR Format Notification Comment -->"
50
51
"**👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!**"
51
52
""
52
53
"为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流。"
63
64
"- 设置需排除的文件/目录(目录请以\"/\"结尾)"
64
65
"Set files/directories to exclude (directories should end with \"/\")"
65
66
"- 将目标分支设置为 \ Set the target branch to:**\`${branch}\`**"
66
- "- 设置PR number为 \ Set the PR number to:**\`${{ github.event.number }}\`**"
67
+ "- 设置PR number为 \ Set the PR number to:**\`${{ github.event.pull_request. number }}\`**"
67
68
""
68
69
"3. **等待工作流完成 | Wait for the workflow to complete**"
69
70
"格式化后的代码将自动推送至你的分支。"
@@ -82,26 +83,46 @@ jobs:
82
83
echo "Message content:"
83
84
echo "$message"
84
85
86
+ # 查找现有的 bot 评论
87
+ existing_comment=$(curl -s \
88
+ -H "Accept: application/vnd.github.v3+json" \
89
+ -H "Authorization: Bearer $GITHUB_TOKEN" \
90
+ "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" | \
91
+ jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- PR Format Notification Comment -->"))) | {id: .id, body: .body} | @base64')
92
+
85
93
# 使用 jq 安全地构建 JSON 负载
86
94
json_payload=$(jq -n --arg body "$message" '{"body": $body}')
87
95
88
- # 发送评论到 PR
89
- response=$(curl -s -w "\n%{http_code}" \
90
- -X POST \
91
- -H "Accept: application/vnd.github.v3+json" \
92
- -H "Authorization: Bearer $GITHUB_TOKEN" \
93
- "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
94
- -d "$json_payload")
96
+ if [[ -n "$existing_comment" ]]; then
97
+ # 更新现有评论
98
+ comment_id=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .id)
99
+ echo "Updating existing comment $comment_id"
100
+ response=$(curl -s -w "\n%{http_code}" \
101
+ -X PATCH \
102
+ -H "Accept: application/vnd.github.v3+json" \
103
+ -H "Authorization: Bearer $GITHUB_TOKEN" \
104
+ -d "$json_payload" \
105
+ "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id")
106
+ else
107
+ # 创建新评论
108
+ echo "Creating new comment"
109
+ response=$(curl -s -w "\n%{http_code}" \
110
+ -X POST \
111
+ -H "Accept: application/vnd.github.v3+json" \
112
+ -H "Authorization: Bearer $GITHUB_TOKEN" \
113
+ -d "$json_payload" \
114
+ "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments")
115
+ fi
95
116
96
117
# 提取 HTTP 状态码和响应体
97
118
http_code=$(echo "$response" | tail -n1)
98
119
response_body=$(echo "$response" | sed '$d')
99
120
100
- if [ "$http_code" -eq 201 ]; then
101
- echo "Format notification comment added successfully"
121
+ if [ "$http_code" -eq 201 ] || [ "$http_code" -eq 200 ] ; then
122
+ echo "Format notification comment added/updated successfully"
102
123
echo "Comment URL: $(echo "$response_body" | jq -r '.html_url')"
103
124
else
104
- echo "Failed to add comment. HTTP status: $http_code"
125
+ echo "Failed to add/update comment. HTTP status: $http_code"
105
126
echo "Response: $response_body"
106
127
exit 1
107
128
fi
0 commit comments