| name: Code Formatting |
| on: |
| workflow_call: |
| inputs: |
| auto_fix: |
| description: "Automatically fix formatting issues instead of just checking" |
| required: false |
| type: boolean |
| default: false |
| outputs: |
| commit_sha: |
| description: "The SHA of the commit with formatting fixes" |
| value: ${{ jobs.formatting.outputs.commit_sha }} |
| push: |
| branches-ignore: |
| - "queue-main-*" |
| - "queue-dev-*" |
| pull_request: |
| |
| permissions: |
| contents: write |
| |
| jobs: |
| formatting: |
| outputs: |
| commit_sha: ${{ steps.get_commit.outputs.commit_sha }} |
| runs-on: ubuntu-latest |
| steps: |
| - uses: actions/checkout@v4 |
| with: |
| ref: ${{ (inputs.auto_fix == true && (github.head_ref || github.ref)) || (github.event_name == 'pull_request' && github.event.pull_request.head.sha) || github.ref }} |
| # Need full history for queue-main pushes |
| fetch-depth: 0 |
| |
| # Setup for Prettier |
| - name: Setup Node.js |
| uses: actions/setup-node@v4 |
| with: |
| node-version: "20" |
| cache: "npm" |
| cache-dependency-path: webui/package-lock.json |
| |
| - name: Install dependencies |
| working-directory: ./webui |
| run: npm ci |
| |
| # Setup for gofumpt |
| - name: Setup Go |
| uses: actions/setup-go@v4 |
| with: |
| go-version: stable |
| cache: true |
| |
| - name: Install gofumpt v0.8.0 |
| run: | |
| go install mvdan.cc/gofumpt@v0.8.0 |
| echo "$(go env GOPATH)/bin" >> $GITHUB_PATH |
| |
| - name: Check formatting |
| if: inputs.auto_fix != true |
| run: bin/run-formatters.sh check |
| |
| - name: Fix formatting |
| if: inputs.auto_fix == true |
| run: bin/run-formatters.sh fix |
| |
| # Commit formatting fixes if auto_fix is true |
| - name: Commit and push formatting fixes if needed |
| if: inputs.auto_fix == true |
| run: | |
| # Only proceed if there are changes to commit |
| if [[ -z $(git status --porcelain) ]]; then |
| echo "No formatting changes detected, skipping commit" |
| exit 0 |
| fi |
| |
| git config --global user.name "Autoformatter" |
| git config --global user.email "bot@sketch.dev" |
| git add . |
| |
| git fetch origin main |
| MERGE_BASE=$(git merge-base HEAD origin/main) |
| COMMIT_COUNT=$(git rev-list --count $MERGE_BASE..HEAD) |
| |
| if [ "$COMMIT_COUNT" -eq 1 ]; then |
| echo "Found exactly one commit since merge-base with origin/main. Amending the commit." |
| git commit --amend --no-edit |
| else |
| echo "Found multiple commits ($COMMIT_COUNT) since merge-base with origin/main. Creating a new commit." |
| git commit -m "all: fix formatting" |
| fi |
| |
| git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/${GITHUB_REPOSITORY}.git HEAD |
| |
| - name: Get commit SHA |
| id: get_commit |
| run: echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT |