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