Skip to content

Commit 4ca97e5

Browse files
kurisaWRbb666
authored andcommitted
[ci][format] fix the issue of CLA verification interception
1 parent f78bef1 commit 4ca97e5

File tree

1 file changed

+46
-19
lines changed

1 file changed

+46
-19
lines changed

.github/workflows/pr_clang_format.yml

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -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')
@@ -250,13 +268,22 @@ jobs:
250268
- name: Commit and push changes
251269
if: steps.check-changes.outputs.has_changes == 'true'
252270
run: |
253-
git config --local user.email "github-actions[bot]@users.noreply.github.com"
254-
git config --local user.name "github-actions[bot]"
255-
271+
# 使用PR作者作为commit author,避免CLA检查失败
272+
# CLA检查只识别PR作者(已签署CLA),而不识别github-actions[bot]
273+
PR_AUTHOR="${{ steps.get-pr-info.outputs.pr_author }}"
274+
PR_AUTHOR_EMAIL="${{ steps.get-pr-info.outputs.pr_author_email }}"
275+
276+
git config --local user.email "$PR_AUTHOR_EMAIL"
277+
git config --local user.name "$PR_AUTHOR"
278+
279+
# 使用GIT_AUTHOR环境变量让commit以贡献者名义提交
280+
export GIT_AUTHOR_NAME="$PR_AUTHOR"
281+
export GIT_AUTHOR_EMAIL="$PR_AUTHOR_EMAIL"
282+
256283
git add -A
257284
git commit -m "style: format code with clang-format [skip ci]"
258285
git push origin HEAD:${{ github.event.inputs.branch }}
259-
286+
260287
echo "✅ 代码格式化完成并已推送到分支 ${{ github.event.inputs.branch }}"
261288
262289
- name: Summary

0 commit comments

Comments
 (0)