From df09437df672cf28f9a4c910e5502ea820ea7699 Mon Sep 17 00:00:00 2001 From: Drewry Pope Date: Tue, 3 Nov 2026 02:17:06 -0600 Subject: [PATCH] ci(auto-format): push and record success with the REPO_AUTOMATION_TOKEN PAT MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The workflow token can never write repository variables (the workflow permissions vocabulary has no such key — proven by probe dispatches, see #4787) and cannot push workflow files (#3118). A fine-grained PAT (developing-today-code-automation: Actions, Variables, Contents, Issues, Pull requests, commit statuses and Workflows read/write, Metadata read-only, this repo only, expires 2027-11-04) now lives in the REPO_AUTOMATION_TOKEN secret, with a copy in Bitwarden. Checkout and record_success use it; the workflow-file exclusion is gone since the PAT may push workflow files. Token verified end-to-end against the variables API before this change. The laptop-side variable sync stays as a backstop until an in-workflow record is observed. --- .github/workflows/auto-format.yml | 39 ++++++++++++------------------- 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/.github/workflows/auto-format.yml b/.github/workflows/auto-format.yml index 8f88b2b3c9..91cddeebd1 100644 --- a/.github/workflows/auto-format.yml +++ b/.github/workflows/auto-format.yml @@ -21,6 +21,13 @@ name: auto-format # missing, stale, or not an ancestor of HEAD, the run falls back to # the last 25 hours of changed files, and a failed variable update # only means the next run re-checks the same range. +# +# Pushes and variable writes use the REPO_AUTOMATION_TOKEN secret +# (fine-grained PAT "developing-today-code-automation", this repo +# only): the default GITHUB_TOKEN cannot write repository variables +# (no such key exists in the workflow permissions vocabulary) and +# cannot push workflow files (#3118). The PAT does both, and its +# pushes trigger downstream checks like a human push. on: schedule: @@ -44,6 +51,7 @@ jobs: steps: - uses: actions/checkout@v7 with: + token: ${{ secrets.REPO_AUTOMATION_TOKEN }} fetch-depth: 0 - uses: DeterminateSystems/nix-installer-action@3138316df39ed29be04236d7ffc686fa525866aa # v23 @@ -51,6 +59,7 @@ jobs: - name: Format changed files with treefmt and commit any fixes env: LAST_SUCCESS_SHA: ${{ vars.AUTO_FORMAT_LAST_SUCCESS_SHA }} + REPO_AUTOMATION_TOKEN: ${{ secrets.REPO_AUTOMATION_TOKEN }} GH_TOKEN: ${{ github.token }} run: | set -euo pipefail @@ -61,9 +70,9 @@ jobs: record_success() { local sha sha="$(git rev-parse HEAD)" - if gh api -X PATCH "repos/$GITHUB_REPOSITORY/actions/variables/AUTO_FORMAT_LAST_SUCCESS_SHA" -f name="AUTO_FORMAT_LAST_SUCCESS_SHA" -f value="$sha" >/dev/null 2>&1; then + if GH_TOKEN="$REPO_AUTOMATION_TOKEN" gh api -X PATCH "repos/$GITHUB_REPOSITORY/actions/variables/AUTO_FORMAT_LAST_SUCCESS_SHA" -f name="AUTO_FORMAT_LAST_SUCCESS_SHA" -f value="$sha" >/dev/null 2>&1; then echo "Recorded AUTO_FORMAT_LAST_SUCCESS_SHA=$sha" - elif gh api -X POST "repos/$GITHUB_REPOSITORY/actions/variables" -f name="AUTO_FORMAT_LAST_SUCCESS_SHA" -f value="$sha" >/dev/null 2>&1; then + elif GH_TOKEN="$REPO_AUTOMATION_TOKEN" gh api -X POST "repos/$GITHUB_REPOSITORY/actions/variables" -f name="AUTO_FORMAT_LAST_SUCCESS_SHA" -f value="$sha" >/dev/null 2>&1; then echo "Created AUTO_FORMAT_LAST_SUCCESS_SHA=$sha" else echo "::warning::could not update AUTO_FORMAT_LAST_SUCCESS_SHA; the next run will re-check this range." @@ -93,28 +102,10 @@ jobs: record_success exit 0 fi - # The bot pushes with the default GITHUB_TOKEN, which lacks - # workflow authorization: staging a workflow formatting diff - # rejects the whole push and loses the unrelated fixes with - # it (#3118). Workflow files are excluded from this write - # pass only; treefmt still formats them everywhere else. - format_files=() - skipped_workflow_files=() - for f in "${changed_files[@]}"; do - case "$f" in - .github/workflows/*) skipped_workflow_files+=("$f") ;; - *) format_files+=("$f") ;; - esac - done - if ((${#skipped_workflow_files[@]} > 0)); then - printf 'Skipping workflow file(s) the bot cannot push:\n' - printf ' %s\n' "${skipped_workflow_files[@]}" - fi - if ((${#format_files[@]} == 0)); then - echo "Only workflow files changed; nothing for the auto-format bot to push." - record_success - exit 0 - fi + # The push authenticates with the REPO_AUTOMATION_TOKEN + # PAT (see header), which may push workflow files, so + # nothing is excluded from the write pass. + format_files=("${changed_files[@]}") printf 'Formatting %s file(s):\n' "${#format_files[@]}" printf ' %s\n' "${format_files[@]}" nix run .#format -- "${format_files[@]}"