Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

321 #14

Merged
merged 3 commits into from
Sep 21, 2024
Merged

321 #14

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
226 changes: 114 additions & 112 deletions .github/workflows/comment-check.yml
Original file line number Diff line number Diff line change
@@ -1,149 +1,125 @@
# name: Non-English Comments Check

# on:
# pull_request:
# branches:
# - main
# workflow_dispatch:

# jobs:
# non-english-comments-check:
# runs-on: ubuntu-latest

# env:
# # Directories to be excluded
# EXCLUDE_DIRS: ".git docs tests scripts assets node_modules build"
# # Files to be excluded (e.g., markdown, text 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

# # Use grep to find all comments containing Non-English characters
# grep -Pnr "$pattern" . $exclude_dirs | while read -r line ; do
# file_name=$(echo "$line" | cut -d':' -f1)

# # Check if the file matches any pattern in EXCLUDE_FILES
# skip_file=false
# for file_pattern in $EXCLUDE_FILES; do
# # Use echo and grep to match the file name with the pattern
# if echo "$file_name" | grep -q "$file_pattern"; then
# skip_file=true
# break
# fi
# done

# # If the file matches an 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

# - name: Output non-English comments if found
# id: check-comments
# run: |
# if [ -s non_english_comments.txt ]; then
# echo "Non-English comments found in the following locations:"
# cat non_english_comments.txt
# comments=$(cat non_english_comments.txt | sed ':a;N;$!ba;s/\n/\\n/g')
# echo "non_english_comments=$comments" >> $GITHUB_ENV
# exit 1 # terminate the workflow
# else
# echo "No Non-English comments found."
# fi

# - 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 }} # GitHub token to post the comment
# issue-number: ${{ github.event.pull_request.number }} # PR number
# edit-mode: replace # This ensures that the comment is updated instead of creating a new one
# body: |
# ⚠️ Non-English comments were found in the following locations:
# ```
# $(while IFS= read -r line; do echo "$line"; done < non_english_comments.txt)
# ```

name: Non-English Comments Check

on:
pull_request:
pull_request_target:
types: [opened, synchronize, reopened]
branches:
- main
workflow_dispatch:

jobs:
non-english-comments-check:
# if: github.event.pull_request.base.ref == 'main'
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write

env:
# 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
fetch-depth: 0

- name: Search for Non-English comments in PR files
- name: Search for Non-English comments in the entire repository
run: |
set -e
# Define the regex pattern to match Chinese characters
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 }})

# 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

# Check only the files modified in this PR for non-English comments
for file in $changed_files; do
# Skip files matching excluded patterns

# 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 file_pattern in $EXCLUDE_FILES; do
if echo "$file" | grep -q "$file_pattern"; then
for dir in ${EXCLUDE_DIRS}; do
if [[ "$file" == ./$dir/* ]]; then
skip_file=true
break
fi
done

# If the file matches an exclude pattern, skip it

# 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
grep -Pn "$pattern" "$file" >> non_english_comments.txt || 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: Search for Non-English comments in PR diff files
# run: |
# set -e
# # Define the regex pattern to match Chinese characters
# 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 }})

# # 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

# # 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<<EOF" >> $GITHUB_ENV
echo "$NON_ENGLISH_COMMENTS" >> $GITHUB_ENV
echo "EOF" >> $GITHUB_ENV
fi

- name: Output non-English comments if found
run: |
if [ -s non_english_comments.txt ]; then
Expand All @@ -153,3 +129,29 @@ jobs:
else
echo "No Non-English comments found."
fi

- 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: 'OpenIM-Robot'
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 }} # GitHub token to post the comment
token: ${{ secrets.BOT_TOKEN }}
issue-number: ${{ github.event.pull_request.number }} # PR number
comment-id: ${{ steps.fc.outputs.comment-id }}
edit-mode: replace # This ensures that the comment is updated instead of creating a new one
body: |
⚠️ Non-English comments were found in the following locations:
```
${{ env.NON_ENGLISH_COMMENTS }}
```

### 这就是中文的含金量
Loading