Skip to content
Closed
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
114 changes: 88 additions & 26 deletions .github/workflows/auto-assign-reviewers.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
# 2025-05-10 kurisaW Fixed file existence, cache, and comment time issues
# 2025-05-11 kurisaW Fixed missing unique files creation and cache logic
# 2025-07-14 kurisaW Merge same tag with different paths, remove Path display from CI comment
# 2025-01-23 Assistant Added unique identifier for comment isolation



# Script Function Description: Assign PR reviews based on the MAINTAINERS list.

Expand All @@ -23,7 +26,7 @@ on:
jobs:
assign-reviewers:
runs-on: ubuntu-22.04
if: github.repository_owner == 'RT-Thread'
# if: github.repository_owner == 'RT-Thread'
permissions:
issues: read
pull-requests: write
Expand Down Expand Up @@ -80,14 +83,22 @@ jobs:
echo "$changed_files" > changed_files.txt
echo "Successfully fetched $(wc -l < changed_files.txt) changed files"

# 以下是原有的评论处理逻辑(保持不变)
# 唯一标识符 - 用于识别此工作流的评论
COMMENT_IDENTIFIER="<!-- RT-Thread-Auto-Review-Assistant -->"

# 获取现有评论,只查找包含特定标识符的评论
existing_comment=$(curl -s \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")

# Check if response is valid JSON
if jq -e . >/dev/null 2>&1 <<<"$existing_comment"; then
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64' <<< "$existing_comment")
# 只查找包含特定标识符的评论
existing_comment=$(jq -r --arg identifier "$COMMENT_IDENTIFIER" '
.[] |
select(.user.login == "github-actions[bot]") |
select(.body | contains($identifier)) |
{body: .body} | @base64' <<< "$existing_comment")
else
existing_comment=""
echo "Warning: Invalid JSON response from GitHub API for comments"
Expand All @@ -98,7 +109,7 @@ jobs:
if [[ ! -z "$existing_comment" ]]; then
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')
comment_time=$(TZ='Asia/Shanghai' date -d "$comment_body" +%s)
echo "CACHE_TIMESTAMP=${comment_time}" >> $GITHUB_OUTPUT # 统一使用这个变量名
echo "CACHE_TIMESTAMP=${comment_time}" >> $GITHUB_OUTPUT
echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT
else
comment_time=""
Expand Down Expand Up @@ -340,6 +351,10 @@ jobs:
notified_users=$(cat unique_reviewers_bak.txt | xargs)
fi
current_time=$(TZ='Asia/Shanghai' date +"%Y-%m-%d %H:%M CST")

# 唯一标识符
COMMENT_IDENTIFIER="<!-- RT-Thread-Auto-Review-Assistant -->"

{
echo "## 📌 Code Review Assignment"
echo ""
Expand Down Expand Up @@ -395,49 +410,96 @@ jobs:
id: post_comment
if: steps.generate_reviewers.outputs.HAS_REVIEWERS == 'true'
run: |
# 查找现有的 bot 评论
existing_comment=$(curl -s \
# 唯一标识符
COMMENT_IDENTIFIER="<!-- RT-Thread-Auto-Review-Assistant -->"

# 查找现有的 bot 评论(只查找包含特定标识符的评论)
existing_comments=$(curl -s \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \
jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64')
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")

# 查找包含标识符的评论ID
existing_comment_id=$(echo "$existing_comments" | jq -r --arg identifier "$COMMENT_IDENTIFIER" '
.[] |
select(.user.login == "github-actions[bot]") |
select(.body | contains($identifier)) |
.id' | head -1)

# 读取评论内容
comment_content=$(cat review_data.md)

if [[ -n "$existing_comment" ]]; then
if [[ -n "$existing_comment_id" ]] && [[ "$existing_comment_id" != "null" ]]; then
# 更新现有评论
comment_id=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .id)
echo "Updating existing comment $comment_id"
response=$(curl -s -X PATCH \
echo "Updating existing comment $existing_comment_id"
response=$(curl -s -w "\n%{http_code}" -X PATCH \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id")
-H "Accept: application/vnd.github.v3+json" \
-d "$(jq -n --arg body "$comment_content" '{body: $body}')" \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$existing_comment_id")
else
# 创建新评论
echo "Creating new comment"
response=$(curl -s -X POST \
response=$(curl -s -w "\n%{http_code}" -X POST \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \
-H "Accept: application/vnd.github.v3+json" \
-d "$(jq -n --arg body "$comment_content" '{body: $body}')" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")
fi

# 检查响应
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$d')

if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then
echo "Comment processed successfully (HTTP $http_code)"
else
echo "Failed to process comment. HTTP status: $http_code"
echo "Response: $response_body"
exit 1
fi

- name: Get Comment Time
id: get_comment_time
if: steps.generate_reviewers.outputs.HAS_REVIEWERS == 'true'
run: |
existing_comment=$(curl -s \
# 唯一标识符
COMMENT_IDENTIFIER="<!-- RT-Thread-Auto-Review-Assistant -->"

existing_comments=$(curl -s \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments")

# Check if response is valid JSON
if jq -e . >/dev/null 2>&1 <<<"$existing_comment"; then
existing_comment=$(jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64' <<< "$existing_comment")
if jq -e . >/dev/null 2>&1 <<<"$existing_comments"; then
# 只查找包含特定标识符的评论
existing_comment_body=$(echo "$existing_comments" | jq -r --arg identifier "$COMMENT_IDENTIFIER" '
.[] |
select(.user.login == "github-actions[bot]") |
select(.body | contains($identifier)) |
.body' | head -1)
else
existing_comment=""
existing_comment_body=""
echo "Warning: Invalid JSON response from GitHub API"
echo "Response: $existing_comment"
fi
comment_body="${{ steps.get_approval.outputs.CURRENT_TIME }}"
comment_time=$(TZ='Asia/Shanghai' date -d "$comment_body" +%s)
echo "CACHE_TIMESTAMP=${comment_time}" >> $GITHUB_OUTPUT # 统一使用这个变量名
echo "Debug - Saving cache with timestamp: $comment_time"

if [[ -n "$existing_comment_body" ]]; then
comment_time=$(echo "$existing_comment_body" | sed -nE 's/.*Last Updated: ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} CST).*/\1/p')
if [[ -n "$comment_time" ]]; then
timestamp=$(TZ='Asia/Shanghai' date -d "$comment_time" +%s)
echo "CACHE_TIMESTAMP=${timestamp}" >> $GITHUB_OUTPUT
echo "Debug - Found existing comment with timestamp: $comment_time -> $timestamp"
else
current_time=$(TZ='Asia/Shanghai' date +"%Y-%m-%d %H:%M CST")
timestamp=$(TZ='Asia/Shanghai' date -d "$current_time" +%s)
echo "CACHE_TIMESTAMP=${timestamp}" >> $GITHUB_OUTPUT
echo "Debug - Using current time for cache: $current_time -> $timestamp"
fi
else
current_time=$(TZ='Asia/Shanghai' date +"%Y-%m-%d %H:%M CST")
timestamp=$(TZ='Asia/Shanghai' date -d "$current_time" +%s)
echo "CACHE_TIMESTAMP=${timestamp}" >> $GITHUB_OUTPUT
echo "Debug - No existing comment found, using current time: $current_time -> $timestamp"
fi

mkdir -p $(dirname unique_reviewers_bak.txt)
if [[ -s unique_reviewers.txt ]]; then
Expand All @@ -461,4 +523,4 @@ jobs:
path: |
unique_tags_bak.txt
unique_reviewers_bak.txt
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CACHE_TIMESTAMP }}-${{ github.run_id }}
key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CACHE_TIMESTAMP }}-${{ github.run_id }}
2 changes: 2 additions & 0 deletions .github/workflows/pr_clang_format.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -263,3 +263,5 @@ jobs:
echo "处理文件数: ${{ steps.find-files.outputs.files_count }}"
echo "有更改: ${{ steps.check-changes.outputs.has_changes }}"
echo "clang-format 版本: $(clang-format --version | head -1)"


45 changes: 36 additions & 9 deletions .github/workflows/pr_format_bot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ permissions:

jobs:
notify-format:
if: github.repository_owner == 'RT-Thread'
# if: github.repository_owner == 'RT-Thread'
runs-on: ubuntu-latest
steps:
- name: Check if first commit and add comment
Expand Down Expand Up @@ -45,8 +45,12 @@ jobs:
workflow_url="https://github.com/${fork_repo}/actions/workflows/clang-format.yml"
direct_link="${workflow_url}?branch=${branch}"

# 唯一标识符 - 用于识别此工作流的评论
COMMENT_IDENTIFIER="<!-- RT-Thread-Format-Notification -->"

# 使用数组存储多行消息
message_lines=(
"$COMMENT_IDENTIFIER"
"**👋 感谢您对 RT-Thread 的贡献!Thank you for your contribution to RT-Thread!**"
""
"为确保代码符合 RT-Thread 的编码规范,请在你的仓库中执行以下步骤运行代码格式化工作流。"
Expand Down Expand Up @@ -85,23 +89,46 @@ jobs:
# 使用 jq 安全地构建 JSON 负载
json_payload=$(jq -n --arg body "$message" '{"body": $body}')

# 发送评论到 PR
response=$(curl -s -w "\n%{http_code}" \
-X POST \
# 首先检查是否已存在此工作流的评论
existing_comments=$(curl -s \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
-d "$json_payload")
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments")

# 查找是否已存在带有标识符的评论
existing_comment_id=$(echo "$existing_comments" | jq -r --arg identifier "$COMMENT_IDENTIFIER" '.[] | select(.body | contains($identifier)) | .id' | head -1)

if [ -n "$existing_comment_id" ] && [ "$existing_comment_id" != "null" ]; then
echo "Updating existing format notification comment (ID: $existing_comment_id)"

# 更新现有评论
response=$(curl -s -w "\n%{http_code}" \
-X PATCH \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/issues/comments/$existing_comment_id" \
-d "$json_payload")
else
echo "Adding new format notification comment"

# 发送新评论到 PR
response=$(curl -s -w "\n%{http_code}" \
-X POST \
-H "Accept: application/vnd.github.v3+json" \
-H "Authorization: Bearer $GITHUB_TOKEN" \
"https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \
-d "$json_payload")
fi

# 提取 HTTP 状态码和响应体
http_code=$(echo "$response" | tail -n1)
response_body=$(echo "$response" | sed '$d')

if [ "$http_code" -eq 201 ]; then
echo "Format notification comment added successfully"
if [ "$http_code" -eq 200 ] || [ "$http_code" -eq 201 ]; then
echo "Format notification comment processed successfully"
echo "Comment URL: $(echo "$response_body" | jq -r '.html_url')"
else
echo "Failed to add comment. HTTP status: $http_code"
echo "Failed to process comment. HTTP status: $http_code"
echo "Response: $response_body"
exit 1
fi
Expand Down