| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 1 | name: Code Formatting |
| 2 | on: |
| 3 | workflow_call: |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 4 | inputs: |
| 5 | auto_fix: |
| 6 | description: "Automatically fix formatting issues instead of just checking" |
| 7 | required: false |
| 8 | type: boolean |
| 9 | default: false |
| Josh Bleecher Snyder | eda2a8c | 2025-04-25 21:24:11 +0000 | [diff] [blame] | 10 | outputs: |
| 11 | commit_sha: |
| 12 | description: "The SHA of the commit with formatting fixes" |
| 13 | value: ${{ jobs.formatting.outputs.commit_sha }} |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 14 | push: |
| 15 | branches-ignore: |
| Josh Bleecher Snyder | 93bb66a | 2025-04-30 16:29:05 -0700 | [diff] [blame] | 16 | - "queue-main-*" |
| 17 | - "queue-dev-*" |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 18 | pull_request: |
| 19 | |
| Josh Bleecher Snyder | ae77226 | 2025-04-25 20:02:22 +0000 | [diff] [blame] | 20 | permissions: |
| 21 | contents: write |
| 22 | |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 23 | jobs: |
| 24 | formatting: |
| Josh Bleecher Snyder | c3391a9 | 2025-04-25 21:03:33 +0000 | [diff] [blame] | 25 | outputs: |
| 26 | commit_sha: ${{ steps.get_commit.outputs.commit_sha }} |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 27 | runs-on: ubuntu-latest |
| 28 | steps: |
| 29 | - uses: actions/checkout@v4 |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 30 | with: |
| Josh Bleecher Snyder | 6ada85d | 2025-05-14 22:08:09 +0000 | [diff] [blame] | 31 | ref: ${{ (inputs.auto_fix == true && (github.head_ref || github.ref)) || (github.event_name == 'pull_request' && github.event.pull_request.head.sha) || github.ref }} |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 32 | # Need full history for queue-main pushes |
| 33 | fetch-depth: 0 |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 34 | |
| 35 | # Setup for Prettier |
| 36 | - name: Setup Node.js |
| 37 | uses: actions/setup-node@v4 |
| 38 | with: |
| Josh Bleecher Snyder | 6e42f32 | 2025-04-25 13:18:36 -0700 | [diff] [blame] | 39 | node-version: "20" |
| 40 | cache: "npm" |
| Philip Zeyliger | 2032b1c | 2025-04-23 19:40:42 -0700 | [diff] [blame] | 41 | cache-dependency-path: webui/package-lock.json |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 42 | |
| 43 | - name: Install dependencies |
| Philip Zeyliger | 2032b1c | 2025-04-23 19:40:42 -0700 | [diff] [blame] | 44 | working-directory: ./webui |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 45 | run: npm ci |
| 46 | |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 47 | - name: Install gofumpt v0.8.0 |
| 48 | run: | |
| Josh Bleecher Snyder | 9236cce | 2025-06-04 15:35:35 -0700 | [diff] [blame] | 49 | curl -L https://github.com/mvdan/gofumpt/releases/download/v0.8.0/gofumpt_v0.8.0_linux_amd64 -o /tmp/gofumpt |
| 50 | chmod +x /tmp/gofumpt |
| 51 | sudo mv /tmp/gofumpt /usr/local/bin/gofumpt |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 52 | |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 53 | - name: Check formatting |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 54 | if: inputs.auto_fix != true |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 55 | run: bin/run-formatters.sh check |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 56 | |
| 57 | - name: Fix formatting |
| 58 | if: inputs.auto_fix == true |
| 59 | run: bin/run-formatters.sh fix |
| 60 | |
| 61 | # Commit formatting fixes if auto_fix is true |
| 62 | - name: Commit and push formatting fixes if needed |
| 63 | if: inputs.auto_fix == true |
| 64 | run: | |
| 65 | # Only proceed if there are changes to commit |
| 66 | if [[ -z $(git status --porcelain) ]]; then |
| 67 | echo "No formatting changes detected, skipping commit" |
| 68 | exit 0 |
| 69 | fi |
| 70 | |
| 71 | git config --global user.name "Autoformatter" |
| 72 | git config --global user.email "bot@sketch.dev" |
| 73 | git add . |
| Josh Bleecher Snyder | 4c1015a | 2025-04-30 16:28:21 -0700 | [diff] [blame] | 74 | |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 75 | git fetch origin main |
| 76 | MERGE_BASE=$(git merge-base HEAD origin/main) |
| 77 | COMMIT_COUNT=$(git rev-list --count $MERGE_BASE..HEAD) |
| Josh Bleecher Snyder | 4c1015a | 2025-04-30 16:28:21 -0700 | [diff] [blame] | 78 | |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 79 | if [ "$COMMIT_COUNT" -eq 1 ]; then |
| 80 | echo "Found exactly one commit since merge-base with origin/main. Amending the commit." |
| 81 | git commit --amend --no-edit |
| Philip Zeyliger | 7910eef | 2025-04-30 17:50:33 +0000 | [diff] [blame] | 82 | else |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 83 | echo "Found multiple commits ($COMMIT_COUNT) since merge-base with origin/main. Creating a new commit." |
| Philip Zeyliger | 7910eef | 2025-04-30 17:50:33 +0000 | [diff] [blame] | 84 | git commit -m "all: fix formatting" |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 85 | fi |
| Josh Bleecher Snyder | 4c1015a | 2025-04-30 16:28:21 -0700 | [diff] [blame] | 86 | |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 87 | git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD |
| 88 | |
| Josh Bleecher Snyder | c3391a9 | 2025-04-25 21:03:33 +0000 | [diff] [blame] | 89 | - name: Get commit SHA |
| 90 | id: get_commit |
| 91 | run: echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |