Merge pull request #281 from valeriof7/feature/github_action #1
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Clang-Format Check and Fix | |
on: | |
push: | |
branches: | |
- develop | |
jobs: | |
format-check: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout Code | |
uses: actions/checkout@v3 | |
with: | |
fetch-depth: 2 # Fetch enough history for PR diffing | |
- name: Install Clang-Format | |
run: sudo apt-get install -y clang-format | |
- name: Display Clang-Format Version | |
run: clang-format --version | |
- name: Identify Changed Files in PR | |
run: | | |
echo "Changed files in PR:" | |
git diff --name-only --diff-filter=ACM origin/${{ github.base_ref }}...${{ github.head_ref }} | grep -E '\.(cpp|hpp|c|h)$' || echo "No matching files changed." | |
- name: Run Clang-Format on Changed Files | |
run: | | |
git diff --name-only --diff-filter=ACM origin/${{ github.base_ref }}...${{ github.head_ref }} | grep -E '\.(cpp|hpp|c|h)$' | xargs -r clang-format -style=file -i | |
- name: Check for Formatting Differences | |
id: check_diff | |
run: git diff --exit-code || echo "Formatting issues detected." | |
- name: Auto-commit Formatting Changes | |
if: failure() | |
run: | | |
git config --local user.name "github-actions[bot]" | |
git config --local user.email "github-actions[bot]@users.noreply.github.com" | |
git add . | |
git commit -m "Apply clang-format" | |
git push origin HEAD:${{ github.head_ref }} | |
- name: Report Success or Failure | |
run: | | |
if [ "${{ steps.check_diff.outcome }}" == "failure" ]; then | |
echo "Formatting issues were detected and fixed." | |
else | |
echo "Code is properly formatted." | |
fi |