11name : PR Format Notification
22on :
33 pull_request_target :
4- types : [opened, synchronize ]
4+ types : [opened]
55
66permissions :
77 pull-requests : write
@@ -42,11 +42,12 @@ jobs:
4242 # 构建工作流链接
4343 branch="${{ github.event.pull_request.head.ref }}"
4444 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"
4646 direct_link="${workflow_url}?branch=${branch}"
4747
4848 # 使用数组存储多行消息
4949 message_lines=(
50+ "<!-- PR Format Notification Comment -->"
5051 "**👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!**"
5152 ""
5253 "为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流。"
6364 "- 设置需排除的文件/目录(目录请以\"/\"结尾)"
6465 "Set files/directories to exclude (directories should end with \"/\")"
6566 "- 将目标分支设置为 \ 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 }}\`**"
6768 ""
6869 "3. **等待工作流完成 | Wait for the workflow to complete**"
6970 "格式化后的代码将自动推送至你的分支。"
@@ -82,26 +83,46 @@ jobs:
8283 echo "Message content:"
8384 echo "$message"
8485
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+
8593 # 使用 jq 安全地构建 JSON 负载
8694 json_payload=$(jq -n --arg body "$message" '{"body": $body}')
8795
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
95116
96117 # 提取 HTTP 状态码和响应体
97118 http_code=$(echo "$response" | tail -n1)
98119 response_body=$(echo "$response" | sed '$d')
99120
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"
102123 echo "Comment URL: $(echo "$response_body" | jq -r '.html_url')"
103124 else
104- echo "Failed to add comment. HTTP status: $http_code"
125+ echo "Failed to add/update comment. HTTP status: $http_code"
105126 echo "Response: $response_body"
106127 exit 1
107128 fi
0 commit comments