| 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 |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 10 | push: |
| 11 | branches-ignore: |
| 12 | - "queue-main-**" |
| 13 | pull_request: |
| 14 | |
| Josh Bleecher Snyder | ae77226 | 2025-04-25 20:02:22 +0000 | [diff] [blame] | 15 | permissions: |
| 16 | contents: write |
| 17 | |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 18 | jobs: |
| 19 | formatting: |
| 20 | runs-on: ubuntu-latest |
| 21 | steps: |
| 22 | - uses: actions/checkout@v4 |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 23 | with: |
| 24 | ref: ${{ github.head_ref || github.ref }} |
| 25 | # Need full history for queue-main pushes |
| 26 | fetch-depth: 0 |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 27 | |
| 28 | # Setup for Prettier |
| 29 | - name: Setup Node.js |
| 30 | uses: actions/setup-node@v4 |
| 31 | with: |
| Josh Bleecher Snyder | 6e42f32 | 2025-04-25 13:18:36 -0700 | [diff] [blame] | 32 | node-version: "20" |
| 33 | cache: "npm" |
| Philip Zeyliger | 2032b1c | 2025-04-23 19:40:42 -0700 | [diff] [blame] | 34 | cache-dependency-path: webui/package-lock.json |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 35 | |
| 36 | - name: Install dependencies |
| Philip Zeyliger | 2032b1c | 2025-04-23 19:40:42 -0700 | [diff] [blame] | 37 | working-directory: ./webui |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 38 | run: npm ci |
| 39 | |
| Philip Zeyliger | 4de80d2 | 2025-04-23 12:33:31 -0700 | [diff] [blame] | 40 | # Setup for gofumpt |
| 41 | - name: Setup Go |
| 42 | uses: actions/setup-go@v4 |
| 43 | with: |
| 44 | go-version: stable |
| 45 | cache: true |
| 46 | |
| 47 | - name: Install gofumpt v0.8.0 |
| 48 | run: | |
| 49 | go install mvdan.cc/gofumpt@v0.8.0 |
| 50 | echo "$(go env GOPATH)/bin" >> $GITHUB_PATH |
| 51 | |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 52 | - name: Check formatting |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 53 | if: inputs.auto_fix != true |
| Philip Zeyliger | 8b00db1 | 2025-04-25 18:41:38 +0000 | [diff] [blame] | 54 | run: bin/run-formatters.sh check |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 55 | |
| 56 | - name: Fix formatting |
| 57 | if: inputs.auto_fix == true |
| 58 | run: bin/run-formatters.sh fix |
| 59 | |
| 60 | # Commit formatting fixes if auto_fix is true |
| 61 | - name: Commit and push formatting fixes if needed |
| 62 | if: inputs.auto_fix == true |
| 63 | run: | |
| 64 | # Only proceed if there are changes to commit |
| 65 | if [[ -z $(git status --porcelain) ]]; then |
| 66 | echo "No formatting changes detected, skipping commit" |
| 67 | exit 0 |
| 68 | fi |
| 69 | |
| 70 | git config --global user.name "Autoformatter" |
| 71 | git config --global user.email "bot@sketch.dev" |
| 72 | git add . |
| Josh Bleecher Snyder | 15a4db7 | 2025-04-25 13:18:06 -0700 | [diff] [blame] | 73 | git commit -m "all: fix formatting" |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 74 | |
| 75 | # If this is a queue-main branch, push the changes |
| 76 | if [[ "${{ github.ref }}" == refs/heads/queue-main-* ]]; then |
| Josh Bleecher Snyder | ae77226 | 2025-04-25 20:02:22 +0000 | [diff] [blame] | 77 | git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/boldsoftware/sketch.git HEAD |
| Josh Bleecher Snyder | bd6c168 | 2025-04-25 12:04:07 -0700 | [diff] [blame] | 78 | fi |