| 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: |
| 31 | ref: ${{ github.head_ref || github.ref }} |
| 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 | # Setup for gofumpt |
| 48 | - name: Setup Go |
| 49 | uses: actions/setup-go@v4 |
| 50 | with: |
| 51 | go-version: stable |
| 52 | cache: true |
| 53 | |
| 54 | - name: Install gofumpt v0.8.0 |
| 55 | run: | |
| 56 | go install mvdan.cc/gofumpt@v0.8.0 |
| 57 | echo "$(go env GOPATH)/bin" >> $GITHUB_PATH |
| 58 | |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 59 | - name: Check formatting |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 60 | if: inputs.auto_fix != true |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 61 | run: bin/run-formatters.sh check |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 62 | |
| 63 | - name: Fix formatting |
| 64 | if: inputs.auto_fix == true |
| 65 | run: bin/run-formatters.sh fix |
| 66 | |
| 67 | # Commit formatting fixes if auto_fix is true |
| 68 | - name: Commit and push formatting fixes if needed |
| 69 | if: inputs.auto_fix == true |
| 70 | run: | |
| 71 | # Only proceed if there are changes to commit |
| 72 | if [[ -z $(git status --porcelain) ]]; then |
| 73 | echo "No formatting changes detected, skipping commit" |
| 74 | exit 0 |
| 75 | fi |
| 76 | |
| 77 | git config --global user.name "Autoformatter" |
| 78 | git config --global user.email "bot@sketch.dev" |
| 79 | git add . |
| Josh Bleecher Snyder | 4c1015a | 2025-04-30 16:28:21 -0700 | [diff] [blame] | 80 | |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 81 | git fetch origin main |
| 82 | MERGE_BASE=$(git merge-base HEAD origin/main) |
| 83 | COMMIT_COUNT=$(git rev-list --count $MERGE_BASE..HEAD) |
| Josh Bleecher Snyder | 4c1015a | 2025-04-30 16:28:21 -0700 | [diff] [blame] | 84 | |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 85 | if [ "$COMMIT_COUNT" -eq 1 ]; then |
| 86 | echo "Found exactly one commit since merge-base with origin/main. Amending the commit." |
| 87 | git commit --amend --no-edit |
| Philip Zeyliger | 7910eef | 2025-04-30 17:50:33 +0000 | [diff] [blame] | 88 | else |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 89 | 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] | 90 | git commit -m "all: fix formatting" |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 91 | fi |
| Josh Bleecher Snyder | 4c1015a | 2025-04-30 16:28:21 -0700 | [diff] [blame] | 92 | |
| Josh Bleecher Snyder | 48c84c9 | 2025-04-30 16:15:27 -0700 | [diff] [blame] | 93 | git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD |
| 94 | |
| Josh Bleecher Snyder | c3391a9 | 2025-04-25 21:03:33 +0000 | [diff] [blame] | 95 | - name: Get commit SHA |
| 96 | id: get_commit |
| 97 | run: echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |