Skip to content

Check code formatting #10

Check code formatting

Check code formatting #10

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
find . -type f \( -name '*.cpp' -o -name '*.h' -o -name '*.hpp' \) \
-exec bash -c '
f="$1"
echo "Processing file: $f"
diff=$(clang-format --fallback-style=LLVM --style=file $f | diff -q $f -)
if [ -n "$diff" ]; then
echo "::warning file=$f::Formatting issues found"
echo "- $f" >> /tmp/clang_format_errors.txt
fi
' bash {} \;
ERRORS=$(cat /tmp/clang_format_errors.txt 2>/dev/null)
echo "DEBUG: Accumulated errors:"
echo "$ERRORS"
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
});
- name: Create Annotations for Formatting Issues
if: ${{ github.event_name == 'workflow_dispatch' && steps.fmt.outputs.errors != '' }}
uses: actions/github-script@v6
with:
script: |
const errors = `${{ steps.fmt.outputs.errors }}`.trim()
.split('\n')
.filter(e => e);
if (errors.length === 0) return;
github.rest.checks.create({
owner: context.repo.owner,
repo: context.repo.repo,
name: 'Check code formatting',
head_sha: context.sha,
status: 'completed',
conclusion: 'failure',
output: {
title: 'Formatting Issues',
summary: `Formatting issues detected in the following files:\n${errors.join('\n')}`,
annotations: errors.map(f => ({
path: f.replace(/^- /, ''),
start_line: 1,
end_line: 1,
annotation_level: 'failure',
message: 'Formatting issues found'
}))
}
});