-
Notifications
You must be signed in to change notification settings - Fork 321
157 lines (136 loc) · 5.84 KB
/
comment-check.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
name: Non-English Comments Check
on:
pull_request_target:
types: [opened, synchronize, reopened]
branches:
- main
workflow_dispatch:
jobs:
non-english-comments-check:
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: ".md .txt .html .css .min.js .mdx"
steps:
- uses: actions/checkout@v4
with:
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: |
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}]'
# # 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 }} ${{ github.event.pull_request.head.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
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
- name: Find Comment
if: failure() && github.event_name != 'workflow_dispatch'
uses: peter-evans/[email protected]
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() && github.event_name != 'workflow_dispatch' # This step runs only if the previous step fails
uses: peter-evans/[email protected]
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 }}
```