blob: 2920665c8ea99d8f642738e9791318572f56471a [file] [log] [blame]
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-**"
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: ${{ github.head_ref || 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 commit -m "all: fix formatting"
# If this is a queue-main branch, push the changes
if [[ "${{ github.ref }}" == refs/heads/queue-main-* ]]; then
git push -f https://${GITHUB_ACTOR}:${GITHUB_TOKEN}@github.com/boldsoftware/sketch.git HEAD
fi
- name: Get commit SHA
id: get_commit
run: echo "commit_sha=$(git rev-parse HEAD)" >> $GITHUB_OUTPUT