blob: 1313995535349134a325d145a6699e6ed342e57b [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
Philip Zeyligerd2ba10c2025-06-12 13:51:27 -070036 # We don't need to do "npm ci" because run-formatters.sh uses "npx prettier"
37 # and this is speedier than installing everything.
Philip Zeyliger4de80d22025-04-23 12:33:31 -070038 - name: Setup Node.js
39 uses: actions/setup-node@v4
40 with:
Josh Bleecher Snyder6e42f322025-04-25 13:18:36 -070041 node-version: "20"
42 cache: "npm"
Philip Zeyliger2032b1c2025-04-23 19:40:42 -070043 cache-dependency-path: webui/package-lock.json
Philip Zeyliger4de80d22025-04-23 12:33:31 -070044
Philip Zeyliger4de80d22025-04-23 12:33:31 -070045 - name: Install gofumpt v0.8.0
46 run: |
Josh Bleecher Snyder9236cce2025-06-04 15:35:35 -070047 curl -L https://github.com/mvdan/gofumpt/releases/download/v0.8.0/gofumpt_v0.8.0_linux_amd64 -o /tmp/gofumpt
48 chmod +x /tmp/gofumpt
49 sudo mv /tmp/gofumpt /usr/local/bin/gofumpt
Philip Zeyliger4de80d22025-04-23 12:33:31 -070050
Philip Zeyliger8b00db12025-04-25 18:41:38 +000051 - name: Check formatting
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070052 if: inputs.auto_fix != true
Philip Zeyliger8b00db12025-04-25 18:41:38 +000053 run: bin/run-formatters.sh check
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070054
55 - name: Fix formatting
56 if: inputs.auto_fix == true
57 run: bin/run-formatters.sh fix
58
59 # Commit formatting fixes if auto_fix is true
60 - name: Commit and push formatting fixes if needed
61 if: inputs.auto_fix == true
62 run: |
63 # Only proceed if there are changes to commit
64 if [[ -z $(git status --porcelain) ]]; then
65 echo "No formatting changes detected, skipping commit"
66 exit 0
67 fi
68
69 git config --global user.name "Autoformatter"
70 git config --global user.email "bot@sketch.dev"
71 git add .
Josh Bleecher Snyder4c1015a2025-04-30 16:28:21 -070072
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070073 git fetch origin main
74 MERGE_BASE=$(git merge-base HEAD origin/main)
75 COMMIT_COUNT=$(git rev-list --count $MERGE_BASE..HEAD)
Josh Bleecher Snyder4c1015a2025-04-30 16:28:21 -070076
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070077 if [ "$COMMIT_COUNT" -eq 1 ]; then
78 echo "Found exactly one commit since merge-base with origin/main. Amending the commit."
79 git commit --amend --no-edit
Philip Zeyliger7910eef2025-04-30 17:50:33 +000080 else
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070081 echo "Found multiple commits ($COMMIT_COUNT) since merge-base with origin/main. Creating a new commit."
Philip Zeyliger7910eef2025-04-30 17:50:33 +000082 git commit -m "all: fix formatting"
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070083 fi
Josh Bleecher Snyder4c1015a2025-04-30 16:28:21 -070084
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070085 git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD
86
Josh Bleecher Snyderc3391a92025-04-25 21:03:33 +000087 - name: Get commit SHA
88 id: get_commit
89 run: echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT