From f08f86f7a2dea107d198cb4211a480a40bd6886a Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Wed, 18 Sep 2024 15:21:55 +0800 Subject: [PATCH 01/12] feat: implement comment-check workflows. --- .github/workflows/comment-check.yml | 54 +++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 .github/workflows/comment-check.yml diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml new file mode 100644 index 00000000000..470da4ced23 --- /dev/null +++ b/.github/workflows/comment-check.yml @@ -0,0 +1,54 @@ +name: Non-English Comments Check + +on: + pull_request: + branches: + - main + workflow_dispatch: + +jobs: + non-english-comments-check: + runs-on: ubuntu-latest + + env: + # need ignore Dirs + EXCLUDE_DIRS: ".git docs tests scripts assets node_modules build" + # need ignore Files + EXCLUDE_FILES: "*.md *.txt *.html *.css *.min.js *.mdx" + + steps: + - uses: actions/checkout@v4 + + - name: Search for Non-English comments + run: | + set -e + # Define the regex pattern to match Chinese characters + pattern='[\p{Han}]' + + # Process the directories to be excluded + exclude_dirs="" + for dir in $EXCLUDE_DIRS; do + exclude_dirs="$exclude_dirs --exclude-dir=$dir" + done + + # Process the file types to be excluded + exclude_files="" + for file in $EXCLUDE_FILES; do + exclude_files="$exclude_files --exclude=$file" + done + + # Use grep to find all comments containing Non-English characters and save to file + grep -Pnr "$pattern" . $exclude_dirs $exclude_files > non_english_comments.txt || true + + - name: Output non-English comments are found + run: | + if [ -s non_english_comments.txt ]; then + echo "Non-English comments found in the following locations:" + cat non_english_comments.txt + exit 1 # terminate the workflow + else + echo "No Non_English comments found." + fi + + # 如果在此处存在非英文注释,将会终止CI流程并报错 + # 里面会提示具体文件以及对应行数,方便快速定位 \ No newline at end of file From 8ca4ef3600d6d18cb4c7e9f655ba4a3ac8ace4ac Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Wed, 18 Sep 2024 15:42:21 +0800 Subject: [PATCH 02/12] fix uncorrect effect. --- .github/workflows/comment-check.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index 470da4ced23..d7f3cbd8feb 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -36,6 +36,9 @@ jobs: for file in $EXCLUDE_FILES; do exclude_files="$exclude_files --exclude=$file" done + + # .github/pull_request_template.md need to specify handling + exclude_files="$exclude_files --exclude=.github/pull_request_template.md" # Use grep to find all comments containing Non-English characters and save to file grep -Pnr "$pattern" . $exclude_dirs $exclude_files > non_english_comments.txt || true From 573b8e805623fe9c2196319ab4710af8716e277f Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Wed, 18 Sep 2024 15:56:34 +0800 Subject: [PATCH 03/12] fix: exclude pull request template.md from comment check workflow --- .github/workflows/comment-check.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index d7f3cbd8feb..fff5db65ccf 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -36,12 +36,9 @@ jobs: for file in $EXCLUDE_FILES; do exclude_files="$exclude_files --exclude=$file" done - - # .github/pull_request_template.md need to specify handling - exclude_files="$exclude_files --exclude=.github/pull_request_template.md" # Use grep to find all comments containing Non-English characters and save to file - grep -Pnr "$pattern" . $exclude_dirs $exclude_files > non_english_comments.txt || true + grep -Pnr "$pattern" . $exclude_dirs $exclude_files --exclude='.github/pull_request_template.md' > non_english_comments.txt || true - name: Output non-English comments are found run: | From 5035f25571891926fe962516c87293a9576f8407 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Wed, 18 Sep 2024 16:02:48 +0800 Subject: [PATCH 04/12] refactor write logic. --- .github/workflows/comment-check.yml | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index fff5db65ccf..0f008aa2fee 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -11,10 +11,10 @@ jobs: runs-on: ubuntu-latest env: - # need ignore Dirs + # Directories to be excluded EXCLUDE_DIRS: ".git docs tests scripts assets node_modules build" - # need ignore Files - EXCLUDE_FILES: "*.md *.txt *.html *.css *.min.js *.mdx" + # Files to be excluded + EXCLUDE_FILES: "*.txt *.html *.css *.min.js *.mdx" steps: - uses: actions/checkout@v4 @@ -37,17 +37,25 @@ jobs: exclude_files="$exclude_files --exclude=$file" done - # Use grep to find all comments containing Non-English characters and save to file - grep -Pnr "$pattern" . $exclude_dirs $exclude_files --exclude='.github/pull_request_template.md' > non_english_comments.txt || true - - - name: Output non-English comments are found + # Use grep to find all comments containing Non-English characters + # Check if the file contains '.md' and skip if it does + grep -Pnr "$pattern" . $exclude_dirs $exclude_files | while read -r line ; do + # If the filename contains '.md', skip it + if echo "$line" | grep -q '\.md'; then + continue + fi + # Write the result to the non_english_comments.txt file + echo "$line" >> non_english_comments.txt + done || true + + - name: Output non-English comments if found run: | if [ -s non_english_comments.txt ]; then echo "Non-English comments found in the following locations:" cat non_english_comments.txt exit 1 # terminate the workflow else - echo "No Non_English comments found." + echo "No Non-English comments found." fi # 如果在此处存在非英文注释,将会终止CI流程并报错 From 1b298272686dd9ff27d905a3c613d15d2837ed3a Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Sep 2024 11:18:27 +0800 Subject: [PATCH 05/12] refactor: update diff file check logic and comment entire repo logic. feat: add bot output comment when failed. --- .github/workflows/comment-check.yml | 137 +++++++++++++++++++++++----- 1 file changed, 112 insertions(+), 25 deletions(-) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index 0f008aa2fee..c36b4e4a24c 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -14,39 +14,106 @@ jobs: # Directories to be excluded EXCLUDE_DIRS: ".git docs tests scripts assets node_modules build" # Files to be excluded - EXCLUDE_FILES: "*.txt *.html *.css *.min.js *.mdx" + EXCLUDE_FILES: ".md .txt .html .css .min.js .mdx" steps: - uses: actions/checkout@v4 + with: + fetch-depth: 0 - - name: Search for Non-English comments + # - name: Search for Non-English comments in the entire repository + # run: | + # set -e + # # Define the regex pattern to match Chinese characters + # pattern='[\p{Han}]' + + # # Use find to get all files in the repository + # all_files=$(find . -type f) + + # # Loop over each file in the repository + # for file in $all_files; do + # # Skip files in excluded directories + # skip_file=false + # for dir in ${EXCLUDE_DIRS}; do + # if [[ "$file" == ./$dir/* ]]; then + # skip_file=true + # break + # fi + # done + + # # Skip files matching excluded patterns + # for file_pattern in ${EXCLUDE_FILES}; do + # if [[ "$file" == *$file_pattern ]]; then + # skip_file=true + # break + # fi + # done + + # # If the file matches any exclude pattern, skip it + # if [ "$skip_file" = true ]; then + # continue + # fi + + # # Use grep to find all comments containing Non-English characters in filtered files + # grep_output=$(grep -PnH "$pattern" "$file" || true) + # if [ -n "$grep_output" ]; then + # # Insert a tab after the line number, keeping the colon between the file path and line number + # formatted_output=$(echo "$grep_output" | sed 's/^\(.*:[0-9]\+\):/\1\t/') + # echo "$formatted_output" >> non_english_comments.txt # Save to file + # fi + # done + + - name: Search for Non-English comments in PR diff files run: | set -e # Define the regex pattern to match Chinese characters pattern='[\p{Han}]' - - # Process the directories to be excluded - exclude_dirs="" - for dir in $EXCLUDE_DIRS; do - exclude_dirs="$exclude_dirs --exclude-dir=$dir" - done - - # Process the file types to be excluded - exclude_files="" - for file in $EXCLUDE_FILES; do - exclude_files="$exclude_files --exclude=$file" - done - - # Use grep to find all comments containing Non-English characters - # Check if the file contains '.md' and skip if it does - grep -Pnr "$pattern" . $exclude_dirs $exclude_files | while read -r line ; do - # If the filename contains '.md', skip it - if echo "$line" | grep -q '\.md'; then + + # Get the list of files changed in this PR compared to the base branch + changed_files=$(git diff --name-only ${{ github.event.pull_request.base.sha }}) + + # Loop over each changed file + for file in $changed_files; do + # Skip files in excluded directories + skip_file=false + for dir in ${EXCLUDE_DIRS}; do + if [[ "$file" == ./$dir/* ]]; then + skip_file=true + break + fi + done + + # Skip files matching excluded patterns + for file_pattern in ${EXCLUDE_FILES}; do + if [[ "$file" == *$file_pattern ]]; then + skip_file=true + break + fi + done + + # If the file matches any exclude pattern, skip it + if [ "$skip_file" = true ]; then continue fi - # Write the result to the non_english_comments.txt file - echo "$line" >> non_english_comments.txt - done || true + + # Use grep to find all comments containing Non-English characters in filtered files + grep_output=$(grep -PnH "$pattern" "$file" || true) + if [ -n "$grep_output" ]; then + # Insert a tab after the line number, keeping the colon between the file path and line number + formatted_output=$(echo "$grep_output" | sed 's/^\(.*:[0-9]\+\):/\1\t/') + echo "$formatted_output" >> non_english_comments.txt # Save to file + fi + done + + - name: Store non-English comments in ENV + run: | + # Store the entire content of non_english_comments.txt into an environment variable + if [ -f non_english_comments.txt ]; then + NON_ENGLISH_COMMENTS=$(cat non_english_comments.txt) + echo "NON_ENGLISH_COMMENTS<> $GITHUB_ENV + echo "$NON_ENGLISH_COMMENTS" >> $GITHUB_ENV + echo "EOF" >> $GITHUB_ENV + fi - name: Output non-English comments if found run: | @@ -58,5 +125,25 @@ jobs: echo "No Non-English comments found." fi - # 如果在此处存在非英文注释,将会终止CI流程并报错 - # 里面会提示具体文件以及对应行数,方便快速定位 \ No newline at end of file + - name: Find Comment + if: failure() + uses: peter-evans/find-comment@v3.1.0 + id: fc + with: + issue-number: ${{ github.event.pull_request.number }} + comment-author: 'github-actions[bot]' + body-includes: Non-English comments were found in the following locations + + - name: Comment on PR if errors found + if: failure() # This step runs only if the previous step fails + uses: peter-evans/create-or-update-comment@v4.0.0 + with: + token: ${{ secrets.GITHUB_TOKEN }} + issue-number: ${{ github.event.pull_request.number }} + comment-id: ${{ steps.fc.outputs.comment-id }} + edit-mode: replace + body: | + ⚠️ Non-English comments were found in the following locations: + ``` + ${{ env.NON_ENGLISH_COMMENTS }} + ``` From 69b6e3fcf7adda32c0e3b612ff1c33104ac2e25e Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Sep 2024 11:27:01 +0800 Subject: [PATCH 06/12] add test effect. --- .github/workflows/comment-check.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index c36b4e4a24c..c1d6efc1412 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -147,3 +147,7 @@ jobs: ``` ${{ env.NON_ENGLISH_COMMENTS }} ``` + + + # 测试一下 如果当前提交的PR内的非Excluded文件中有中文注释, + # 那么这个workflow会失败,同时会在PR内添加一条评论,告知用户有中文注释 \ No newline at end of file From 607c0cc4374c5e5296bdccc5f6fec577f53cddd5 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Sep 2024 11:31:23 +0800 Subject: [PATCH 07/12] update bot permissions --- .github/workflows/comment-check.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index c1d6efc1412..e308c025de0 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -9,6 +9,8 @@ on: jobs: non-english-comments-check: runs-on: ubuntu-latest + permissions: + pull-requests: write env: # Directories to be excluded From 0132855b59cdfe89bd79ec4528936f0c63348c8e Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Sep 2024 11:32:59 +0800 Subject: [PATCH 08/12] update kratos offical bot token --- .github/workflows/comment-check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index e308c025de0..93d8c86762d 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -140,7 +140,7 @@ jobs: if: failure() # This step runs only if the previous step fails uses: peter-evans/create-or-update-comment@v4.0.0 with: - token: ${{ secrets.GITHUB_TOKEN }} + token: ${{ secrets.BOT_GITHUB_TOKEN }} issue-number: ${{ github.event.pull_request.number }} comment-id: ${{ steps.fc.outputs.comment-id }} edit-mode: replace From d843adb2503638280629757abf0dcc5962d97731 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Sep 2024 11:35:10 +0800 Subject: [PATCH 09/12] remove token --- .github/workflows/comment-check.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index 93d8c86762d..9eb6716b056 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -140,7 +140,6 @@ jobs: if: failure() # This step runs only if the previous step fails uses: peter-evans/create-or-update-comment@v4.0.0 with: - token: ${{ secrets.BOT_GITHUB_TOKEN }} issue-number: ${{ github.event.pull_request.number }} comment-id: ${{ steps.fc.outputs.comment-id }} edit-mode: replace From 69697a199d1454907702c2a92d49fc5ed9fcbda8 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Sep 2024 11:38:27 +0800 Subject: [PATCH 10/12] update token an permissions --- .github/workflows/comment-check.yml | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index 9eb6716b056..c9c8e08a18f 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -10,6 +10,7 @@ jobs: non-english-comments-check: runs-on: ubuntu-latest permissions: + issues: write pull-requests: write env: @@ -140,6 +141,7 @@ jobs: if: failure() # This step runs only if the previous step fails uses: peter-evans/create-or-update-comment@v4.0.0 with: + token: ${{ secrets.GITHUB_TOKEN }} issue-number: ${{ github.event.pull_request.number }} comment-id: ${{ steps.fc.outputs.comment-id }} edit-mode: replace From acd1bdb4247c59cf0babd990a5f513bcde08899a Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Fri, 20 Sep 2024 11:45:27 +0800 Subject: [PATCH 11/12] use kratos-ci-bot --- .github/workflows/comment-check.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index c9c8e08a18f..ca23ca4f336 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -141,7 +141,7 @@ jobs: if: failure() # This step runs only if the previous step fails uses: peter-evans/create-or-update-comment@v4.0.0 with: - token: ${{ secrets.GITHUB_TOKEN }} + token: ${{ secrets.BOT_GITHUB_TOKEN }} issue-number: ${{ github.event.pull_request.number }} comment-id: ${{ steps.fc.outputs.comment-id }} edit-mode: replace From d4033f098a9ef1ba50c7178739d67d3e76320450 Mon Sep 17 00:00:00 2001 From: Monet Lee Date: Sat, 21 Sep 2024 13:48:52 +0800 Subject: [PATCH 12/12] Refactor comment-check workflow to include pull_request_target event and update permissions --- .github/workflows/comment-check.yml | 30 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 16 deletions(-) diff --git a/.github/workflows/comment-check.yml b/.github/workflows/comment-check.yml index ca23ca4f336..699d6aaf4c2 100644 --- a/.github/workflows/comment-check.yml +++ b/.github/workflows/comment-check.yml @@ -1,16 +1,17 @@ name: Non-English Comments Check on: - pull_request: + pull_request_target: + types: [opened, synchronize, reopened] branches: - main - workflow_dispatch: + # workflow_dispatch: jobs: non-english-comments-check: runs-on: ubuntu-latest permissions: - issues: write + contents: read pull-requests: write env: @@ -22,7 +23,9 @@ jobs: steps: - uses: actions/checkout@v4 with: - fetch-depth: 0 + ref: ${{ github.event.pull_request.head.ref }} + repository: ${{ github.event.pull_request.head.repo.full_name }} + fetch-depth: 0 # - name: Search for Non-English comments in the entire repository # run: | @@ -73,7 +76,7 @@ jobs: pattern='[\p{Han}]' # Get the list of files changed in this PR compared to the base branch - changed_files=$(git diff --name-only ${{ github.event.pull_request.base.sha }}) + changed_files=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }}) # Loop over each changed file for file in $changed_files; do @@ -107,7 +110,7 @@ jobs: echo "$formatted_output" >> non_english_comments.txt # Save to file fi done - + - name: Store non-English comments in ENV run: | # Store the entire content of non_english_comments.txt into an environment variable @@ -126,19 +129,18 @@ jobs: exit 1 # terminate the workflow else echo "No Non-English comments found." - fi - + - name: Find Comment - if: failure() + if: failure() && github.event_name != 'workflow_dispatch' uses: peter-evans/find-comment@v3.1.0 id: fc with: issue-number: ${{ github.event.pull_request.number }} - comment-author: 'github-actions[bot]' + comment-author: "kratos-ci-bot" body-includes: Non-English comments were found in the following locations - + - name: Comment on PR if errors found - if: failure() # This step runs only if the previous step fails + if: failure() && github.event_name != 'workflow_dispatch' # This step runs only if the previous step fails uses: peter-evans/create-or-update-comment@v4.0.0 with: token: ${{ secrets.BOT_GITHUB_TOKEN }} @@ -150,7 +152,3 @@ jobs: ``` ${{ env.NON_ENGLISH_COMMENTS }} ``` - - - # 测试一下 如果当前提交的PR内的非Excluded文件中有中文注释, - # 那么这个workflow会失败,同时会在PR内添加一条评论,告知用户有中文注释 \ No newline at end of file