blob: b94ee511031a1d2005f6507d7309b003acc9aadd (
plain)
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
|
name: Code Format with Clang-Format
on:
workflow_dispatch:
inputs:
branch:
description: "PR 分支名称\nPR branch name"
required: true
type: string
exclude_patterns:
description: "排除文件/目录 (以逗号间隔)\n Files/Directories to exclude(comma-separated)"
required: false
default: ''
concurrency:
group: ${{ github.workflow }}-${{ inputs.branch || github.ref }}
cancel-in-progress: true
permissions:
contents: write
pull-requests: read
jobs:
format-code:
if: |
github.repository_owner != 'RT-Thread'
runs-on: ubuntu-latest
env:
HEAD_OWNER: ${{ github.repository_owner }}
SOURCE_REPOSITORY: ${{ github.repository }}
TARGET_BRANCH: ${{ inputs.branch }}
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
ref: ${{ inputs.branch }}
fetch-depth: 0
token: ${{ secrets.GITHUB_TOKEN }}
lfs: false
- name: Install clang-format
run: sudo apt-get update && sudo apt-get install -y clang-format
- name: Check clang-format version
run: |
echo "📋 clang-format version information:"
clang-format --version
echo "📋 Detailed version info:"
clang-format -version
# 检查支持的功能
echo "📋 Checking supported features..."
clang-format --help | grep -i "align\|consecutive" || echo "No align/consecutive options found"
- name: Get PR info and changed files
id: get-pr-info
run: |
max_retries=3
retry_count=0
changed_files=""
api_response=""
# fork 查询父仓库中的 PR,独立镜像则查询当前仓库
repository=$(curl -sS \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
"https://api.github.com/repos/${SOURCE_REPOSITORY}")
UPSTREAM_REPOSITORY=$(jq -r '.parent.full_name // .full_name // empty' <<<"$repository")
if [ -z "$UPSTREAM_REPOSITORY" ]; then
echo "Error: Failed to determine the pull request repository"
echo "API response: $repository"
exit 1
fi
echo "Using PR repository: $UPSTREAM_REPOSITORY"
echo "upstream_repository=$UPSTREAM_REPOSITORY" >> "$GITHUB_OUTPUT"
# 根据当前仓库所有者和所选分支自动查找上游 PR
pull_requests=$(curl -sS -G \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github+json" \
--data-urlencode "state=open" \
--data-urlencode "head=${HEAD_OWNER}:${TARGET_BRANCH}" \
"https://api.github.com/repos/${UPSTREAM_REPOSITORY}/pulls")
if ! jq -e 'type == "array"' <<<"$pull_requests" >/dev/null; then
echo "Error: Failed to query the upstream pull request"
echo "API response: $pull_requests"
exit 1
fi
pr_count=$(jq 'length' <<<"$pull_requests")
if [ "$pr_count" -ne 1 ]; then
echo "Error: Expected exactly one open PR for ${HEAD_OWNER}:${TARGET_BRANCH}, found $pr_count"
jq -r '.[] | "PR #\(.number): \(.html_url)"' <<<"$pull_requests"
exit 1
fi
PR_NUMBER=$(jq -r '.[0].number' <<<"$pull_requests")
PR_AUTHOR=$(jq -r '.[0].user.login' <<<"$pull_requests")
PR_AUTHOR_ID=$(jq -r '.[0].user.id' <<<"$pull_requests")
PR_AUTHOR_EMAIL="${PR_AUTHOR_ID}+${PR_AUTHOR}@users.noreply.github.com"
echo "Matched upstream PR #$PR_NUMBER"
echo "pr_number=$PR_NUMBER" >> "$GITHUB_OUTPUT"
echo "pr_author=$PR_AUTHOR" >> "$GITHUB_OUTPUT"
echo "pr_author_email=$PR_AUTHOR_EMAIL" >> "$GITHUB_OUTPUT"
echo "Fetching changed files for PR #$PR_NUMBER..."
while [ $retry_count -lt $max_retries ]; do
# 使用一个curl调用同时获取响应内容和状态码
api_response=$(curl -s -w "\n%{http_code}" \
-H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \
-H "Accept: application/vnd.github.v3+json" \
"https://api.github.com/repos/${UPSTREAM_REPOSITORY}/pulls/$PR_NUMBER/files")
# 分离HTTP状态码和响应内容
http_status=$(echo "$api_response" | tail -1)
api_response=$(echo "$api_response" | sed '$d')
echo "HTTP Status: $http_status"
# 检查HTTP状态码
if [ "$http_status" -ne 200 ]; then
echo "Retry $((retry_count+1)): HTTP $http_status - API response error"
echo "API Response: $api_response"
sleep 5
((retry_count++))
continue
fi
# 验证响应是否为有效JSON且包含文件数组
if jq -e 'if type=="array" then .[0].filename else empty end' <<<"$api_response" >/dev/null 2>&1; then
changed_files=$(jq -r '.[].filename' <<<"$api_response")
break
else
echo "Retry $((retry_count+1)): API response not ready or invalid format"
echo "API Response: $api_response"
sleep 5
((retry_count++))
fi
done
if [ -z "$changed_files" ]; then
echo "Error: Failed to get changed files after $max_retries attempts"
echo "Final API Response: $api_response"
exit 1
fi
# 将文件列表转换为逗号分隔格式
changed_files_comma=$(echo "$changed_files" | tr '\n' ',' | sed 's/,$//')
echo "Successfully fetched $(echo "$changed_files" | wc -l) changed files"
# 设置输出
echo "all_changed_files=$changed_files_comma" >> $GITHUB_OUTPUT
echo "changed_files_count=$(echo "$changed_files" | wc -l)" >> $GITHUB_OUTPUT
- name: Find source files to format
id: find-files
run: |
# 获取PR中修改的文件
CHANGED_FILES="${{ steps.get-pr-info.outputs.all_changed_files }}"
# 将逗号分隔的文件列表转换为换行分隔
CHANGED_FILES_LINES=$(echo "$CHANGED_FILES" | tr ',' '\n')
# 美化打印PR中修改的文件
echo "📋 PR中修改的文件列表:"
echo "┌───────────────────────────────────────────────────────"
count=1
while IFS= read -r file; do
if [ -n "$file" ]; then
echo "│ $count. $file"
((count++))
fi
done <<< "$CHANGED_FILES_LINES"
echo "└───────────────────────────────────────────────────────"
echo "总共修改了 $((count-1)) 个文件"
# 如果没有修改的文件,退出
if [ -z "$CHANGED_FILES" ]; then
echo "❌ PR中没有修改的文件"
echo "files_count=0" >> $GITHUB_OUTPUT
exit 0
fi
# 继续使用CHANGED_FILES进行后续处理
CHANGED_FILES="$CHANGED_FILES_LINES"
# 过滤出需要格式化的源文件(扩展clang-format支持的文件类型)
FILES=""
while IFS= read -r file; do
if [ -n "$file" ] && [[ "$file" =~ \.(cpp|h|c|hpp|cc|hh|C|H|cp|cxx|hxx|inc|inl|ipp|tpp|txx)$ ]]; then
FILES="$FILES$file"$'\n'
fi
done <<< "$CHANGED_FILES"
FILES=$(echo "$FILES" | sort | uniq)
# 处理排除模式
EXCLUDE_PATTERNS="${{ github.event.inputs.exclude_patterns }}"
if [ -n "$EXCLUDE_PATTERNS" ] && [ -n "$FILES" ]; then
IFS=',' read -ra PATTERNS <<< "$EXCLUDE_PATTERNS"
for pattern in "${PATTERNS[@]}"; do
pattern=$(echo "$pattern" | xargs) # 去除空格
if [ -n "$pattern" ]; then
# 去除末尾的斜杠(如果有)
pattern=${pattern%/}
echo "排除模式: $pattern"
# 使用 grep 过滤排除模式
FILES=$(echo "$FILES" | grep -v "$pattern" || echo "$FILES")
fi
done
fi
if [ -z "$FILES" ]; then
echo "❌ 没有需要格式化的文件(可能都被排除了)"
echo "files_count=0" >> $GITHUB_OUTPUT
exit 0
fi
# 显示找到的文件用于调试
echo "🎯 需要格式化的文件:"
echo "┌───────────────────────────────────────────────────────"
count=1
while IFS= read -r file; do
if [ -n "$file" ]; then
echo "│ $count. $file"
((count++))
fi
done <<< "$FILES"
echo "└───────────────────────────────────────────────────────"
FILE_COUNT=$(echo "$FILES" | wc -l)
echo "找到 $FILE_COUNT 个需要格式化的文件"
echo "files_count=$FILE_COUNT" >> $GITHUB_OUTPUT
# 将文件列表保存为多行输出
echo "files_list<<EOF" >> $GITHUB_OUTPUT
echo "$FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Clean up temporary files
run: |
rm -f changed_files.txt
rm -f format_files.sh
echo "✅ 临时文件清理完成"
- name: Format code with clang-format
if: steps.find-files.outputs.files_count != '0'
run: |
echo "开始格式化代码..."
FILES="${{ steps.find-files.outputs.files_list }}"
# 使用clang-format批量格式化文件
echo "$FILES" | xargs -I {} sh -c '
file="{}"
if [ -f "$file" ]; then
echo "📝 格式化: $file"
clang-format -style=file -i "$file"
if [ $? -eq 0 ]; then
echo "✅ 格式化成功: $file"
else
echo "❌ 格式化失败: $file"
exit 1
fi
else
echo "⚠️ 文件不存在: $file"
fi
'
echo "✅ 代码格式化完成"
- name: Check for changes
id: check-changes
run: |
if git diff --quiet; then
echo "✅ 代码无需格式化"
echo "has_changes=false" >> $GITHUB_OUTPUT
else
echo "📋 检测到格式化更改:"
git diff --name-only
echo "has_changes=true" >> $GITHUB_OUTPUT
fi
- name: Commit and push changes
if: steps.check-changes.outputs.has_changes == 'true'
run: |
PR_AUTHOR="${{ steps.get-pr-info.outputs.pr_author }}"
PR_AUTHOR_EMAIL="${{ steps.get-pr-info.outputs.pr_author_email }}"
git config --local user.name "$PR_AUTHOR"
git config --local user.email "$PR_AUTHOR_EMAIL"
git add -A
git commit -m "style: format code with clang-format"
git push origin "HEAD:refs/heads/${TARGET_BRANCH}"
echo "✅ 格式化结果已作为独立提交推送到分支 ${TARGET_BRANCH}"
- name: Summary
run: |
echo "=== 格式化总结 ==="
echo "分支: ${TARGET_BRANCH}"
echo "PR 仓库: ${{ steps.get-pr-info.outputs.upstream_repository }}"
echo "上游 PR: #${{ steps.get-pr-info.outputs.pr_number }}"
echo "排除模式: ${{ github.event.inputs.exclude_patterns || '无' }}"
echo "处理文件数: ${{ steps.find-files.outputs.files_count }}"
echo "有更改: ${{ steps.check-changes.outputs.has_changes }}"
echo "clang-format 版本: $(clang-format --version | head -1)"
|