blob: ed59445d5241d3885fdd9774ec43404d853cbea3 [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:
31 ref: ${{ github.head_ref || github.ref }}
32 # 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 # 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 Zeyliger8b00db12025-04-25 18:41:38 +000059 - name: Check formatting
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070060 if: inputs.auto_fix != true
Philip Zeyliger8b00db12025-04-25 18:41:38 +000061 run: bin/run-formatters.sh check
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070062
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 Snyder4c1015a2025-04-30 16:28:21 -070080
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070081 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 Snyder4c1015a2025-04-30 16:28:21 -070084
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070085 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 Zeyliger7910eef2025-04-30 17:50:33 +000088 else
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070089 echo "Found multiple commits ($COMMIT_COUNT) since merge-base with origin/main. Creating a new commit."
Philip Zeyliger7910eef2025-04-30 17:50:33 +000090 git commit -m "all: fix formatting"
Josh Bleecher Snyderbd6c1682025-04-25 12:04:07 -070091 fi
Josh Bleecher Snyder4c1015a2025-04-30 16:28:21 -070092
Josh Bleecher Snyder48c84c92025-04-30 16:15:27 -070093 git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD
94
Josh Bleecher Snyderc3391a92025-04-25 21:03:33 +000095 - name: Get commit SHA
96 id: get_commit
97 run: echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT