Format #91
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| name: Format | |
| # Runs ktfmt and rustfmt over the codebase on a daily schedule and opens a PR | |
| # only if a formatter produced changes. Mirrors the coverage-badge cron: it | |
| # force-pushes a dedicated, bot-owned branch so re-runs reuse one PR instead of | |
| # accumulating stale branches, and it never touches master directly. | |
| on: | |
| schedule: | |
| # 12:00 UTC daily (one hour before the coverage-badge cron). | |
| - cron: '0 12 * * *' | |
| workflow_dispatch: | |
| jobs: | |
| format: | |
| runs-on: ubuntu-latest | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| steps: | |
| - name: Setup Java JDK | |
| uses: actions/setup-java@v4 | |
| with: | |
| distribution: 'temurin' | |
| java-version: '21' | |
| - name: Setup Go environment | |
| uses: actions/setup-go@v5 | |
| with: | |
| go-version: ^1.17 | |
| id: go | |
| - name: Setup Bazelisk | |
| # Retries guard against transient proxy.golang.org module-fetch errors. | |
| run: | | |
| for i in 1 2 3; do | |
| go install github.com/bazelbuild/bazelisk@latest && break | |
| echo "bazelisk install failed (attempt $i); retrying in 10s..." | |
| sleep 10 | |
| done | |
| export PATH=$PATH:$(go env GOPATH)/bin | |
| - uses: actions/checkout@v4 | |
| - name: Run ktfmt | |
| # Pinned to the repo's .bazelversion (no USE_BAZEL_VERSION) and run with | |
| # --lockfile_mode=off so MODULE.bazel.lock is never rewritten, keeping the | |
| # generated PR limited to formatting-only changes. | |
| run: ~/go/bin/bazelisk run //cli/format --enable_bzlmod=true --enable_workspace=false --lockfile_mode=off | |
| - name: Run rustfmt | |
| run: ~/go/bin/bazelisk run //cli/format:rustfmt --enable_bzlmod=true --enable_workspace=false --lockfile_mode=off | |
| - name: Open PR if formatting changed | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| run: | | |
| if [ -z "$(git status --porcelain)" ]; then | |
| echo "No formatting changes; nothing to do." | |
| exit 0 | |
| fi | |
| BRANCH="ci/format" | |
| git config user.name "github-actions[bot]" | |
| git config user.email "41898282+github-actions[bot]@users.noreply.github.com" | |
| git checkout -B "$BRANCH" | |
| git add -A | |
| git commit -m "ci: apply ktfmt and rustfmt formatting" | |
| # Force-push the dedicated, bot-owned branch so re-runs reuse one PR | |
| # instead of accumulating stale branches. This never touches master. | |
| git push --force origin "$BRANCH" | |
| if gh pr view "$BRANCH" --json state --jq '.state' 2>/dev/null | grep -q OPEN; then | |
| echo "PR already open for $BRANCH; it now points at the latest formatting." | |
| else | |
| gh pr create \ | |
| --base master \ | |
| --head "$BRANCH" \ | |
| --title "ci: apply ktfmt and rustfmt formatting" \ | |
| --body "Automated ktfmt and rustfmt run via \`bazel run //cli/format\` and \`bazel run //cli/format:rustfmt\`. This PR contains formatting-only changes." | |
| fi |