blob: 0e357ec80a4aa1f6c4c030dcced49db16be01af3 [file] [log] [blame]
Philip Zeyliger4de80d22025-04-23 12:33:31 -07001name: Code Formatting
2on:
3 workflow_call:
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -07004 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 Snydereda2a8c2025-04-25 21:24:11 +000010 outputs:
11 commit_sha:
12 description: "The SHA of the commit with formatting fixes"
13 value: ${{ jobs.formatting.outputs.commit_sha }}
Philip Zeyliger4de80d22025-04-23 12:33:31 -070014 push:
15 branches-ignore:
Josh Bleecher Snyder93bb66a2025-04-30 16:29:05 -070016 - "queue-main-*"
17 - "queue-dev-*"
Philip Zeyliger4de80d22025-04-23 12:33:31 -070018 pull_request:
19
Josh Bleecher Snyderae772262025-04-25 20:02:22 +000020permissions:
21 contents: write
22
Philip Zeyliger4de80d22025-04-23 12:33:31 -070023jobs:
24 formatting:
Josh Bleecher Snyderc3391a92025-04-25 21:03:33 +000025 outputs:
26 commit_sha: ${{ steps.get_commit.outputs.commit_sha }}
Philip Zeyliger4de80d22025-04-23 12:33:31 -070027 runs-on: ubuntu-latest
28 steps:
29 - uses: actions/checkout@v4
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070030 with:
Josh Bleecher Snyder6ada85d2025-05-14 22:08:09 +000031 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 Snyderbd6c1682025-04-25 12:04:07 -070032 # Need full history for queue-main pushes
33 fetch-depth: 0
Philip Zeyliger4de80d22025-04-23 12:33:31 -070034
35 # Setup for Prettier
36 - name: Setup Node.js
37 uses: actions/setup-node@v4
38 with:
Josh Bleecher Snyder6e42f322025-04-25 13:18:36 -070039 node-version: "20"
40 cache: "npm"
Philip Zeyliger2032b1c2025-04-23 19:40:42 -070041 cache-dependency-path: webui/package-lock.json
Philip Zeyliger4de80d22025-04-23 12:33:31 -070042
43 - name: Install dependencies
Philip Zeyliger2032b1c2025-04-23 19:40:42 -070044 working-directory: ./webui
Philip Zeyliger4de80d22025-04-23 12:33:31 -070045 run: npm ci
46
Philip Zeyliger4de80d22025-04-23 12:33:31 -070047 - name: Install gofumpt v0.8.0
48 run: |
Josh Bleecher Snyder9236cce2025-06-04 15:35:35 -070049 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 Zeyliger4de80d22025-04-23 12:33:31 -070052
Philip Zeyliger8b00db12025-04-25 18:41:38 +000053 - name: Check formatting
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070054 if: inputs.auto_fix != true
Philip Zeyliger8b00db12025-04-25 18:41:38 +000055 run: bin/run-formatters.sh check
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070056
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 Snyder4c1015a2025-04-30 16:28:21 -070074
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070075 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 Snyder4c1015a2025-04-30 16:28:21 -070078
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070079 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 Zeyliger7910eef2025-04-30 17:50:33 +000082 else
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070083 echo "Found multiple commits ($COMMIT_COUNT) since merge-base with origin/main. Creating a new commit."
Philip Zeyliger7910eef2025-04-30 17:50:33 +000084 git commit -m "all: fix formatting"
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070085 fi
Josh Bleecher Snyder4c1015a2025-04-30 16:28:21 -070086
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070087 git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD
88
Josh Bleecher Snyderc3391a92025-04-25 21:03:33 +000089 - name: Get commit SHA
90 id: get_commit
91 run: echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT