chore: add prettier #126
Workflow file for this run
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: ci | |
| on: | |
| push: | |
| branches: [main] | |
| pull_request: | |
| branches: [main] | |
| jobs: | |
| build: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| issues: write | |
| pull-requests: write | |
| steps: | |
| - uses: actions/checkout@v4 | |
| with: | |
| token: ${{ secrets.GITHUB_TOKEN }} | |
| fetch-depth: 0 | |
| - uses: actions/setup-node@v3 | |
| with: | |
| node-version: "18" | |
| - name: Install dependencies | |
| run: npm ci | |
| - name: Check code formatting | |
| id: prettier-check | |
| continue-on-error: true | |
| run: | | |
| set +e | |
| npx prettier --check . | |
| echo "exitcode=$?" >> $GITHUB_OUTPUT | |
| - name: Format code | |
| if: steps.prettier-check.outputs.exitcode == '1' | |
| run: npm run fmt | |
| - name: Run tests | |
| run: npm test | |
| - name: Update snapshot | |
| run: npm run update-snapshot && git add --all | |
| - name: Check for formatting or snapshot changes | |
| id: changes | |
| continue-on-error: true | |
| run: | | |
| set +e | |
| git diff --exit-code | |
| echo "exitcode=$?" >> $GITHUB_OUTPUT | |
| - name: Commit and push changes | |
| if: github.event_name == 'pull_request' && steps.changes.outputs.exitcode == '1' | |
| run: | | |
| git config --local user.email "[email protected]" | |
| git config --local user.name "GitHub Action" | |
| git add --all | |
| if git diff --staged --quiet; then | |
| echo "No changes to commit" | |
| else | |
| git commit -m "Auto-format code and update snapshots [skip ci]" | |
| git push origin HEAD:${{ github.head_ref }} | |
| fi | |
| - name: Prepare diff for comment | |
| id: diff | |
| continue-on-error: true | |
| run: | | |
| set +e | |
| body=$(git diff --exit-code --diff-filter=AM HEAD~1 -- '*.snap' 2>&1) | |
| echo "exitcode=$?" >> $GITHUB_OUTPUT | |
| body="${body//'<'/'<'}" | |
| body="${body//'>'/'>'}" | |
| delimiter="$(openssl rand -hex 8)" | |
| echo "body<<${delimiter}" >> $GITHUB_OUTPUT | |
| echo "${body:0:64000}" >> $GITHUB_OUTPUT | |
| echo "${delimiter}" >> $GITHUB_OUTPUT | |
| - uses: marocchino/sticky-pull-request-comment@v2 | |
| if: github.event_name == 'pull_request' && steps.changes.outputs.exitcode == '1' && steps.prettier-check.outputs.exitcode == '1' | |
| with: | |
| header: prettier-auto-format | |
| hide_and_recreate: true | |
| hide_classify: "OUTDATED" | |
| message: | | |
| 🎨 **Code has been automatically formatted with Prettier.** | |
| The formatting changes have been committed to this PR. Please pull the latest changes if you're working locally. | |
| - uses: marocchino/sticky-pull-request-comment@v2 | |
| if: github.event_name == 'pull_request' && steps.diff.outputs.exitcode == '1' | |
| with: | |
| header: sticky-comment-for-git-diff | |
| hide_and_recreate: true | |
| hide_classify: "OUTDATED" | |
| message: | | |
| **Found changes in the snapshot tests. Please review.** | |
| <details> | |
| <summary>Diff 📖</summary> | |
| <pre lang="diff"><code> | |
| ${{ steps.diff.outputs.body }} | |
| </code></pre> | |
| </details> | |
| - uses: marocchino/sticky-pull-request-comment@v2 | |
| if: github.event_name == 'pull_request' && steps.diff.outputs.exitcode == '0' | |
| with: | |
| header: sticky-comment-for-git-diff | |
| hide: true | |
| hide_classify: "OUTDATED" |