Jesse interface thesis proj #196
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: Format | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| branches: [main] | |
| # running tests can be skipped for docs-only changes | |
| paths: ['src/**', 'basilmill/**', '**.mill', '.?mill*', '*.conf', '.github/workflows/format.yml'] | |
| workflow_dispatch: | |
| inputs: | |
| autoformat: | |
| required: false | |
| default: false | |
| type: boolean | |
| jobs: | |
| scalafmt: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| pull-requests: write | |
| contents: write | |
| env: | |
| autoformat: ${{ inputs.autoformat == true }} | |
| branch: ${{ github.head_ref || github.ref_name }} | |
| commit: ${{ github.event.pull_request.head.sha || github.sha }} | |
| footer: >- | |
| last updated at ${{ github.event.pull_request.head.sha || github.sha }} | |
| ([logs](${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }})). | |
| steps: | |
| - name: Checkout | |
| uses: actions/checkout@v4 | |
| - uses: coursier/cache-action@v6 | |
| with: | |
| extraMillFiles: '**/*.mill' | |
| ignoreJob: true | |
| ignoreMatrix: true | |
| - uses: actions/cache@v4 | |
| with: | |
| path: out | |
| key: | | |
| build-${{ runner.os }}-${{ hashFiles('**/*.mill') }}-${{ hashFiles('**/*.scala') }} | |
| restore-keys: | | |
| build-${{ runner.os }}-${{ hashFiles('**/*.mill') }}- | |
| build-${{ runner.os }}- | |
| build- | |
| - run: ./mill -i fmt | |
| - name: Check source formatting and imports | |
| id: diff | |
| run: git diff --exit-code --color | |
| continue-on-error: true | |
| - name: Prepare empty PR comment | |
| if: always() && steps.diff.outcome == 'success' | |
| run: | | |
| cat <<EOF > /tmp/pr-message.txt | |
| scalafmt check passed. | |
| $footer | |
| EOF | |
| - name: Prepare diff for PR comment | |
| if: always() && steps.diff.outcome == 'failure' | |
| id: prepare | |
| run: | | |
| git config user.name 'github-actions[bot]' | |
| git config user.email '41898282+github-actions[bot]@users.noreply.github.com' | |
| git add -u && git commit -m "automated scalafmt" | |
| git format-patch -U1 -1 HEAD --stdout > scalafmt.patch | |
| # this function will only be used from the `workflow_dispatch` action, which | |
| # already uses the right branch - we don't need to worry about merge branches. | |
| try_apply() { | |
| git push | |
| } | |
| patch_escaped="'$(cat scalafmt.patch | sed "s/'/'\\\\''/g")'" | |
| # XXX: i'm very sorry. what follows is a sequence of arguments to printf which get concatenated. each | |
| # of these arguments is a multi-line single-quoted string or a variable. | |
| printf '%s' ' | |
| <details> | |
| <summary>options to apply scalafmt changes:</summary> | |
| run this command: | |
| `````diff | |
| echo ' "$patch_escaped" ' | git am -3 | |
| ````` | |
| or, trigger a workflow run on `' $branch '` with `autoformat=true`, either by [using the website](https://github.com/UQ-PAC/BASIL/actions/workflows/format.yml) or by using the `gh` tool: | |
| ```bash | |
| gh workflow run format.yml --field "autoformat=true" --ref ' "'$branch'" ' | |
| ``` | |
| or, run the formatter locally: | |
| ```bash | |
| ./mill -i fmt | |
| ``` | |
| </details> | |
| ' > /tmp/how-to-apply.txt | |
| if "$autoformat"; then | |
| if try_apply; then | |
| ok=true | |
| echo 'automated scalafmt applied successfully.' > /tmp/pr-message.txt | |
| else | |
| ok=false | |
| printf '%s\n' \ | |
| "automated scalafmt **failed**. please report this issue and try another method." \ | |
| '' \ | |
| "$(cat /tmp/how-to-apply.txt)" > /tmp/pr-message.txt | |
| fi | |
| else | |
| ok=true | |
| printf '%s\n' \ | |
| "scalafmt changes needed." \ | |
| '' \ | |
| "$(cat /tmp/how-to-apply.txt)" > /tmp/pr-message.txt | |
| fi | |
| printf '\n%s\n' "$footer" >> /tmp/pr-message.txt | |
| "$ok" | |
| - uses: mshick/add-pr-comment@v2 | |
| if: always() && (github.event_name == 'pull_request' || github.event_name == 'workflow_dispatch') | |
| with: | |
| message-path: /tmp/pr-message.txt | |
| # avoid posting a comment if it succeeded the first time. | |
| update-only: ${{ steps.diff.outcome == 'success' }} | |
| - name: Report workflow status | |
| if: always() | |
| run: | | |
| set -x | |
| if ! "$autoformat"; then | |
| : Scalafmt check outcome: | |
| ${{ steps.diff.outcome != 'failure' }} | |
| : Scalafmt comment outcome: | |
| ${{ steps.prepare.outcome != 'failure' }} | |
| else | |
| : Scalafmt auto-format outcome: | |
| ${{ steps.prepare.outcome != 'failure' }} | |
| fi | |