Skip to content

Commit 2f5e5d8

Browse files
authored
Update pr_clang_format.yml
1 parent 73f1651 commit 2f5e5d8

1 file changed

Lines changed: 82 additions & 33 deletions

File tree

.github/workflows/pr_clang_format.yml

Lines changed: 82 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ permissions:
2727
jobs:
2828
format-code:
2929
if: |
30-
github.repository_owner != 'RT-Thread'
30+
github.repository_owner != 'Nedki-L'
3131
runs-on: ubuntu-latest
3232

3333
steps:
@@ -52,37 +52,55 @@ jobs:
5252
echo "📋 Checking supported features..."
5353
clang-format --help | grep -i "align\|consecutive" || echo "No align/consecutive options found"
5454
55-
- name: Get changed files from PR
56-
id: get-pr-files
55+
- name: Get PR info (files and author)
56+
id: get-pr-info
5757
run: |
5858
max_retries=3
5959
retry_count=0
6060
changed_files=""
6161
api_response=""
62-
62+
6363
# 获取PR编号(workflow_dispatch时需要手动输入)
6464
PR_NUMBER="${{ github.event.inputs.pr_number }}"
65-
65+
6666
if [ -z "$PR_NUMBER" ]; then
6767
echo "Error: PR number is required"
6868
exit 1
6969
fi
70-
70+
71+
echo "Fetching PR info for #$PR_NUMBER..."
72+
73+
# 获取PR的详细信息(包括作者)
74+
pr_response=$(curl -s \
75+
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
76+
-H "Accept: application/vnd.github.v3+json" \
77+
"https://api.github.com/repos/RT-Thread/rt-thread/pulls/$PR_NUMBER")
78+
79+
# 获取PR作者的GitHub用户名
80+
PR_AUTHOR=$(jq -r '.user.login' <<<"$pr_response")
81+
echo "PR Author: $PR_AUTHOR"
82+
83+
# 使用GitHub noreply邮箱格式
84+
PR_AUTHOR_EMAIL="${PR_AUTHOR}+github[bot]@noreply.github.com"
85+
86+
echo "pr_author=$PR_AUTHOR" >> $GITHUB_OUTPUT
87+
echo "pr_author_email=$PR_AUTHOR_EMAIL" >> $GITHUB_OUTPUT
88+
7189
echo "Fetching changed files for PR #$PR_NUMBER..."
72-
90+
7391
while [ $retry_count -lt $max_retries ]; do
7492
# 使用一个curl调用同时获取响应内容和状态码
7593
api_response=$(curl -s -w "\n%{http_code}" \
7694
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
7795
-H "Accept: application/vnd.github.v3+json" \
7896
"https://api.github.com/repos/RT-Thread/rt-thread/pulls/$PR_NUMBER/files")
79-
97+
8098
# 分离HTTP状态码和响应内容
8199
http_status=$(echo "$api_response" | tail -1)
82100
api_response=$(echo "$api_response" | sed '$d')
83-
101+
84102
echo "HTTP Status: $http_status"
85-
103+
86104
# 检查HTTP状态码
87105
if [ "$http_status" -ne 200 ]; then
88106
echo "Retry $((retry_count+1)): HTTP $http_status - API response error"
@@ -91,7 +109,7 @@ jobs:
91109
((retry_count++))
92110
continue
93111
fi
94-
112+
95113
# 验证响应是否为有效JSON且包含文件数组
96114
if jq -e 'if type=="array" then .[0].filename else empty end' <<<"$api_response" >/dev/null 2>&1; then
97115
changed_files=$(jq -r '.[].filename' <<<"$api_response")
@@ -103,18 +121,18 @@ jobs:
103121
((retry_count++))
104122
fi
105123
done
106-
124+
107125
if [ -z "$changed_files" ]; then
108126
echo "Error: Failed to get changed files after $max_retries attempts"
109127
echo "Final API Response: $api_response"
110128
exit 1
111129
fi
112-
130+
113131
# 将文件列表转换为逗号分隔格式
114132
changed_files_comma=$(echo "$changed_files" | tr '\n' ',' | sed 's/,$//')
115-
133+
116134
echo "Successfully fetched $(echo "$changed_files" | wc -l) changed files"
117-
135+
118136
# 设置输出
119137
echo "all_changed_files=$changed_files_comma" >> $GITHUB_OUTPUT
120138
echo "changed_files_count=$(echo "$changed_files" | wc -l)" >> $GITHUB_OUTPUT
@@ -123,7 +141,7 @@ jobs:
123141
id: find-files
124142
run: |
125143
# 获取PR中修改的文件
126-
CHANGED_FILES="${{ steps.get-pr-files.outputs.all_changed_files }}"
144+
CHANGED_FILES="${{ steps.get-pr-info.outputs.all_changed_files }}"
127145
128146
# 将逗号分隔的文件列表转换为换行分隔
129147
CHANGED_FILES_LINES=$(echo "$CHANGED_FILES" | tr ',' '\n')
@@ -215,24 +233,46 @@ jobs:
215233
run: |
216234
echo "开始格式化代码..."
217235
FILES="${{ steps.find-files.outputs.files_list }}"
218-
236+
ROOT_DIR="$(pwd)"
237+
CLANG_FORMAT_CONFIG="$ROOT_DIR/.clang-format"
238+
239+
# 检查根目录的 .clang-format 是否存在
240+
if [ ! -f "$CLANG_FORMAT_CONFIG" ]; then
241+
echo "❌ 错误: 根目录找不到 .clang-format 配置文件"
242+
exit 1
243+
fi
244+
245+
echo "📋 使用配置文件: $CLANG_FORMAT_CONFIG"
246+
247+
# 注意: clang-format 会自动查找 .clang-format-ignore 文件
248+
# 它会在被格式化文件的当前目录及父目录中查找
249+
# 如果fork仓库中包含 .clang-format-ignore 文件,忽略规则会自动生效
250+
219251
# 使用clang-format批量格式化文件
220-
echo "$FILES" | xargs -I {} sh -c '
221-
file="{}"
222-
if [ -f "$file" ]; then
252+
# 使用 -style=llvm 配合 --clang-format-config 明确指定配置文件
253+
# 这确保深路径文件也使用根目录的 .clang-format 而不是子目录的
254+
format_error=0
255+
256+
while IFS= read -r file; do
257+
if [ -n "$file" ] && [ -f "$file" ]; then
223258
echo "📝 格式化: $file"
224-
clang-format -style=file -i "$file"
225-
if [ $? -eq 0 ]; then
226-
echo "✅ 格式化成功: $file"
227-
else
259+
# 使用 --style=llvm 配合 --clang-format-config 指定根目录的配置文件
260+
# 这样可以避免 clang-format 查找子目录的 .clang-format
261+
if ! clang-format --style=llvm --clang-format-config="$CLANG_FORMAT_CONFIG" -i "$file"; then
228262
echo "❌ 格式化失败: $file"
229-
exit 1
263+
format_error=1
264+
break
230265
fi
266+
echo "✅ 格式化成功: $file"
231267
else
232-
echo "⚠️ 文件不存在: $file"
268+
echo "⚠️ 文件不存在或跳过: $file"
233269
fi
234-
'
235-
270+
done <<< "$FILES"
271+
272+
if [ $format_error -ne 0 ]; then
273+
exit 1
274+
fi
275+
236276
echo "✅ 代码格式化完成"
237277
238278
- name: Check for changes
@@ -250,13 +290,22 @@ jobs:
250290
- name: Commit and push changes
251291
if: steps.check-changes.outputs.has_changes == 'true'
252292
run: |
253-
git config --local user.email "github-actions[bot]@users.noreply.github.com"
254-
git config --local user.name "github-actions[bot]"
255-
293+
# 使用PR作者作为commit author,避免CLA检查失败
294+
# CLA检查只识别PR作者(已签署CLA),而不识别github-actions[bot]
295+
PR_AUTHOR="${{ steps.get-pr-info.outputs.pr_author }}"
296+
PR_AUTHOR_EMAIL="${{ steps.get-pr-info.outputs.pr_author_email }}"
297+
298+
git config --local user.email "$PR_AUTHOR_EMAIL"
299+
git config --local user.name "$PR_AUTHOR"
300+
301+
# 使用GIT_AUTHOR环境变量让commit以贡献者名义提交
302+
export GIT_AUTHOR_NAME="$PR_AUTHOR"
303+
export GIT_AUTHOR_EMAIL="$PR_AUTHOR_EMAIL"
304+
256305
git add -A
257306
git commit -m "style: format code with clang-format [skip ci]"
258307
git push origin HEAD:${{ github.event.inputs.branch }}
259-
308+
260309
echo "✅ 代码格式化完成并已推送到分支 ${{ github.event.inputs.branch }}"
261310
262311
- name: Summary
@@ -266,4 +315,4 @@ jobs:
266315
echo "排除模式: ${{ github.event.inputs.exclude_patterns || '无' }}"
267316
echo "处理文件数: ${{ steps.find-files.outputs.files_count }}"
268317
echo "有更改: ${{ steps.check-changes.outputs.has_changes }}"
269-
echo "clang-format 版本: $(clang-format --version | head -1)"
318+
echo "clang-format 版本: $(clang-format --version | head -1)"

0 commit comments

Comments
 (0)