From 6ca46798a3f64258f69bdf7254069b83f66aa438 Mon Sep 17 00:00:00 2001 From: Yuqiang Wang <2053731441@qq.com> Date: Sat, 10 May 2025 11:12:11 +0800 Subject: [PATCH 01/16] Update main.c --- bsp/stm32/stm32f407-rt-spark/applications/main.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bsp/stm32/stm32f407-rt-spark/applications/main.c b/bsp/stm32/stm32f407-rt-spark/applications/main.c index 38757e1edd0..bfa1d419128 100644 --- a/bsp/stm32/stm32f407-rt-spark/applications/main.c +++ b/bsp/stm32/stm32f407-rt-spark/applications/main.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, RT-Thread Development Team + * Copyright (c) 2025, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * From b2d52f8092120e7b1482ffbbd4a7ccebc7087aa6 Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 11:31:29 +0800 Subject: [PATCH 02/16] fix null file delete error --- .github/workflows/auto-assign-reviewers.yml | 34 +++++++++++++++------ 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index 19fe224b491..900c113ad51 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -88,18 +88,34 @@ jobs: - name: Generate reviewers list id: generate_reviewers run: | - # 根据变更文件路径匹配维护者规则 - rm -f triggered_reviewers.txt - rm -f triggered_tags.txt + # 删除旧文件并初始化空文件 + rm -f triggered_reviewers.txt triggered_tags.txt + touch triggered_reviewers.txt triggered_tags.txt # 确保文件存在 + + # 匹配路径并追加维护者信息 while IFS='|' read -r tag path reviewers; do - # 使用正则匹配路径(支持子目录) - if grep -qE "^$path(/|$)" changed_files.txt; then - echo "$reviewers" | tr ' ' '\n' >> triggered_reviewers.txt - echo "$tag" | tr ' ' '\n' >> triggered_tags.txt + # 转义路径中的正则特殊字符 + escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") + # 精确匹配路径及其子路径 + if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then + # 清理空格并移除空行后追加到文件 + echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt + echo "$tag" >> triggered_tags.txt fi done < tag_data.csv - awk 'NF && !seen[$0]++' triggered_reviewers.txt > unique_reviewers.txt - awk 'NF && !seen[$0]++' triggered_tags.txt > unique_tags.txt + + # 处理去重前检查文件是否有内容 + if [[ -s triggered_reviewers.txt ]]; then + awk 'NF && !seen[$0]++' triggered_reviewers.txt > unique_reviewers.txt + else + echo "No reviewers matched." > unique_reviewers.txt # 生成占位文件 + fi + + if [[ -s triggered_tags.txt ]]; then + awk 'NF && !seen[$0]++' triggered_tags.txt > unique_tags.txt + else + echo "No tags matched." > unique_tags.txt # 生成占位文件 + fi - name: Restore Reviewers Cache id: reviewers-cache-restore if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }} From 9a73922b97c963720e56de6e389cbdbc9760062d Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 11:38:44 +0800 Subject: [PATCH 03/16] path Paths should have no extra space at the end --- MAINTAINERS | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/MAINTAINERS b/MAINTAINERS index 80509dcf18a..a3d88a91549 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -49,7 +49,7 @@ path: .github owners: supper thomas(supperthomas)<78900636@qq.com>, Bingru Zhang(Rbb666)<751061401@qq.com>, Yuqiang Wang(kurisaW)<2053731441@qq.com> tag: stm32f407-rt-spark -path: bsp/stm32/stm32f407-rt-spark +path: bsp/stm32/stm32f407-rt-spark owners: Bingru Zhang(Rbb666)<751061401@qq.com>, Yuqiang Wang(kurisaW)<2053731441@qq.com> tag: libc From e7910241fb4d173af72bc522ca5ec7be4e2c1518 Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 11:44:30 +0800 Subject: [PATCH 04/16] fix bug --- .github/workflows/auto-assign-reviewers.yml | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index 900c113ad51..a26e4a03125 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -72,8 +72,10 @@ jobs: /^tag:/ { tag = substr($0, index($0, $2)) # 提取标签内容 } - /^path:/ { - path = substr($0, index($0, $2)) # 提取路径内容 + /^path:/ { + # 提取 path 字段并去除前后空格 + path = substr($0, index($0, $2)) + gsub(/^[ \t]+|[ \t]+$/, "", path) # 清理前后空格和制表符 } /^owners:/ { owners = substr($0, index($0, $2)) # 提取维护者信息 @@ -93,14 +95,23 @@ jobs: touch triggered_reviewers.txt triggered_tags.txt # 确保文件存在 # 匹配路径并追加维护者信息 + # while IFS='|' read -r tag path reviewers; do + # # 转义路径中的正则特殊字符 + # escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") + # # 精确匹配路径及其子路径 + # if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then + # # 清理空格并移除空行后追加到文件 + # echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt + # echo "$tag" >> triggered_tags.txt + # fi + # done < tag_data.csv while IFS='|' read -r tag path reviewers; do - # 转义路径中的正则特殊字符 escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - # 精确匹配路径及其子路径 + echo "Checking path: '$path' → escaped: '$escaped_path'" # 调试输出 if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then - # 清理空格并移除空行后追加到文件 echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt echo "$tag" >> triggered_tags.txt + echo "Matched! Tag: $tag, Path: $path" # 调试输出 fi done < tag_data.csv From 5b63e2af34626ae559a500d69c4d25c9b605d53e Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 11:51:41 +0800 Subject: [PATCH 05/16] fix bug --- bsp/stm32/stm32f407-rt-spark/board/board.c | 2 +- bsp/stm32/stm32f407-rt-spark/rtconfig.py | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/bsp/stm32/stm32f407-rt-spark/board/board.c b/bsp/stm32/stm32f407-rt-spark/board/board.c index d49a591e487..d2ce8c7be1d 100644 --- a/bsp/stm32/stm32f407-rt-spark/board/board.c +++ b/bsp/stm32/stm32f407-rt-spark/board/board.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023, RT-Thread Development Team + * Copyright (c) 2025, RT-Thread Development Team * * SPDX-License-Identifier: Apache-2.0 * diff --git a/bsp/stm32/stm32f407-rt-spark/rtconfig.py b/bsp/stm32/stm32f407-rt-spark/rtconfig.py index 41c4b00ffb7..cc482008cde 100644 --- a/bsp/stm32/stm32f407-rt-spark/rtconfig.py +++ b/bsp/stm32/stm32f407-rt-spark/rtconfig.py @@ -133,7 +133,6 @@ POST_ACTION = 'fromelf --bin $TARGET --output rtthread.bin \nfromelf -z $TARGET' elif PLATFORM == 'iccarm': - # toolchains CC = 'iccarm' CXX = 'iccarm' AS = 'iasmarm' From d43eaa22a34490bec7d5fda0706b6cc6ff38967b Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 11:58:16 +0800 Subject: [PATCH 06/16] fix --- .github/workflows/auto-assign-reviewers.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index a26e4a03125..b6ea8b46b28 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -108,7 +108,8 @@ jobs: while IFS='|' read -r tag path reviewers; do escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") echo "Checking path: '$path' → escaped: '$escaped_path'" # 调试输出 - if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then + # 修改这行,将(/.*)?改为(/.*)*以支持多级子目录 + if grep -qxE "$escaped_path(/.*)*" changed_files.txt; then echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt echo "$tag" >> triggered_tags.txt echo "Matched! Tag: $tag, Path: $path" # 调试输出 From ca1fe44e1257e9fbbb0be7633d12f323b1dc601a Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 12:02:05 +0800 Subject: [PATCH 07/16] fix --- .github/workflows/auto-assign-reviewers.yml | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index b6ea8b46b28..5717ef295aa 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -107,15 +107,13 @@ jobs: # done < tag_data.csv while IFS='|' read -r tag path reviewers; do escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - echo "Checking path: '$path' → escaped: '$escaped_path'" # 调试输出 - # 修改这行,将(/.*)?改为(/.*)*以支持多级子目录 - if grep -qxE "$escaped_path(/.*)*" changed_files.txt; then + echo "Checking path: '$path' → escaped: '$escaped_path'" + if grep -qE "^$escaped_path(/[^/]+)*/?[^/]*$" changed_files.txt; then echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt echo "$tag" >> triggered_tags.txt - echo "Matched! Tag: $tag, Path: $path" # 调试输出 + echo "Matched! Tag: $tag, Path: $path" fi - done < tag_data.csv - + # 处理去重前检查文件是否有内容 if [[ -s triggered_reviewers.txt ]]; then awk 'NF && !seen[$0]++' triggered_reviewers.txt > unique_reviewers.txt From 8a57a8954575d48fb1e179f52360468b3e0e2b2f Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 12:27:14 +0800 Subject: [PATCH 08/16] fix --- MAINTAINERS | 4 + bsp/renesas/README.md | 1 + bsp/renesas/ra6m3-hmi-board/buildinfo.gpdsc | 141 -------------------- 3 files changed, 5 insertions(+), 141 deletions(-) delete mode 100644 bsp/renesas/ra6m3-hmi-board/buildinfo.gpdsc diff --git a/MAINTAINERS b/MAINTAINERS index a3d88a91549..c2f6f110254 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -52,6 +52,10 @@ tag: stm32f407-rt-spark path: bsp/stm32/stm32f407-rt-spark owners: Bingru Zhang(Rbb666)<751061401@qq.com>, Yuqiang Wang(kurisaW)<2053731441@qq.com> +tag: renesas +path: bsp/renesas +owners: Yuqiang Wang(kurisaW)<2053731441@qq.com> + tag: libc path: components/libc owners: Meco Jianting Man(mysterywolf)<920369182@qq.com> diff --git a/bsp/renesas/README.md b/bsp/renesas/README.md index ef89868352f..0d1a8810806 100644 --- a/bsp/renesas/README.md +++ b/bsp/renesas/README.md @@ -22,6 +22,7 @@ RA 系列 BSP 目前支持情况如下表所示: | **RZ 系列** | | | [rzt2m_rsk](rzt2m_rsk) | Renesas 官方 RSK-RZT2M 开发板 | | [rzn2l_rsk](rzn2l_rsk) | Renesas 官方 RSK-RZN2L 开发板 | +| [rzn2l_etherkit](rzn2l_etherkit) | Renesas 联合 RT-Thread rzn2l_etherkit 开发板 | 可以通过阅读相应 BSP 下的 README 来快速上手,如果想要使用 BSP 更多功能可参考 docs 文件夹下提供的说明文档,如下表所示: diff --git a/bsp/renesas/ra6m3-hmi-board/buildinfo.gpdsc b/bsp/renesas/ra6m3-hmi-board/buildinfo.gpdsc deleted file mode 100644 index 162eb8861ae..00000000000 --- a/bsp/renesas/ra6m3-hmi-board/buildinfo.gpdsc +++ /dev/null @@ -1,141 +0,0 @@ - - - Renesas - Project Content - Project content managed by the Renesas Smart Configurator - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - From 4bacc1126dfc4eb27d848dc8219b62babf449aec Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 12:29:33 +0800 Subject: [PATCH 09/16] fix --- .github/workflows/auto-assign-reviewers.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index 5717ef295aa..ad5b50d6b26 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -126,6 +126,8 @@ jobs: else echo "No tags matched." > unique_tags.txt # 生成占位文件 fi + done < tag_data.csv + - name: Restore Reviewers Cache id: reviewers-cache-restore if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }} From 9f06bebe36860afca4965ae7ac27cdf654e2789a Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 12:31:12 +0800 Subject: [PATCH 10/16] fix --- .github/workflows/auto-assign-reviewers.yml | 32 ++++----------------- 1 file changed, 5 insertions(+), 27 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index ad5b50d6b26..f7e8d9c0619 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -95,39 +95,17 @@ jobs: touch triggered_reviewers.txt triggered_tags.txt # 确保文件存在 # 匹配路径并追加维护者信息 - # while IFS='|' read -r tag path reviewers; do - # # 转义路径中的正则特殊字符 - # escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - # # 精确匹配路径及其子路径 - # if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then - # # 清理空格并移除空行后追加到文件 - # echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt - # echo "$tag" >> triggered_tags.txt - # fi - # done < tag_data.csv while IFS='|' read -r tag path reviewers; do + # 转义路径中的正则特殊字符 escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - echo "Checking path: '$path' → escaped: '$escaped_path'" - if grep -qE "^$escaped_path(/[^/]+)*/?[^/]*$" changed_files.txt; then + # 精确匹配路径及其子路径 + if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then + # 清理空格并移除空行后追加到文件 echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt echo "$tag" >> triggered_tags.txt - echo "Matched! Tag: $tag, Path: $path" fi - - # 处理去重前检查文件是否有内容 - if [[ -s triggered_reviewers.txt ]]; then - awk 'NF && !seen[$0]++' triggered_reviewers.txt > unique_reviewers.txt - else - echo "No reviewers matched." > unique_reviewers.txt # 生成占位文件 - fi - - if [[ -s triggered_tags.txt ]]; then - awk 'NF && !seen[$0]++' triggered_tags.txt > unique_tags.txt - else - echo "No tags matched." > unique_tags.txt # 生成占位文件 - fi done < tag_data.csv - + - name: Restore Reviewers Cache id: reviewers-cache-restore if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }} From 568d660f55a2515e06364c725bd80608f0dfc2ee Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 12:39:08 +0800 Subject: [PATCH 11/16] fix --- .github/workflows/auto-assign-reviewers.yml | 36 +++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index f7e8d9c0619..b6fc5c24e60 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -63,6 +63,11 @@ jobs: echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT fi echo "COMMENT_TIME=${comment_time}" + + echo "=== Changed Files ===" + cat changed_files.txt + echo "=====================" + - name: Parse MAINTAINERS file id: parse_maintainer run: | @@ -90,22 +95,41 @@ jobs: - name: Generate reviewers list id: generate_reviewers run: | - # 删除旧文件并初始化空文件 + # # 删除旧文件并初始化空文件 + # rm -f triggered_reviewers.txt triggered_tags.txt + # touch triggered_reviewers.txt triggered_tags.txt # 确保文件存在 + + # # 匹配路径并追加维护者信息 + # while IFS='|' read -r tag path reviewers; do + # # 转义路径中的正则特殊字符 + # escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") + # # 精确匹配路径及其子路径 + # if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then + # # 清理空格并移除空行后追加到文件 + # echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt + # echo "$tag" >> triggered_tags.txt + # fi + # done < tag_data.csv rm -f triggered_reviewers.txt triggered_tags.txt - touch triggered_reviewers.txt triggered_tags.txt # 确保文件存在 + touch triggered_reviewers.txt triggered_tags.txt - # 匹配路径并追加维护者信息 while IFS='|' read -r tag path reviewers; do # 转义路径中的正则特殊字符 escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - # 精确匹配路径及其子路径 - if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then - # 清理空格并移除空行后追加到文件 + + # 使用增强型正则匹配路径及其所有子目录 + if grep -qE "^$escaped_path(/.*)*" changed_files.txt; then echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt echo "$tag" >> triggered_tags.txt + echo "Matched: $path → $tag" fi done < tag_data.csv + echo "=== Matched Paths ===" + cat triggered_tags.txt + echo "=== Matched Reviewers ===" + cat triggered_reviewers.txt + - name: Restore Reviewers Cache id: reviewers-cache-restore if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }} From 0975518ec3e0e93b700f2adb17e01ea01fb5cbea Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 13:00:20 +0800 Subject: [PATCH 12/16] fix --- .github/workflows/auto-assign-reviewers.yml | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index b6fc5c24e60..d0a0046e980 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -50,6 +50,10 @@ jobs: "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \ jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64') + echo "=== Changed Files ===" + cat changed_files.txt + echo "=====================" + comment_body="" 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} UTC).*/\1/p') @@ -64,9 +68,6 @@ jobs: fi echo "COMMENT_TIME=${comment_time}" - echo "=== Changed Files ===" - cat changed_files.txt - echo "=====================" - name: Parse MAINTAINERS file id: parse_maintainer @@ -123,12 +124,12 @@ jobs: echo "$tag" >> triggered_tags.txt echo "Matched: $path → $tag" fi - done < tag_data.csv + done < tag_data.csv - echo "=== Matched Paths ===" - cat triggered_tags.txt - echo "=== Matched Reviewers ===" - cat triggered_reviewers.txt + echo "=== Matched Paths ===" + cat triggered_tags.txt + echo "=== Matched Reviewers ===" + cat triggered_reviewers.txt - name: Restore Reviewers Cache id: reviewers-cache-restore From 9f263c0a5e444c6de77409159a3c7d91d4f71c59 Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 13:05:51 +0800 Subject: [PATCH 13/16] ci test --- .github/workflows/auto-assign-reviewers.yml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index d0a0046e980..31dc000d3d3 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -93,10 +93,11 @@ jobs: print tag "|" path "|" github_ids } ' MAINTAINERS > tag_data.csv + - name: Generate reviewers list id: generate_reviewers run: | - # # 删除旧文件并初始化空文件 + # 删除旧文件并初始化空文件 # rm -f triggered_reviewers.txt triggered_tags.txt # touch triggered_reviewers.txt triggered_tags.txt # 确保文件存在 @@ -124,12 +125,12 @@ jobs: echo "$tag" >> triggered_tags.txt echo "Matched: $path → $tag" fi - done < tag_data.csv + done < tag_data.csv - echo "=== Matched Paths ===" - cat triggered_tags.txt - echo "=== Matched Reviewers ===" - cat triggered_reviewers.txt + echo "=== Matched Paths ===" + cat triggered_tags.txt + echo "=== Matched Reviewers ===" + cat triggered_reviewers.txt - name: Restore Reviewers Cache id: reviewers-cache-restore From 02fe3ee360491c62632fdc5e2d16272c63ba9db1 Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 13:42:19 +0800 Subject: [PATCH 14/16] ci test --- .github/workflows/auto-assign-reviewers.yml | 283 +++++++++----------- 1 file changed, 126 insertions(+), 157 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index 31dc000d3d3..a3587128978 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -7,6 +7,7 @@ # Date Author Notes # 2025-01-21 kurisaW Initial version # 2025-03-14 hydevcode +# 2025-05-10 kurisaW Fixed file existence, cache, and comment time issues # Script Function Description: Assign PR reviews based on the MAINTAINERS list. @@ -40,229 +41,201 @@ jobs: - name: Get changed files id: changed_files run: | - # 通过 GitHub API 获取 PR 的变更文件列表 + # Initialize changed_files.txt + touch $GITHUB_WORKSPACE/changed_files.txt + + # Get changed files via GitHub API changed_files=$(curl -s \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \ - jq -r '.[].filename') # 使用 jq 提取文件名 - echo "$changed_files" | grep -v '^MAINTAINERS$' > changed_files.txt + jq -r '.[].filename') + echo "$changed_files" | grep -v '^MAINTAINERS$' > $GITHUB_WORKSPACE/changed_files.txt + # Get existing bot comment existing_comment=$(curl -s \ - "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \ - jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64') - + -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]") | .body | @base64') + echo "=== Changed Files ===" - cat changed_files.txt + cat $GITHUB_WORKSPACE/changed_files.txt echo "=====================" - - comment_body="" - 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} UTC).*/\1/p') - - comment_time=$(date -d "$comment_body" +%s) - - echo "${comment_body}" - echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT - else - comment_time="" - echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT + + comment_time="" + if [[ -n "$existing_comment" ]]; then + comment_body=$(echo "$existing_comment" | base64 -d | sed -nE 's/.*Last Updated: ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} UTC).*/\1/p') + if [[ -n "$comment_body" ]]; then + comment_time=$(date -d "$comment_body" +%s 2>/dev/null || echo "") + fi fi - echo "COMMENT_TIME=${comment_time}" - - + echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT + - name: Parse MAINTAINERS file id: parse_maintainer run: | - # 使用 AWK 解析 MAINTAINERS 文件格式: - # 提取 tag(标签)、path(路径)和 owners(维护者 GitHub ID) + # Initialize tag_data.csv + touch $GITHUB_WORKSPACE/tag_data.csv + + # Parse MAINTAINERS file awk ' /^tag:/ { - tag = substr($0, index($0, $2)) # 提取标签内容 + tag = substr($0, index($0, $2)) } /^path:/ { - # 提取 path 字段并去除前后空格 path = substr($0, index($0, $2)) - gsub(/^[ \t]+|[ \t]+$/, "", path) # 清理前后空格和制表符 + gsub(/^[ \t]+|[ \t]+$/, "", path) } /^owners:/ { - owners = substr($0, index($0, $2)) # 提取维护者信息 - split(owners, parts, /[()]/) # 拆分出 GitHub ID(括号内内容) + owners = substr($0, index($0, $2)) + split(owners, parts, /[()]/) github_ids = "" for (i=2; i<=length(parts); i+=2) { - github_ids = github_ids "@" parts[i] " " # 拼接为 @user 格式 + github_ids = github_ids "@" parts[i] " " } print tag "|" path "|" github_ids } - ' MAINTAINERS > tag_data.csv - + ' $GITHUB_WORKSPACE/MAINTAINERS > $GITHUB_WORKSPACE/tag_data.csv + - name: Generate reviewers list id: generate_reviewers run: | - # 删除旧文件并初始化空文件 - # rm -f triggered_reviewers.txt triggered_tags.txt - # touch triggered_reviewers.txt triggered_tags.txt # 确保文件存在 - - # # 匹配路径并追加维护者信息 - # while IFS='|' read -r tag path reviewers; do - # # 转义路径中的正则特殊字符 - # escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - # # 精确匹配路径及其子路径 - # if grep -qxE "$escaped_path(/.*)?" changed_files.txt; then - # # 清理空格并移除空行后追加到文件 - # echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt - # echo "$tag" >> triggered_tags.txt - # fi - # done < tag_data.csv - rm -f triggered_reviewers.txt triggered_tags.txt - touch triggered_reviewers.txt triggered_tags.txt - + # Initialize output files + touch $GITHUB_WORKSPACE/triggered_reviewers.txt $GITHUB_WORKSPACE/triggered_tags.txt $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_tags.txt + while IFS='|' read -r tag path reviewers; do - # 转义路径中的正则特殊字符 escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - - # 使用增强型正则匹配路径及其所有子目录 - if grep -qE "^$escaped_path(/.*)*" changed_files.txt; then - echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt - echo "$tag" >> triggered_tags.txt + if grep -qE "^$escaped_path(/.*)*" $GITHUB_WORKSPACE/changed_files.txt; then + echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> $GITHUB_WORKSPACE/triggered_reviewers.txt + echo "$tag" >> $GITHUB_WORKSPACE/triggered_tags.txt echo "Matched: $path → $tag" fi - done < tag_data.csv - + done < $GITHUB_WORKSPACE/tag_data.csv + + # Generate unique lists + sort -u $GITHUB_WORKSPACE/triggered_reviewers.txt > $GITHUB_WORKSPACE/unique_reviewers.txt + sort -u $GITHUB_WORKSPACE/triggered_tags.txt > $GITHUB_WORKSPACE/unique_tags.txt + echo "=== Matched Paths ===" - cat triggered_tags.txt + cat $GITHUB_WORKSPACE/triggered_tags.txt echo "=== Matched Reviewers ===" - cat triggered_reviewers.txt - + cat $GITHUB_WORKSPACE/triggered_reviewers.txt + - name: Restore Reviewers Cache - id: reviewers-cache-restore + id: reviewers-cache-restore if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }} uses: actions/cache/restore@v4 - with: + with: path: | - unique_tags_bak.txt - unique_reviewers_bak.txt + $GITHUB_WORKSPACE/unique_tags_bak.txt + $GITHUB_WORKSPACE/unique_reviewers_bak.txt key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.COMMENT_TIME }} + - name: Get approval status id: get_approval run: | current_time=$(date -u +"%Y-%m-%d %H:%M UTC") - reviewers=$(cat unique_reviewers.txt | tr '\n' '|') + touch $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/approval_data.txt $GITHUB_WORKSPACE/review_status.md + + reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers.txt | tr '\n' '|' | sed 's/|$//') - # 获取 PR 的所有评论 comments=$(curl -s \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments") - - echo '#!/bin/bash' > approval_data.sh - echo 'declare -A approvals=()' >> approval_data.sh - # 使用 jq 解析包含 LGTM 的有效评论 + echo '#!/bin/bash' > $GITHUB_WORKSPACE/approval_data.sh + echo 'declare -A approvals=()' >> $GITHUB_WORKSPACE/approval_data.sh + jq -r --arg reviewers "$reviewers" ' .[] | - select(.user.login != "github-actions[bot]") | # 排除 bot 的评论 - select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写) + select(.user.login != "github-actions[bot]") | + select(.body | test("^\\s*LGTM\\s*$"; "i")) | .user.login as $user | "@\($user)" as $mention | - select($mention | inside($reviewers)) | # 过滤有效审查者 - "approvals[\"\($mention)\"]=\"\(.created_at)\"" # 记录审批时间 - ' <<< "$comments" >> approval_data.sh + select($mention | inside($reviewers)) | + "approvals[\"\($mention)\"]=\"\(.created_at)\"" + ' <<< "$comments" >> $GITHUB_WORKSPACE/approval_data.sh - # 加载审查数据并生成状态报告 - chmod +x approval_data.sh - source ./approval_data.sh - jq -r --arg reviewers "$reviewers" ' .[] | - select(.user.login != "github-actions[bot]") | # 排除 bot 的评论 - select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写) + select(.user.login != "github-actions[bot]") | + select(.body | test("^\\s*LGTM\\s*$"; "i")) | .user.login as $user | "@\($user)" as $mention | - select($mention | inside($reviewers)) | # 过滤有效审查者 - "\($mention) \(.created_at)" # 输出审查者和时间 - ' <<< "$comments" >> approval_data.txt - + select($mention | inside($reviewers)) | + "\($mention) \(.created_at)" + ' <<< "$comments" >> $GITHUB_WORKSPACE/approval_data.txt + + chmod +x $GITHUB_WORKSPACE/approval_data.sh + source $GITHUB_WORKSPACE/approval_data.sh + notified_users="" - if [[ -f unique_reviewers_bak.txt ]]; then - notified_users=$(cat unique_reviewers_bak.txt | xargs) - else - notified_users="" + if [[ -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt ]]; then + notified_users=$(cat $GITHUB_WORKSPACE/unique_reviewers_bak.txt | xargs) fi - + { echo "---" echo "### 📊 Current Review Status (Last Updated: $current_time)" while read -r reviewer; do - formatted_reviewers="" - for r in $reviewers; do - if [[ " ${notified_users[@]} " =~ " $reviewer " ]]; then - formatted_reviewers+="${reviewer#@}" - else - formatted_reviewers+="$reviewer" - fi - done - + formatted_reviewer="${reviewer#@}" if [[ -n "${approvals[$reviewer]}" ]]; then timestamp=$(date -d "${approvals[$reviewer]}" -u +"%Y-%m-%d %H:%M UTC") - - echo "- ✅ **$formatted_reviewers** Reviewed On $timestamp" + echo "- ✅ **$formatted_reviewer** Reviewed On $timestamp" else - echo "- ⌛ **$formatted_reviewers** Pending Review" + if [[ " $notified_users " =~ " $reviewer " ]]; then + echo "- ⌛ **$formatted_reviewer** Pending Review" + else + echo "- ⌛ **@$formatted_reviewer** Pending Review" + fi fi - done < unique_reviewers.txt - } > review_status.md + done < $GITHUB_WORKSPACE/unique_reviewers.txt + } > $GITHUB_WORKSPACE/review_status.md echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT + - name: Generate review data id: generate_review run: | - unique_tags="" - unique_tags=$(cat unique_tags.txt | xargs) + touch $GITHUB_WORKSPACE/review_data.md + + unique_tags=$(cat $GITHUB_WORKSPACE/unique_tags.txt | xargs) unique_tags_bak="" - if [[ -f unique_tags_bak.txt ]]; then - unique_tags_bak=$(cat unique_tags_bak.txt | xargs) + if [[ -f $GITHUB_WORKSPACE/unique_tags_bak.txt ]]; then + unique_tags_bak=$(cat $GITHUB_WORKSPACE/unique_tags_bak.txt | xargs) fi - + existing_tags="" - for r in $unique_tags; do - if [[ " ${unique_tags_bak[@]} " =~ " $r " ]]; then - echo "$r 不存在于数组中" - else - existing_tags+="$r " + for tag in $unique_tags; do + if [[ ! " $unique_tags_bak " =~ " $tag " ]]; then + existing_tags+="$tag " fi done - + current_time=$(date -u +"%Y-%m-%d %H:%M UTC") { - - # 生成审查分配信息 echo "## 📌 Code Review Assignment" echo "" - while IFS='|' read -r tag path reviewers; do - if grep -qE "^$path(/|$)" changed_files.txt; then + escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") + if grep -qE "^$escaped_path(/|$)" $GITHUB_WORKSPACE/changed_files.txt; then echo "### 🏷️ Tag: $tag" - echo "**Path:** \`$path\` " - - if [[ " ${existing_tags[@]} " =~ " $tag " ]]; then - echo "**Reviewers:** $reviewers " + echo "**Path:** \`$path\`" + if [[ " $existing_tags " =~ " $tag " ]]; then + echo "**Reviewers:** $reviewers" else - formatted_reviewers="" - for r in $reviewers; do - formatted_reviewers+="${r#@} " - done - echo "**Reviewers:** $formatted_reviewers " + formatted_reviewers=$(echo "$reviewers" | sed 's/@//g') + echo "**Reviewers:** $formatted_reviewers" fi - echo "
" echo "Changed Files (Click to expand)" echo "" - grep -E "^$path(/|$)" changed_files.txt | sed 's/^/- /' # 列出匹配的变更文件 + grep -E "^$escaped_path(/|$)" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /' echo "" echo "
" echo "" fi - done < tag_data.csv - # 插入审查状态 - cat review_status.md - + done < $GITHUB_WORKSPACE/tag_data.csv + cat $GITHUB_WORKSPACE/review_status.md echo "---" echo "### 📝 Review Instructions" echo "" @@ -277,48 +250,44 @@ jobs: echo "" echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**" echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**" - } > review_data.md + } > $GITHUB_WORKSPACE/review_data.md + - name: Post/Update comment id: post_comment run: | - # 查找现有的 bot 评论 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" | \ jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64') if [[ -n "$existing_comment" ]]; then - # 更新现有评论 - comment_id=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .id) + comment_id=$(echo "$existing_comment" | base64 -d | jq -r .id) echo "Updating existing comment $comment_id" - response=$(curl -s -X PATCH \ + curl -s -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") + -d "$(jq -n --arg body "$(cat $GITHUB_WORKSPACE/review_data.md)" '{body: $body}')" \ + "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id" else - # 创建新评论 echo "Creating new comment" - response=$(curl -s -X POST \ + curl -s -X POST \ -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/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments") + -d "$(jq -n --arg body "$(cat $GITHUB_WORKSPACE/review_data.md)" '{body: $body}')" \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" fi + - name: Get Comment Time id: get_comment_time run: | - existing_comment=$(curl -s \ - "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \ - jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64') - comment_body="${{ steps.get_approval.outputs.CURRENT_TIME }}" - comment_time=$(date -d "$comment_body" +%s) - echo "CURRENT_TIME=${comment_time}" >> $GITHUB_OUTPUT - cp unique_reviewers.txt unique_reviewers_bak.txt - cp unique_tags.txt unique_tags_bak.txt - - name: Restore Reviewers Save + current_time=$(date -u +"%s") + echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT + cp $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_reviewers_bak.txt + cp $GITHUB_WORKSPACE/unique_tags.txt $GITHUB_WORKSPACE/unique_tags_bak.txt + + - name: Save Reviewers Cache id: reviewers-cache-save uses: actions/cache/save@v4 with: path: | - unique_tags_bak.txt - unique_reviewers_bak.txt + $GITHUB_WORKSPACE/unique_tags_bak.txt + $GITHUB_WORKSPACE/unique_reviewers_bak.txt key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }} \ No newline at end of file From 0bdd6fcc95de2207cfa2a4e15be9a218f52cce61 Mon Sep 17 00:00:00 2001 From: Yuqiang Wang Date: Sat, 10 May 2025 14:01:54 +0800 Subject: [PATCH 15/16] ci test --- .github/workflows/auto-assign-reviewers.yml | 160 +++++++++++++++----- 1 file changed, 124 insertions(+), 36 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index a3587128978..6c6c1f5d10d 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -21,7 +21,7 @@ on: jobs: assign-reviewers: runs-on: ubuntu-22.04 - if: github.repository_owner == 'RT-Thread' + if: github.repository_owner == 'RT-Thread' && github.event.pull_request.head.repo.full_name == github.repository permissions: issues: read pull-requests: write @@ -32,30 +32,59 @@ jobs: run: | PR_NUMBER=${{ github.event.pull_request.number }} echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT + echo "DEBUG: Extracted PR number: $PR_NUMBER" >&2 + - name: Checkout code uses: actions/checkout@v4 with: ref: master sparse-checkout: MAINTAINERS persist-credentials: false + + - name: Verify MAINTAINERS file + run: | + if [[ ! -f $GITHUB_WORKSPACE/MAINTAINERS ]]; then + echo "Error: MAINTAINERS file not found" + exit 1 + fi + echo "DEBUG: MAINTAINERS file verified" >&2 + - name: Get changed files id: changed_files run: | - # Initialize changed_files.txt touch $GITHUB_WORKSPACE/changed_files.txt - # Get changed files via GitHub API - changed_files=$(curl -s \ - -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \ - jq -r '.[].filename') - echo "$changed_files" | grep -v '^MAINTAINERS$' > $GITHUB_WORKSPACE/changed_files.txt + # Retry API call up to 3 times + for attempt in {1..3}; do + changed_files=$(curl -s -f \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \ + jq -r '.[].filename' || echo "") + if [[ -n "$changed_files" ]]; then + echo "$changed_files" | grep -v '^MAINTAINERS$' > $GITHUB_WORKSPACE/changed_files.txt + break + fi + echo "Attempt $attempt failed to fetch changed files, retrying..." + sleep 2 + done + + if [[ -z "$changed_files" ]]; then + echo "Error: Failed to fetch changed files after 3 attempts" + exit 1 + fi # Get existing bot comment - 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" | \ - jq -r '.[] | select(.user.login == "github-actions[bot]") | .body | @base64') + for attempt in {1..3}; do + existing_comment=$(curl -s -f \ + -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]") | .body | @base64' || echo "") + if [[ -n "$existing_comment" || $attempt -eq 3 ]]; then + break + fi + echo "Attempt $attempt failed to fetch comments, retrying..." + sleep 2 + done echo "=== Changed Files ===" cat $GITHUB_WORKSPACE/changed_files.txt @@ -69,14 +98,13 @@ jobs: fi fi echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT - + echo "DEBUG: Comment time: $comment_time" >&2 + - name: Parse MAINTAINERS file id: parse_maintainer run: | - # Initialize tag_data.csv touch $GITHUB_WORKSPACE/tag_data.csv - # Parse MAINTAINERS file awk ' /^tag:/ { tag = substr($0, index($0, $2)) @@ -95,23 +123,27 @@ jobs: print tag "|" path "|" github_ids } ' $GITHUB_WORKSPACE/MAINTAINERS > $GITHUB_WORKSPACE/tag_data.csv - + echo "DEBUG: MAINTAINERS parsed, tag_data.csv created" >&2 + - name: Generate reviewers list id: generate_reviewers run: | - # Initialize output files touch $GITHUB_WORKSPACE/triggered_reviewers.txt $GITHUB_WORKSPACE/triggered_tags.txt $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_tags.txt while IFS='|' read -r tag path reviewers; do escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - if grep -qE "^$escaped_path(/.*)*" $GITHUB_WORKSPACE/changed_files.txt; then + if [[ "$path" == */ ]]; then + grep -qE "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt + else + grep -qE "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt + fi + if [[ $? -eq 0 ]]; then echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> $GITHUB_WORKSPACE/triggered_reviewers.txt echo "$tag" >> $GITHUB_WORKSPACE/triggered_tags.txt - echo "Matched: $path → $tag" + echo "DEBUG: Matched: $path → $tag" >&2 fi done < $GITHUB_WORKSPACE/tag_data.csv - # Generate unique lists sort -u $GITHUB_WORKSPACE/triggered_reviewers.txt > $GITHUB_WORKSPACE/unique_reviewers.txt sort -u $GITHUB_WORKSPACE/triggered_tags.txt > $GITHUB_WORKSPACE/unique_tags.txt @@ -119,17 +151,26 @@ jobs: cat $GITHUB_WORKSPACE/triggered_tags.txt echo "=== Matched Reviewers ===" cat $GITHUB_WORKSPACE/triggered_reviewers.txt - + - name: Restore Reviewers Cache id: reviewers-cache-restore - if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }} + if: steps.changed_files.outputs.COMMENT_TIME != '' uses: actions/cache/restore@v4 with: path: | $GITHUB_WORKSPACE/unique_tags_bak.txt $GITHUB_WORKSPACE/unique_reviewers_bak.txt key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.COMMENT_TIME }} - + + - name: Validate Restored Cache + if: steps.reviewers-cache-restore.outputs.cache-hit == 'true' + run: | + if [[ ! -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt || ! -f $GITHUB_WORKSPACE/unique_tags_bak.txt ]]; then + echo "Error: Invalid cache data" + exit 1 + fi + echo "DEBUG: Cache validated" >&2 + - name: Get approval status id: get_approval run: | @@ -138,9 +179,21 @@ jobs: reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers.txt | tr '\n' '|' | sed 's/|$//') - comments=$(curl -s \ - -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments") + for attempt in {1..3}; do + comments=$(curl -s -f \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" || echo "") + if [[ -n "$comments" || $attempt -eq 3 ]]; then + break + fi + echo "Attempt $attempt failed to fetch comments, retrying..." + sleep 2 + done + + if [[ -z "$comments" ]]; then + echo "Error: Failed to fetch comments after 3 attempts" + exit 1 + fi echo '#!/bin/bash' > $GITHUB_WORKSPACE/approval_data.sh echo 'declare -A approvals=()' >> $GITHUB_WORKSPACE/approval_data.sh @@ -192,7 +245,8 @@ jobs: } > $GITHUB_WORKSPACE/review_status.md echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT - + echo "DEBUG: Approval status generated" >&2 + - name: Generate review data id: generate_review run: | @@ -217,7 +271,12 @@ jobs: echo "" while IFS='|' read -r tag path reviewers; do escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - if grep -qE "^$escaped_path(/|$)" $GITHUB_WORKSPACE/changed_files.txt; then + if [[ "$path" == */ ]]; then + grep -qE "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt + else + grep -qE "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt + fi + if [[ $? -eq 0 ]]; then echo "### 🏷️ Tag: $tag" echo "**Path:** \`$path\`" if [[ " $existing_tags " =~ " $tag " ]]; then @@ -229,7 +288,11 @@ jobs: echo "
" echo "Changed Files (Click to expand)" echo "" - grep -E "^$escaped_path(/|$)" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /' + if [[ "$path" == */ ]]; then + grep -E "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /' + else + grep -E "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /' + fi echo "" echo "
" echo "" @@ -251,14 +314,37 @@ jobs: echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**" echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**" } > $GITHUB_WORKSPACE/review_data.md - + echo "DEBUG: Review data generated" >&2 + - name: Post/Update comment id: post_comment run: | - 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" | \ - jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64') + for attempt in {1..3}; do + existing_comment=$(curl -s -f \ + -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' || echo "") + if [[ -n "$existing_comment" || $attempt -eq 3 ]]; then + break + fi + echo "Attempt $attempt failed to fetch comments, retrying..." + sleep 2 + done + + new_reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers.txt | sort | tr '\n' ' ') + old_reviewers="" + if [[ -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt ]]; then + old_reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers_bak.txt | sort | tr '\n' ' ') + fi + + if [[ "$new_reviewers" != "$old_reviewers" && -n "$existing_comment" ]]; then + comment_id=$(echo "$existing_comment" | base64 -d | jq -r .id) + echo "Reviewers changed, deleting old comment $comment_id" + curl -s -X DELETE \ + -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ + "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id" + existing_comment="" + fi if [[ -n "$existing_comment" ]]; then comment_id=$(echo "$existing_comment" | base64 -d | jq -r .id) @@ -274,7 +360,8 @@ jobs: -d "$(jq -n --arg body "$(cat $GITHUB_WORKSPACE/review_data.md)" '{body: $body}')" \ "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" fi - + echo "DEBUG: Comment posted/updated" >&2 + - name: Get Comment Time id: get_comment_time run: | @@ -282,7 +369,8 @@ jobs: echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT cp $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_reviewers_bak.txt cp $GITHUB_WORKSPACE/unique_tags.txt $GITHUB_WORKSPACE/unique_tags_bak.txt - + echo "DEBUG: Comment time: $current_time" >&2 + - name: Save Reviewers Cache id: reviewers-cache-save uses: actions/cache/save@v4 From c69176fe8d174c7bee433fb431b52931b79d9850 Mon Sep 17 00:00:00 2001 From: Yuqiang Wang <2053731441@qq.com> Date: Sun, 11 May 2025 08:30:48 +0800 Subject: [PATCH 16/16] Update auto-assign-reviewers.yml --- .github/workflows/auto-assign-reviewers.yml | 384 +++++++++----------- 1 file changed, 163 insertions(+), 221 deletions(-) diff --git a/.github/workflows/auto-assign-reviewers.yml b/.github/workflows/auto-assign-reviewers.yml index 6c6c1f5d10d..c46cb0eaf2e 100644 --- a/.github/workflows/auto-assign-reviewers.yml +++ b/.github/workflows/auto-assign-reviewers.yml @@ -21,7 +21,7 @@ on: jobs: assign-reviewers: runs-on: ubuntu-22.04 - if: github.repository_owner == 'RT-Thread' && github.event.pull_request.head.repo.full_name == github.repository + if: github.repository_owner == 'RT-Thread' permissions: issues: read pull-requests: write @@ -32,273 +32,236 @@ jobs: run: | PR_NUMBER=${{ github.event.pull_request.number }} echo "PR_NUMBER=${PR_NUMBER}" >> $GITHUB_OUTPUT - echo "DEBUG: Extracted PR number: $PR_NUMBER" >&2 - - name: Checkout code uses: actions/checkout@v4 with: ref: master sparse-checkout: MAINTAINERS persist-credentials: false - - - name: Verify MAINTAINERS file - run: | - if [[ ! -f $GITHUB_WORKSPACE/MAINTAINERS ]]; then - echo "Error: MAINTAINERS file not found" - exit 1 - fi - echo "DEBUG: MAINTAINERS file verified" >&2 - - name: Get changed files id: changed_files run: | - touch $GITHUB_WORKSPACE/changed_files.txt - - # Retry API call up to 3 times - for attempt in {1..3}; do - changed_files=$(curl -s -f \ - -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \ - jq -r '.[].filename' || echo "") - if [[ -n "$changed_files" ]]; then - echo "$changed_files" | grep -v '^MAINTAINERS$' > $GITHUB_WORKSPACE/changed_files.txt - break - fi - echo "Attempt $attempt failed to fetch changed files, retrying..." - sleep 2 - done - - if [[ -z "$changed_files" ]]; then - echo "Error: Failed to fetch changed files after 3 attempts" - exit 1 - fi - - # Get existing bot comment - for attempt in {1..3}; do - existing_comment=$(curl -s -f \ - -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]") | .body | @base64' || echo "") - if [[ -n "$existing_comment" || $attempt -eq 3 ]]; then - break - fi - echo "Attempt $attempt failed to fetch comments, retrying..." - sleep 2 - done + # 通过 GitHub API 获取 PR 的变更文件列表 + changed_files=$(curl -s \ + "https://api.github.com/repos/${{ github.repository }}/pulls/${{ steps.extract-pr.outputs.PR_NUMBER }}/files" | \ + jq -r '.[].filename') # 使用 jq 提取文件名 + echo "$changed_files" | grep -v '^MAINTAINERS$' > changed_files.txt + existing_comment=$(curl -s \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \ + jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64') + echo "=== Changed Files ===" - cat $GITHUB_WORKSPACE/changed_files.txt + cat changed_files.txt echo "=====================" - - comment_time="" - if [[ -n "$existing_comment" ]]; then - comment_body=$(echo "$existing_comment" | base64 -d | sed -nE 's/.*Last Updated: ([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2} UTC).*/\1/p') - if [[ -n "$comment_body" ]]; then - comment_time=$(date -d "$comment_body" +%s 2>/dev/null || echo "") - fi + + comment_body="" + 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} UTC).*/\1/p') + + comment_time=$(date -d "$comment_body" +%s) + + echo "${comment_body}" + echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT + else + comment_time="" + echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT fi - echo "COMMENT_TIME=${comment_time}" >> $GITHUB_OUTPUT - echo "DEBUG: Comment time: $comment_time" >&2 + echo "COMMENT_TIME=${comment_time}" + - name: Parse MAINTAINERS file id: parse_maintainer run: | - touch $GITHUB_WORKSPACE/tag_data.csv - + # 使用 AWK 解析 MAINTAINERS 文件格式: + # 提取 tag(标签)、path(路径)和 owners(维护者 GitHub ID) awk ' /^tag:/ { - tag = substr($0, index($0, $2)) + tag = substr($0, index($0, $2)) # 提取标签内容 } /^path:/ { + # 提取 path 字段并去除前后空格 path = substr($0, index($0, $2)) - gsub(/^[ \t]+|[ \t]+$/, "", path) + gsub(/^[ \t]+|[ \t]+$/, "", path) # 清理前后空格和制表符 } /^owners:/ { - owners = substr($0, index($0, $2)) - split(owners, parts, /[()]/) + owners = substr($0, index($0, $2)) # 提取维护者信息 + split(owners, parts, /[()]/) # 拆分出 GitHub ID(括号内内容) github_ids = "" for (i=2; i<=length(parts); i+=2) { - github_ids = github_ids "@" parts[i] " " + github_ids = github_ids "@" parts[i] " " # 拼接为 @user 格式 } print tag "|" path "|" github_ids } - ' $GITHUB_WORKSPACE/MAINTAINERS > $GITHUB_WORKSPACE/tag_data.csv - echo "DEBUG: MAINTAINERS parsed, tag_data.csv created" >&2 + ' MAINTAINERS > tag_data.csv - name: Generate reviewers list id: generate_reviewers run: | - touch $GITHUB_WORKSPACE/triggered_reviewers.txt $GITHUB_WORKSPACE/triggered_tags.txt $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_tags.txt - + rm -f triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt + touch triggered_reviewers.txt triggered_tags.txt unique_reviewers.txt unique_tags.txt + while IFS='|' read -r tag path reviewers; do + # 转义路径中的正则特殊字符 escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - if [[ "$path" == */ ]]; then - grep -qE "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt - else - grep -qE "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt + + # 使用增强型正则匹配路径及其所有子目录 + if grep -qE "^$escaped_path(/.*)*" changed_files.txt; then + echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> triggered_reviewers.txt + echo "$tag" >> triggered_tags.txt + echo "Matched: $path → $tag" fi - if [[ $? -eq 0 ]]; then - echo "$reviewers" | tr -s ' ' '\n' | sed '/^$/d' >> $GITHUB_WORKSPACE/triggered_reviewers.txt - echo "$tag" >> $GITHUB_WORKSPACE/triggered_tags.txt - echo "DEBUG: Matched: $path → $tag" >&2 - fi - done < $GITHUB_WORKSPACE/tag_data.csv - - sort -u $GITHUB_WORKSPACE/triggered_reviewers.txt > $GITHUB_WORKSPACE/unique_reviewers.txt - sort -u $GITHUB_WORKSPACE/triggered_tags.txt > $GITHUB_WORKSPACE/unique_tags.txt - + done < tag_data.csv + + # 从 triggered_reviewers.txt 生成 unique_reviewers.txt(去重) + sort triggered_reviewers.txt | uniq > unique_reviewers.txt + # 从 triggered_tags.txt 生成 unique_tags.txt(去重) + sort triggered_tags.txt | uniq > unique_tags.txt + echo "=== Matched Paths ===" - cat $GITHUB_WORKSPACE/triggered_tags.txt + cat unique_tags.txt echo "=== Matched Reviewers ===" - cat $GITHUB_WORKSPACE/triggered_reviewers.txt + cat unique_reviewers.txt - name: Restore Reviewers Cache - id: reviewers-cache-restore - if: steps.changed_files.outputs.COMMENT_TIME != '' + id: reviewers-cache-restore + if: ${{ steps.changed_files.outputs.COMMENT_TIME != '' }} uses: actions/cache/restore@v4 - with: + with: path: | - $GITHUB_WORKSPACE/unique_tags_bak.txt - $GITHUB_WORKSPACE/unique_reviewers_bak.txt + unique_tags_bak.txt + unique_reviewers_bak.txt key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.changed_files.outputs.COMMENT_TIME }} - - - name: Validate Restored Cache - if: steps.reviewers-cache-restore.outputs.cache-hit == 'true' - run: | - if [[ ! -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt || ! -f $GITHUB_WORKSPACE/unique_tags_bak.txt ]]; then - echo "Error: Invalid cache data" - exit 1 - fi - echo "DEBUG: Cache validated" >&2 - - name: Get approval status id: get_approval run: | current_time=$(date -u +"%Y-%m-%d %H:%M UTC") - touch $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/approval_data.txt $GITHUB_WORKSPACE/review_status.md - - reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers.txt | tr '\n' '|' | sed 's/|$//') - - for attempt in {1..3}; do - comments=$(curl -s -f \ - -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" || echo "") - if [[ -n "$comments" || $attempt -eq 3 ]]; then - break - fi - echo "Attempt $attempt failed to fetch comments, retrying..." - sleep 2 - done - if [[ -z "$comments" ]]; then - echo "Error: Failed to fetch comments after 3 attempts" - exit 1 + # 检查 unique_reviewers.txt 是否存在 + if [[ ! -f unique_reviewers.txt ]]; then + echo "Error: unique_reviewers.txt not found. Skipping approval status generation." + echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT + exit 0 fi + + reviewers=$(cat unique_reviewers.txt | tr '\n' '|') - echo '#!/bin/bash' > $GITHUB_WORKSPACE/approval_data.sh - echo 'declare -A approvals=()' >> $GITHUB_WORKSPACE/approval_data.sh + # 获取 PR 的所有评论 + comments=$(curl -s \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments") + + echo '#!/bin/bash' > approval_data.sh + echo 'declare -A approvals=()' >> approval_data.sh + # 使用 jq 解析包含 LGTM 的有效评论 jq -r --arg reviewers "$reviewers" ' .[] | - select(.user.login != "github-actions[bot]") | - select(.body | test("^\\s*LGTM\\s*$"; "i")) | + select(.user.login != "github-actions[bot]") | # 排除 bot 的评论 + select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写) .user.login as $user | "@\($user)" as $mention | - select($mention | inside($reviewers)) | - "approvals[\"\($mention)\"]=\"\(.created_at)\"" - ' <<< "$comments" >> $GITHUB_WORKSPACE/approval_data.sh + select($mention | inside($reviewers)) | # 过滤有效审查者 + "approvals[\"\($mention)\"]=\"\(.created_at)\"" # 记录审批时间 + ' <<< "$comments" >> approval_data.sh + # 加载审查数据并生成状态报告 + chmod +x approval_data.sh + source ./approval_data.sh + jq -r --arg reviewers "$reviewers" ' .[] | - select(.user.login != "github-actions[bot]") | - select(.body | test("^\\s*LGTM\\s*$"; "i")) | + select(.user.login != "github-actions[bot]") | # 排除 bot 的评论 + select(.body | test("^\\s*LGTM\\s*$"; "i")) | # 匹配 LGTM 评论(不区分大小写) .user.login as $user | "@\($user)" as $mention | - select($mention | inside($reviewers)) | - "\($mention) \(.created_at)" - ' <<< "$comments" >> $GITHUB_WORKSPACE/approval_data.txt - - chmod +x $GITHUB_WORKSPACE/approval_data.sh - source $GITHUB_WORKSPACE/approval_data.sh - + select($mention | inside($reviewers)) | # 过滤有效审查者 + "\($mention) \(.created_at)" # 输出审查者和时间 + ' <<< "$comments" >> approval_data.txt + notified_users="" - if [[ -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt ]]; then - notified_users=$(cat $GITHUB_WORKSPACE/unique_reviewers_bak.txt | xargs) + if [[ -f unique_reviewers_bak.txt ]]; then + notified_users=$(cat unique_reviewers_bak.txt | xargs) + else + notified_users="" fi - + { echo "---" echo "### 📊 Current Review Status (Last Updated: $current_time)" while read -r reviewer; do - formatted_reviewer="${reviewer#@}" + formatted_reviewers="" + for r in $reviewers; do + if [[ " ${notified_users[@]} " =~ " $reviewer " ]]; then + formatted_reviewers+="${reviewer#@}" + else + formatted_reviewers+="$reviewer" + fi + done + if [[ -n "${approvals[$reviewer]}" ]]; then timestamp=$(date -d "${approvals[$reviewer]}" -u +"%Y-%m-%d %H:%M UTC") - echo "- ✅ **$formatted_reviewer** Reviewed On $timestamp" + echo "- ✅ **$formatted_reviewers** Reviewed On $timestamp" else - if [[ " $notified_users " =~ " $reviewer " ]]; then - echo "- ⌛ **$formatted_reviewer** Pending Review" - else - echo "- ⌛ **@$formatted_reviewer** Pending Review" - fi + echo "- ⌛ **$formatted_reviewers** Pending Review" fi - done < $GITHUB_WORKSPACE/unique_reviewers.txt - } > $GITHUB_WORKSPACE/review_status.md + done < unique_reviewers.txt + } > review_status.md echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT - echo "DEBUG: Approval status generated" >&2 - + - name: Generate review data id: generate_review run: | - touch $GITHUB_WORKSPACE/review_data.md - - unique_tags=$(cat $GITHUB_WORKSPACE/unique_tags.txt | xargs) + unique_tags="" + unique_tags=$(cat unique_tags.txt | xargs) unique_tags_bak="" - if [[ -f $GITHUB_WORKSPACE/unique_tags_bak.txt ]]; then - unique_tags_bak=$(cat $GITHUB_WORKSPACE/unique_tags_bak.txt | xargs) + if [[ -f unique_tags_bak.txt ]]; then + unique_tags_bak=$(cat unique_tags_bak.txt | xargs) fi - + existing_tags="" - for tag in $unique_tags; do - if [[ ! " $unique_tags_bak " =~ " $tag " ]]; then - existing_tags+="$tag " + for r in $unique_tags; do + if [[ " ${unique_tags_bak[@]} " =~ " $r " ]]; then + echo "$r 不存在于数组中" + else + existing_tags+="$r " fi done - + current_time=$(date -u +"%Y-%m-%d %H:%M UTC") { + + # 生成审查分配信息 echo "## 📌 Code Review Assignment" echo "" + while IFS='|' read -r tag path reviewers; do - escaped_path=$(sed 's/[.[\*^$]/\\&/g' <<< "$path") - if [[ "$path" == */ ]]; then - grep -qE "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt - else - grep -qE "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt - fi - if [[ $? -eq 0 ]]; then + if grep -qE "^$path(/|$)" changed_files.txt; then echo "### 🏷️ Tag: $tag" - echo "**Path:** \`$path\`" - if [[ " $existing_tags " =~ " $tag " ]]; then - echo "**Reviewers:** $reviewers" + echo "**Path:** \`$path\` " + + if [[ " ${existing_tags[@]} " =~ " $tag " ]]; then + echo "**Reviewers:** $reviewers " else - formatted_reviewers=$(echo "$reviewers" | sed 's/@//g') - echo "**Reviewers:** $formatted_reviewers" + formatted_reviewers="" + for r in $reviewers; do + formatted_reviewers+="${r#@} " + done + echo "**Reviewers:** $formatted_reviewers " fi + echo "
" echo "Changed Files (Click to expand)" echo "" - if [[ "$path" == */ ]]; then - grep -E "^$escaped_path(/.*)?$" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /' - else - grep -E "^$escaped_path$" $GITHUB_WORKSPACE/changed_files.txt | sed 's/^/- /' - fi + grep -E "^$path(/|$)" changed_files.txt | sed 's/^/- /' # 列出匹配的变更文件 echo "" echo "
" echo "" fi - done < $GITHUB_WORKSPACE/tag_data.csv - cat $GITHUB_WORKSPACE/review_status.md + done < tag_data.csv + # 插入审查状态 + cat review_status.md + echo "---" echo "### 📝 Review Instructions" echo "" @@ -313,69 +276,48 @@ jobs: echo "" echo "> ℹ️ **刷新CI状态操作需要具备仓库写入权限。**" echo "> ℹ️ **Refresh CI status operation requires repository Write permission.**" - } > $GITHUB_WORKSPACE/review_data.md - echo "DEBUG: Review data generated" >&2 - + } > review_data.md - name: Post/Update comment id: post_comment run: | - for attempt in {1..3}; do - existing_comment=$(curl -s -f \ - -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' || echo "") - if [[ -n "$existing_comment" || $attempt -eq 3 ]]; then - break - fi - echo "Attempt $attempt failed to fetch comments, retrying..." - sleep 2 - done - - new_reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers.txt | sort | tr '\n' ' ') - old_reviewers="" - if [[ -f $GITHUB_WORKSPACE/unique_reviewers_bak.txt ]]; then - old_reviewers=$(cat $GITHUB_WORKSPACE/unique_reviewers_bak.txt | sort | tr '\n' ' ') - fi - - if [[ "$new_reviewers" != "$old_reviewers" && -n "$existing_comment" ]]; then - comment_id=$(echo "$existing_comment" | base64 -d | jq -r .id) - echo "Reviewers changed, deleting old comment $comment_id" - curl -s -X DELETE \ - -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id" - existing_comment="" - fi + # 查找现有的 bot 评论 + 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" | \ + jq -r '.[] | select(.user.login == "github-actions[bot]") | {id: .id, body: .body} | @base64') if [[ -n "$existing_comment" ]]; then - comment_id=$(echo "$existing_comment" | base64 -d | jq -r .id) + # 更新现有评论 + comment_id=$(echo "$existing_comment" | head -1 | base64 -d | jq -r .id) echo "Updating existing comment $comment_id" - curl -s -X PATCH \ + response=$(curl -s -X PATCH \ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - -d "$(jq -n --arg body "$(cat $GITHUB_WORKSPACE/review_data.md)" '{body: $body}')" \ - "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id" + -d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \ + "https://api.github.com/repos/${{ github.repository }}/issues/comments/$comment_id") else + # 创建新评论 echo "Creating new comment" - curl -s -X POST \ + response=$(curl -s -X POST \ -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ - -d "$(jq -n --arg body "$(cat $GITHUB_WORKSPACE/review_data.md)" '{body: $body}')" \ - "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" + -d "$(jq -n --arg body "$(cat review_data.md)" '{body: $body}')" \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments") fi - echo "DEBUG: Comment posted/updated" >&2 - - name: Get Comment Time id: get_comment_time run: | - current_time=$(date -u +"%s") - echo "CURRENT_TIME=${current_time}" >> $GITHUB_OUTPUT - cp $GITHUB_WORKSPACE/unique_reviewers.txt $GITHUB_WORKSPACE/unique_reviewers_bak.txt - cp $GITHUB_WORKSPACE/unique_tags.txt $GITHUB_WORKSPACE/unique_tags_bak.txt - echo "DEBUG: Comment time: $current_time" >&2 - - - name: Save Reviewers Cache + existing_comment=$(curl -s \ + "https://api.github.com/repos/${{ github.repository }}/issues/${{ steps.extract-pr.outputs.PR_NUMBER }}/comments" | \ + jq -r '.[] | select(.user.login == "github-actions[bot]") | {body: .body} | @base64') + comment_body="${{ steps.get_approval.outputs.CURRENT_TIME }}" + comment_time=$(date -d "$comment_body" +%s) + echo "CURRENT_TIME=${comment_time}" >> $GITHUB_OUTPUT + cp unique_reviewers.txt unique_reviewers_bak.txt + cp unique_tags.txt unique_tags_bak.txt + - name: Restore Reviewers Save id: reviewers-cache-save uses: actions/cache/save@v4 with: path: | - $GITHUB_WORKSPACE/unique_tags_bak.txt - $GITHUB_WORKSPACE/unique_reviewers_bak.txt - key: ${{ runner.os }}-auto-assign-reviewers-${{ steps.extract-pr.outputs.PR_NUMBER }}-${{ steps.get_comment_time.outputs.CURRENT_TIME }} \ No newline at end of file + 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.CURRENT_TIME }}