Skip to content

Commit e033a16

Browse files
ci: add daily buildifier formatting cron that opens a PR on changes (#375)
Adds a //cli/format:buildifier format_multirun target (backed by a new buildifier_prebuilt dev dependency) and a daily cron that runs it over all Starlark/BUILD files, opening a PR only when buildifier produced changes. Mirrors the ktfmt format cron: force-pushes a dedicated bot-owned branch (ci/buildifier-format) so re-runs reuse one PR, and never touches master. A .gitattributes marks cli/src/test/resources/** as rules-lint-ignored so neither formatter rewrites the deliberately-constructed E2E test fixtures (e.g. module_bazel_comment's comment-preservation scenario). Both format crons now run pinned to .bazelversion with --lockfile_mode=off so MODULE.bazel.lock is never relocked mid-run, keeping the generated PRs limited to formatting-only changes. Co-authored-by: Claude Opus 4.8 <noreply@anthropic.com>
1 parent c32c4cb commit e033a16

6 files changed

Lines changed: 520 additions & 118 deletions

File tree

.gitattributes

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Test fixture workspaces are deliberately constructed inputs to the E2E tests
2+
# (e.g. module_bazel_comment exercises comment preservation, cquery_failing_target
3+
# encodes a specific failure scenario). Exclude them from aspect_rules_lint
4+
# formatters (buildifier/ktfmt) so the cron jobs never rewrite the very content
5+
# under test. `rules-lint-ignored` is honored by //cli/format and //cli/format:buildifier.
6+
cli/src/test/resources/** rules-lint-ignored

.github/workflows/buildifier.yaml

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

.github/workflows/format.yaml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ jobs:
3131
run: go install github.com/bazelbuild/bazelisk@latest && export PATH=$PATH:$(go env GOPATH)/bin
3232
- uses: actions/checkout@v4
3333
- 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
34+
# Pinned to the repo's .bazelversion (no USE_BAZEL_VERSION) and run with
35+
# --lockfile_mode=off so MODULE.bazel.lock is never rewritten, keeping the
36+
# generated PR limited to formatting-only changes.
37+
run: ~/go/bin/bazelisk run //cli/format --enable_bzlmod=true --enable_workspace=false --lockfile_mode=off
3738
- name: Open PR if formatting changed
3839
env:
3940
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}

MODULE.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module(
66

77
bazel_dep(name = "rules_license", version = "1.0.0", dev_dependency = True)
88
bazel_dep(name = "aspect_rules_lint", version = "2.1.0", dev_dependency = True)
9+
bazel_dep(name = "buildifier_prebuilt", version = "8.5.1.2", dev_dependency = True)
910
bazel_dep(name = "aspect_bazel_lib", version = "2.22.5", dev_dependency = True)
1011

1112
bazel_dep(name = "bazel_skylib", version = "1.9.0")

0 commit comments

Comments
 (0)