From 9ef30a5c4cee94d14db6a9dd5fb8dd55bd3cecdc Mon Sep 17 00:00:00 2001 From: DJ Date: Thu, 16 Apr 2026 11:11:57 -0700 Subject: [PATCH 1/2] fix(dependabot-rebase): fall back to @dependabot rebase when update-branch blocked MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit GitHub blocks the update-branch API when the merge would introduce .github/workflows/ changes, regardless of which token is used — the token must have the GitHub App 'workflows' permission, which neither GITHUB_TOKEN nor the dependabot-automerge-petry app has. Fallback: when update-branch fails with a 'workflows permission' error, post '@dependabot rebase' as a PR comment. Dependabot always has permission to push to its own branches including workflow files. After Dependabot rebases, the dependabot-automerge workflow (pull_request_target/synchronize) re-approves the PR and the serialization chain continues. Also fix dead-code else/fi left over from earlier refactor. --- .../workflows/dependabot-rebase-reusable.yml | 29 ++++++++++++++----- 1 file changed, 22 insertions(+), 7 deletions(-) diff --git a/.github/workflows/dependabot-rebase-reusable.yml b/.github/workflows/dependabot-rebase-reusable.yml index 3a865f891..17512a671 100644 --- a/.github/workflows/dependabot-rebase-reusable.yml +++ b/.github/workflows/dependabot-rebase-reusable.yml @@ -91,10 +91,28 @@ jobs: if [[ "$BEHIND" -gt 0 ]]; then echo "PR #$PR_NUMBER ($HEAD_REF) is $BEHIND commit(s) behind — merging main into branch" - if gh api "repos/$REPO/pulls/$PR_NUMBER/update-branch" \ - -X PUT -f update_method=merge \ - --silent; then - echo " Branch updated" + UPDATE_OUTPUT=$(gh api "repos/$REPO/pulls/$PR_NUMBER/update-branch" \ + -X PUT -f update_method=merge 2>&1) + UPDATE_EXIT=$? + if [[ $UPDATE_EXIT -ne 0 ]]; then + echo "$UPDATE_OUTPUT" + if echo "$UPDATE_OUTPUT" | grep -q "workflows.*permission\|without.*permission"; then + # GitHub blocks update-branch when the merge would add .github/workflows/ + # changes, because the token lacks 'workflows' write permission. This applies + # to all tokens (GITHUB_TOKEN and GitHub App tokens alike). + # Fallback: post @dependabot rebase so Dependabot rebases its own branch — + # Dependabot has the necessary permissions. After rebase, the automerge + # workflow fires (pull_request_target/synchronize) and re-approves. + echo " update-branch blocked (workflows permission) — posting @dependabot rebase as fallback" + GH_TOKEN="$APP_TOKEN" gh pr comment "$PR_NUMBER" --repo "$REPO" \ + --body "@dependabot rebase" + else + echo "Warning: failed to update PR #$PR_NUMBER" + fi + continue + fi + if [[ -n "$UPDATE_OUTPUT" ]]; then echo "$UPDATE_OUTPUT"; fi + echo " Branch updated" # Only re-approve if auto-merge is enabled. Auto-merge is set exclusively # by the dependabot-automerge workflow after confirming eligibility (patch/ # minor updates or GitHub Actions). This guards against re-approving PRs @@ -122,9 +140,6 @@ jobs: else echo " Skipping re-approval — auto-merge not enabled (PR may require human review)" fi - else - echo "Warning: failed to update PR #$PR_NUMBER" - fi continue fi From f65f09519ce61fe8d69eaa13057efc689f6245b5 Mon Sep 17 00:00:00 2001 From: DJ Date: Thu, 16 Apr 2026 19:21:02 -0700 Subject: [PATCH 2/2] fix(dependabot-rebase): address review comments on workflows-403 fallback - Fix critical bash -e bug: use 'if UPDATE_OUTPUT=$(cmd)' pattern instead of capturing output then checking $?, which never runs under set -e - Narrow grep to exact string 'without `workflows` permission' to avoid false-positives from other permission errors - Add error handling for the @dependabot rebase comment posting - Make fallback idempotent: check for existing @dependabot rebase comment before posting to avoid spamming PRs on every push-to-main trigger --- .../workflows/dependabot-rebase-reusable.yml | 57 ++++++++++++------- 1 file changed, 35 insertions(+), 22 deletions(-) diff --git a/.github/workflows/dependabot-rebase-reusable.yml b/.github/workflows/dependabot-rebase-reusable.yml index 17512a671..66e76769e 100644 --- a/.github/workflows/dependabot-rebase-reusable.yml +++ b/.github/workflows/dependabot-rebase-reusable.yml @@ -91,28 +91,12 @@ jobs: if [[ "$BEHIND" -gt 0 ]]; then echo "PR #$PR_NUMBER ($HEAD_REF) is $BEHIND commit(s) behind — merging main into branch" - UPDATE_OUTPUT=$(gh api "repos/$REPO/pulls/$PR_NUMBER/update-branch" \ - -X PUT -f update_method=merge 2>&1) - UPDATE_EXIT=$? - if [[ $UPDATE_EXIT -ne 0 ]]; then - echo "$UPDATE_OUTPUT" - if echo "$UPDATE_OUTPUT" | grep -q "workflows.*permission\|without.*permission"; then - # GitHub blocks update-branch when the merge would add .github/workflows/ - # changes, because the token lacks 'workflows' write permission. This applies - # to all tokens (GITHUB_TOKEN and GitHub App tokens alike). - # Fallback: post @dependabot rebase so Dependabot rebases its own branch — - # Dependabot has the necessary permissions. After rebase, the automerge - # workflow fires (pull_request_target/synchronize) and re-approves. - echo " update-branch blocked (workflows permission) — posting @dependabot rebase as fallback" - GH_TOKEN="$APP_TOKEN" gh pr comment "$PR_NUMBER" --repo "$REPO" \ - --body "@dependabot rebase" - else - echo "Warning: failed to update PR #$PR_NUMBER" - fi - continue - fi - if [[ -n "$UPDATE_OUTPUT" ]]; then echo "$UPDATE_OUTPUT"; fi - echo " Branch updated" + # Capture both stdout and stderr; use if-then to avoid bash -e terminating + # the step on non-zero exit before we can inspect the error message. + if UPDATE_OUTPUT=$(gh api "repos/$REPO/pulls/$PR_NUMBER/update-branch" \ + -X PUT -f update_method=merge 2>&1); then + if [[ -n "$UPDATE_OUTPUT" ]]; then echo "$UPDATE_OUTPUT"; fi + echo " Branch updated" # Only re-approve if auto-merge is enabled. Auto-merge is set exclusively # by the dependabot-automerge workflow after confirming eligibility (patch/ # minor updates or GitHub Actions). This guards against re-approving PRs @@ -140,6 +124,35 @@ jobs: else echo " Skipping re-approval — auto-merge not enabled (PR may require human review)" fi + else + # update-branch failed — check if it's the workflows-permission 403 + echo " $UPDATE_OUTPUT" + if echo "$UPDATE_OUTPUT" | grep -qF "without \`workflows\` permission"; then + # GitHub blocks update-branch when merging main would add .github/workflows/ + # changes and the token (any token) lacks the GitHub App 'workflows' permission. + # Fallback: post @dependabot rebase. Dependabot has the correct permissions to + # push to its own branch. After rebase, the automerge workflow fires on + # pull_request_target/synchronize and re-approves. + # + # Idempotent: skip if a @dependabot rebase comment already exists to avoid + # spamming the PR on every push-to-main trigger. + ALREADY_REQUESTED=$(gh pr view "$PR_NUMBER" --repo "$REPO" \ + --json comments --jq '[.comments[] | select(.body == "@dependabot rebase")] | length') + if [[ "$ALREADY_REQUESTED" -gt 0 ]]; then + echo " Skipping — @dependabot rebase already requested (waiting for Dependabot to process)" + else + echo " Posting @dependabot rebase as fallback" + if GH_TOKEN="$APP_TOKEN" gh pr comment "$PR_NUMBER" --repo "$REPO" \ + --body "@dependabot rebase"; then + echo " Posted @dependabot rebase on PR #$PR_NUMBER" + else + echo " Warning: failed to post @dependabot rebase — PR #$PR_NUMBER will remain stuck" + fi + fi + else + echo " Warning: failed to update PR #$PR_NUMBER" + fi + fi continue fi