-
Notifications
You must be signed in to change notification settings - Fork 0
86 lines (73 loc) · 2.38 KB
/
pr-format-check.yml
File metadata and controls
86 lines (73 loc) · 2.38 KB
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
name: PR Format Check
on:
pull_request:
types: [opened, synchronize, reopened]
jobs:
clang-format:
name: Clang Format
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout PR
uses: actions/checkout@v4
with:
fetch-depth: 0
submodules: recursive
- name: Install clang-format
run: |
sudo apt-get update
sudo apt-get install -y clang-format-14
- name: Check changed files
id: changed-files
run: |
# Get list of changed C/C++ files (excluding submodules)
git diff --name-only origin/${{ github.base_ref }}...HEAD | \
grep -E '\.(h|hpp|c|cc|cpp)$' | \
grep -v '^third_party/' > changed_files.txt || true
if [ -s changed_files.txt ]; then
echo "has_changes=true" >> $GITHUB_OUTPUT
echo "Changed C/C++ files:"
cat changed_files.txt
else
echo "has_changes=false" >> $GITHUB_OUTPUT
echo "No C/C++ files changed"
fi
- name: Check formatting of changed files
if: steps.changed-files.outputs.has_changes == 'true'
run: |
exit_code=0
while IFS= read -r file; do
if [ -f "$file" ]; then
echo "Checking $file..."
clang-format-14 --style=file --dry-run --Werror "$file" || {
echo "::error file=$file::File is not properly formatted"
exit_code=1
}
fi
done < changed_files.txt
if [ $exit_code -ne 0 ]; then
echo ""
echo "::error::Some files are not properly formatted."
echo "To fix, run: make format"
exit 1
fi
- name: Post PR comment on failure
if: failure() && steps.changed-files.outputs.has_changes == 'true'
uses: actions/github-script@v7
with:
script: |
const comment = `## Code Formatting Check Failed
Some files in this PR are not properly formatted according to the project's clang-format rules.
**To fix this issue:**
\`\`\`bash
make format
\`\`\`
Then commit and push the changes.`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: comment
});