Check code formatting #2
This file contains hidden or 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: 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/[email protected] | |
| with: | |
| clangformat: 19.1.6 | |
| - name: Check formatting in dynadjust | |
| id: fmt | |
| run: | | |
| echo "Checking formatting in dynadjust" | |
| ERRORS="" | |
| # Use .clang-format if present, otherwise default to LLVM style. | |
| if [ -f .clang-format ]; then | |
| STYLE=file | |
| else | |
| STYLE=LLVM | |
| fi | |
| files=$(find dynadjust -type f \( -name '*.cpp' -o -name '*.h' \)) | |
| ret=0 | |
| for f in $files; do | |
| diff=$(clang-format -style=$STYLE "$f" | diff "$f" -) | |
| if [ -n "$diff" ]; then | |
| echo "::warning file=$f::Formatting issues found" | |
| ERRORS="${ERRORS}\n- $f" | |
| ret=1 | |
| fi | |
| done | |
| echo "errors<<EOF" >> $GITHUB_OUTPUT | |
| echo -e "$ERRORS" >> $GITHUB_OUTPUT | |
| echo "EOF" >> $GITHUB_OUTPUT | |
| exit $ret | |
| - 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:${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(file => ({ | |
| path: file.replace(/^- /, ''), | |
| start_line: 1, | |
| end_line: 1, | |
| annotation_level: 'failure', | |
| message: 'Formatting issues found' | |
| })) | |
| } | |
| }); |