|
| 1 | +name: Format |
| 2 | + |
| 3 | +# Runs ktfmt over the codebase on a daily schedule and opens a PR only if the |
| 4 | +# formatter produced changes. Mirrors the coverage-badge cron: it force-pushes a |
| 5 | +# dedicated, bot-owned branch so re-runs reuse one PR instead of accumulating |
| 6 | +# stale branches, and it never touches master directly. |
| 7 | +on: |
| 8 | + schedule: |
| 9 | + # 12:00 UTC daily (one hour before the coverage-badge cron). |
| 10 | + - cron: '0 12 * * *' |
| 11 | + workflow_dispatch: |
| 12 | + |
| 13 | +jobs: |
| 14 | + format: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + permissions: |
| 17 | + contents: write |
| 18 | + pull-requests: write |
| 19 | + steps: |
| 20 | + - name: Setup Java JDK |
| 21 | + uses: actions/setup-java@v4 |
| 22 | + with: |
| 23 | + distribution: 'temurin' |
| 24 | + java-version: '21' |
| 25 | + - name: Setup Go environment |
| 26 | + uses: actions/setup-go@v5 |
| 27 | + with: |
| 28 | + go-version: ^1.17 |
| 29 | + id: go |
| 30 | + - name: Setup Bazelisk |
| 31 | + run: go install github.com/bazelbuild/bazelisk@latest && export PATH=$PATH:$(go env GOPATH)/bin |
| 32 | + - uses: actions/checkout@v4 |
| 33 | + - name: Run ktfmt |
| 34 | + env: |
| 35 | + USE_BAZEL_VERSION: '9.x' |
| 36 | + run: ~/go/bin/bazelisk run //cli/format --enable_bzlmod=true --enable_workspace=false |
| 37 | + - name: Open PR if formatting changed |
| 38 | + env: |
| 39 | + GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} |
| 40 | + run: | |
| 41 | + if [ -z "$(git status --porcelain)" ]; then |
| 42 | + echo "No formatting changes; nothing to do." |
| 43 | + exit 0 |
| 44 | + fi |
| 45 | + BRANCH="ci/ktfmt-format" |
| 46 | + git config user.name "github-actions[bot]" |
| 47 | + git config user.email "41898282+github-actions[bot]@users.noreply.github.com" |
| 48 | + git checkout -B "$BRANCH" |
| 49 | + git add -A |
| 50 | + git commit -m "ci: apply ktfmt formatting" |
| 51 | + # Force-push the dedicated, bot-owned branch so re-runs reuse one PR |
| 52 | + # instead of accumulating stale branches. This never touches master. |
| 53 | + git push --force origin "$BRANCH" |
| 54 | + if gh pr view "$BRANCH" --json state --jq '.state' 2>/dev/null | grep -q OPEN; then |
| 55 | + echo "PR already open for $BRANCH; it now points at the latest formatting." |
| 56 | + else |
| 57 | + gh pr create \ |
| 58 | + --base master \ |
| 59 | + --head "$BRANCH" \ |
| 60 | + --title "ci: apply ktfmt formatting" \ |
| 61 | + --body "Automated ktfmt run via \`bazel run //cli/format\`. This PR contains formatting-only changes." |
| 62 | + fi |
0 commit comments