Skip to content

Commit 550d072

Browse files
kurisaWRbb666
authored andcommitted
improve[ci]: each comment is assigned a unique ID.
1 parent 4123daf commit 550d072

File tree

2 files changed

+39
-17
lines changed

2 files changed

+39
-17
lines changed

.github/workflows/auto-assign-reviewers.yml

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,13 @@ jobs:
8787
8888
# Check if response is valid JSON
8989
if jq -e . >/dev/null 2>&1 <<<"$existing_comment"; then
90-
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64' <<< "$existing_comment")
90+
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- Auto Review Assistant Comment -->"))) | {body: .body} | @base64' <<< "$existing_comment")
9191
else
9292
existing_comment=""
9393
echo "Warning: Invalid JSON response from GitHub API for comments"
9494
echo "Response: $existing_comment"
9595
fi
96-
96+
9797
comment_body=""
9898
if [[ ! -z "$existing_comment" ]]; then
9999
comment_body=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .body | sed -nE 's/.*Last Updated: ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} CST).*/\1/p')
@@ -341,6 +341,7 @@ jobs:
341341
fi
342342
current_time=$(TZ='Asia/Shanghai' date +"%Y-%m-%d %H:%M CST")
343343
{
344+
echo "<!-- Auto Review Assistant Comment -->"
344345
echo "## 📌 Code Review Assignment"
345346
echo ""
346347
for tag in $unique_tags; do
@@ -399,7 +400,7 @@ jobs:
399400
existing_comment=$(curl -s \
400401
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
401402
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
402-
jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64')
403+
jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- Auto Review Assistant Comment -->"))) | {id: .id, body: .body} | @base64')
403404
404405
if [[ -n "$existing_comment" ]]; then
405406
# 更新现有评论
@@ -428,7 +429,7 @@ jobs:
428429
429430
# Check if response is valid JSON
430431
if jq -e . >/dev/null 2>&1 <<<"$existing_comment"; then
431-
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64' <<< "$existing_comment")
432+
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]" and (.body | contains("<!-- Auto Review Assistant Comment -->"))) | {body: .body} | @base64' <<< "$existing_comment")
432433
else
433434
existing_comment=""
434435
echo "Warning: Invalid JSON response from GitHub API"

.github/workflows/pr_format_bot.yml

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: PR Format Notification
22
on:
33
pull_request_target:
4-
types: [opened, synchronize]
4+
types: [opened]
55

66
permissions:
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 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流。"
@@ -63,7 +64,7 @@ jobs:
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

Comments
 (0)