|
| 1 | +name: Check code formatting |
| 2 | + |
| 3 | +on: |
| 4 | + workflow_dispatch: |
| 5 | +# pull_request: |
| 6 | +# branches: |
| 7 | +# - main |
| 8 | + |
| 9 | +env: |
| 10 | + BUILD_TYPE: Debug |
| 11 | + |
| 12 | +jobs: |
| 13 | + format_check: |
| 14 | + runs-on: ubuntu-latest |
| 15 | + steps: |
| 16 | + - name: Checkout |
| 17 | + uses: actions/checkout@v3 |
| 18 | + |
| 19 | + - name: Install clang-format |
| 20 | + |
| 21 | + with: |
| 22 | + clangformat: 19.1.6 |
| 23 | + |
| 24 | + - name: Check formatting in dynadjust |
| 25 | + id: fmt |
| 26 | + run: | |
| 27 | + echo "Checking formatting in dynadjust" |
| 28 | + ERRORS="" |
| 29 | + # Determine clang-format style: use .clang-format if available, |
| 30 | + # otherwise default to LLVM style. |
| 31 | + if [ -f .clang-format ]; then |
| 32 | + STYLE=file |
| 33 | + else |
| 34 | + STYLE=LLVM |
| 35 | + fi |
| 36 | + files=$(find dynadjust -type f \( -name '*.cpp' -o -name '*.h' \)) |
| 37 | + ret=0 |
| 38 | + for f in $files; do |
| 39 | + diff=$(clang-format -style=$STYLE "$f" | diff "$f" -) |
| 40 | + if [ -n "$diff" ]; then |
| 41 | + echo "Formatting issues found in $f" |
| 42 | + ERRORS="${ERRORS}\n- $f" |
| 43 | + ret=1 |
| 44 | + fi |
| 45 | + done |
| 46 | + echo "errors<<EOF" >> $GITHUB_OUTPUT |
| 47 | + echo -e "$ERRORS" >> $GITHUB_OUTPUT |
| 48 | + echo "EOF" >> $GITHUB_OUTPUT |
| 49 | + exit $ret |
| 50 | +
|
| 51 | + - name: Post comment on PR |
| 52 | + if: ${{ github.event_name == 'pull_request' && steps.fmt.outputs.errors != '' }} |
| 53 | + uses: actions/github-script@v6 |
| 54 | + with: |
| 55 | + script: | |
| 56 | + const err = `${{ steps.fmt.outputs.errors }}`; |
| 57 | + const msg = `### Code Formatting Issues\nThe following files do not adhere to the clang-format style:${err}`; |
| 58 | + github.rest.issues.createComment({ |
| 59 | + issue_number: context.issue.number, |
| 60 | + owner: context.repo.owner, |
| 61 | + repo: context.repo.repo, |
| 62 | + body: msg |
| 63 | + }); |
0 commit comments