-
Notifications
You must be signed in to change notification settings - Fork 38
83 lines (72 loc) · 2.69 KB
/
check_code_format.yml
File metadata and controls
83 lines (72 loc) · 2.69 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
name: Check code formatting
on:
workflow_dispatch:
pull_request:
branches:
- main
env:
BUILD_TYPE: Debug
jobs:
format_check:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Install clang-format
uses: aminya/setup-cpp@v1
with:
clang-format: true
- name: Check clang-format version
run: clang-format --version
- name: Check formatting in repo
id: fmt
continue-on-error: ${{ github.event_name == 'workflow_dispatch' }}
run: |
echo "Checking formatting in repo"
rm -f /tmp/clang_format_errors.txt
if [ "$GITHUB_EVENT_NAME" = "pull_request" ]; then
echo "Pull request detected. Running clang-format on changed files only."
base_sha="${{ github.event.pull_request.base.sha }}"
head_sha="${{ github.event.pull_request.head.sha }}"
changed=$(git diff --name-only "$base_sha" "$head_sha" | grep -E '\.(cpp|h|hpp)$' || true)
else
echo "Non-PR event. Running clang-format on all files."
changed=$(find . -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \))
fi
# Process each file and check formatting
printf '%s\n' "$changed" | while IFS= read -r f; do
if [ -n "$f" ]; then
if ! clang-format -Werror --dry-run --ferror-limit=1 "$f" 2>/dev/null; then
echo "::warning file=$f::Formatting issues in $f"
echo "- $f" >> /tmp/clang_format_errors.txt
fi
fi
done
ERRORS=$(cat /tmp/clang_format_errors.txt 2>/dev/null)
echo "errors<<EOF" >> $GITHUB_OUTPUT
echo -e "$ERRORS" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
echo "DEBUG: Contents of GITHUB_OUTPUT file:"
cat "$GITHUB_OUTPUT"
if [ "$GITHUB_EVENT_NAME" = "workflow_dispatch" ]; then
exit 0
else
if [ -n "$ERRORS" ]; then
exit 1
else
exit 0
fi
fi
- name: Post comment on PR
if: ${{ github.event_name == 'pull_request' && steps.fmt.outputs.errors != '' }}
uses: actions/github-script@v6
with:
script: |
const err = `${{ steps.fmt.outputs.errors }}`;
const msg = `### Code Formatting Issues\nThe following files do not adhere to the clang-format style:\n${err}`;
github.rest.issues.createComment({
issue_number: context.issue.number,
owner: context.repo.owner,
repo: context.repo.repo,
body: msg
});