Skip to content

Commit

Permalink
Suggest clang-format changes on PRs (#499)
Browse files Browse the repository at this point in the history
* Apply clang-format locally and suggest changes

* Add executable permission to clang format apply
  • Loading branch information
samjwu authored Jun 13, 2024
1 parent 2f07ba5 commit bdd7971
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 21 deletions.
49 changes: 46 additions & 3 deletions .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name: Check Clang-Format on Diff

on: [push, pull_request]
on: [pull_request]

jobs:
clang-format:
Expand All @@ -23,6 +23,49 @@ jobs:
run: |
sudo apt-get install clang-format-17
- name: Check Clang-Format on Diff
- name: Fetch branches to check
run: |
./clang-format-diff.sh ${{ github.base_ref }} ${{ github.ref }}
git fetch origin ${{ github.base_ref }}
if [ "${{ github.repository_owner }}" != "${{ github.event.pull_request.head.repo.owner.login }}" ]; then
fork_owner="${{ github.event.pull_request.head.repo.owner.login }}"
fork_url=$(git remote get-url origin)
git remote add fork "$fork_url"
git fetch fork ${{ github.head_ref }}
else
git fetch origin ${{ github.head_ref }}
fi
- name: Apply clang-format locally
if: github.event_name == 'pull_request'
run: |
if [ "${{ github.repository_owner }}" != "${{ github.event.pull_request.head.repo.owner.login }}" ]; then
./scripts/clang-format/clang-format-apply.sh origin/${{ github.base_ref }} fork/${{ github.head_ref }}
else
./scripts/clang-format/clang-format-apply.sh origin/${{ github.base_ref }} origin/${{ github.head_ref }}
fi
- name: Save clang-format changes as patch
run: |
git diff -U0
git diff -U0 > clang_format.patch
- name: Check if patch is not empty
run: |
if [ -s clang_format.patch ]; then
echo "Patch is not empty"
echo "patch_not_empty=true" >> $GITHUB_STATE
else
echo "Patch is empty"
echo "patch_not_empty=false" >> $GITHUB_STATE
fi
- name: Post patch as comment
if: env.GITHUB_STATE.patch_not_empty == 'true'
run: |
API_URL="https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments"
curl -X POST \
-H "Accept: application/vnd.github+json" \
-H "Authorization: token ${{ secrets.GH_ACTION_TOKEN_CLANG_FORMAT }}" \
-H "X-GitHub-Api-Version: 2022-11-28" \
-d "{\"body\": \"clang-format has detected some changes that need formatting\"}" \
"${API_URL}"
18 changes: 0 additions & 18 deletions clang-format-diff.sh

This file was deleted.

20 changes: 20 additions & 0 deletions scripts/clang-format/clang-format-apply.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

BASE_BRANCH=$1
CURRENT_BRANCH=$2

# Save unified diff with 0 context
git diff -U0 --name-only $BASE_BRANCH...$CURRENT_BRANCH > filestochange.diff

cat filestochange.diff

# Run clang-format in-place on .h and .cpp files
while IFS= read -r line; do
if [ -n "$line" ]; then
if [[ "$line" == *.h ]] || [[ "$line" == *.cpp ]]; then
clang-format -i "$line"
fi
fi
done < filestochange.diff

rm filestochange.diff

0 comments on commit bdd7971

Please sign in to comment.