|
| 1 | +on: |
| 2 | + pull_request: |
| 3 | + issue_comment: |
| 4 | + types: [created] |
| 5 | + |
| 6 | +name: Formatting |
| 7 | + |
| 8 | +jobs: |
| 9 | + formatting: |
| 10 | + if: github.event_name == 'pull_request' || (github.event_name == 'issue_comment' && github.event.issue.pull_request && (github.event.comment.author_association == 'MEMBER' || github.event.comment.author_association == 'COLLABORATOR' || github.event.comment.author_association == 'OWNER' || github.event.issue.user.id == github.event.comment.user.id) && startsWith(github.event.comment.body, '/format') ) |
| 11 | + runs-on: ubuntu-latest |
| 12 | + steps: |
| 13 | + - name: Clone the repository |
| 14 | + uses: actions/checkout@v4 |
| 15 | + - name: Checkout the pull request code # this checks out the actual branch so that one can commit into it |
| 16 | + if: github.event_name == 'issue_comment' |
| 17 | + env: |
| 18 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 19 | + run: | |
| 20 | + gh pr checkout ${{ github.event.issue.number }} |
| 21 | + - name: Install JuliaFormatter and format |
| 22 | + run: | |
| 23 | + julia --color=yes -e 'import Pkg; Pkg.add("JuliaFormatter")' |
| 24 | + julia --color=yes -e 'using JuliaFormatter; format(".")' |
| 25 | + - name: Remove trailing whitespace |
| 26 | + run: | |
| 27 | + find -name '*.jl' -or -name '*.md' -or -name '*.toml' -or -name '*.yml' | while read filename ; do |
| 28 | + # remove any trailing spaces |
| 29 | + sed --in-place -e 's/\s*$//' "$filename" |
| 30 | + # add a final newline if missing |
| 31 | + if [[ -s "$filename" && $(tail -c 1 "$filename" |wc -l) -eq 0 ]] ; then |
| 32 | + echo >> "$filename" |
| 33 | + fi |
| 34 | + # squash superfluous final newlines |
| 35 | + sed -i -e :a -e '/^\n*$/{$d;N;};/\n$/ba' "$filename" |
| 36 | + done |
| 37 | + - name: Fail on formatting problems |
| 38 | + if: github.event_name == 'pull_request' |
| 39 | + run: | |
| 40 | + if git diff --exit-code --quiet |
| 41 | + then echo "Looks OK" |
| 42 | + else echo "Formatting fixes required!"; git diff -p ; exit 1 |
| 43 | + fi |
| 44 | + - name: Commit fixes |
| 45 | + if: github.event_name == 'issue_comment' |
| 46 | + env: |
| 47 | + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 48 | + run: | |
| 49 | + if [ `git status -s | wc -l` -ne 0 ] ; then |
| 50 | + git config --local user.name "$GITHUB_ACTOR" |
| 51 | + git config --local user.email "$GITHUB_ACTOR@users.noreply.github.com" |
| 52 | + git commit -a -m "automatic formatting" -m "triggered by @$GITHUB_ACTOR on PR #${{ github.event.issue.number }}" |
| 53 | + if git push |
| 54 | + then gh pr comment ${{ github.event.issue.number }} --body \ |
| 55 | + ":heavy_check_mark: Auto-formatting triggered by [this comment](${{ github.event.comment.html_url }}) succeeded, commited as `git rev-parse HEAD`" |
| 56 | + else gh pr comment ${{ github.event.issue.number }} --body \ |
| 57 | + ":x: Auto-formatting triggered by [this comment](${{ github.event.comment.html_url }}) failed, perhaps someone pushed to the PR in the meantime?" |
| 58 | + fi |
| 59 | + else |
| 60 | + gh pr comment ${{ github.event.issue.number }} --body \ |
| 61 | + ":sunny: Auto-formatting triggered by [this comment](${{ github.event.comment.html_url }}) succeeded, but the code was already formatted correctly." |
| 62 | + fi |
0 commit comments