Skip to content

Commit 74ea588

Browse files
authored
ci: harden auto-approve against self-modification (handle #159) (#160)
* docs: capture auto-approve self-modification lesson and other session learnings Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca> * ci: harden auto-approve against self-modification (handle #159) - Make wf-changes detection resilient (|| echo) so it cannot fail the job after the approve step has run. - Use || echo on enable auto-merge (pattern from patchloom) so the workflow run succeeds even on GITHUB_TOKEN fallback for workflow-touching PRs. - Approval step remains early and unconditional for trusted actors. - Update AGENTS.md gotcha note with the resilience details and reference to patchloom + ci-branch-protection. This ensures reviews are submitted (when PR events run the main definition) and Auto-approve check reports success for such infra PRs. Fixes #159 Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca> --------- Signed-off-by: Sebastien Tardif <sebtardif@ncf.ca>
1 parent d2dfe21 commit 74ea588

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

.github/workflows/auto-approve.yml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,14 +34,16 @@ jobs:
3434
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
3535
run: gh pr review --approve "${{ github.event.pull_request.number }}" --repo "${{ github.repository }}"
3636

37-
# Check for workflow file changes early (to decide whether we can safely use App token for auto-merge)
37+
# Check for workflow file changes early (to decide whether we can safely use App token for auto-merge).
38+
# This step is made robust so a transient gh failure does not fail the job (approve step already ran).
3839
- name: Check for workflow file changes (use GITHUB_TOKEN fallback to avoid needing workflows:write on App)
3940
id: wf-changes
4041
env:
4142
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
4243
run: |
4344
pr="${{ github.event.pull_request.number }}"
44-
if gh pr view "$pr" --json files --jq '.files[].path' | grep -q '^\.github/workflows/'; then
45+
files=$(gh pr view "$pr" --json files --jq '.files[].path' || echo "")
46+
if echo "$files" | grep -q '^\.github/workflows/'; then
4547
echo "changes=true" >> "$GITHUB_OUTPUT"
4648
echo "PR touches .github/workflows/; will use GITHUB_TOKEN for auto-merge (no workflows:write needed on App)"
4749
else
@@ -79,4 +81,6 @@ jobs:
7981
steps.release-guard.outputs.is_release_pr != 'true'
8082
env:
8183
GH_TOKEN: ${{ steps.app-token.outputs.token || secrets.GITHUB_TOKEN }}
82-
run: gh pr merge --auto --squash "${{ github.event.pull_request.number }}" --repo "${{ github.repository }}"
84+
run: |
85+
gh pr merge --auto --squash "${{ github.event.pull_request.number }}" --repo "${{ github.repository }}" \
86+
|| echo "Could not enable auto-merge (common when PR modifies .github/workflows/* using GITHUB_TOKEN fallback, or release guard, or other). Approval from prior step still applies; use manual merge if needed."

AGENTS.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,16 @@ All I/O-dependent functions accept an `inputs` object with injectable callbacks
133133
changes are ready for review/merge. This ensures every pushed branch is
134134
backed by an open (draft) PR from the start. See `~/.grok/skills/owned-repo-gate/SKILL.md`.
135135

136+
- **Auto-approve self-modification:** PRs that change `.github/workflows/auto-approve.yml`
137+
cause GitHub to emit only "push" validation runs (0 jobs, failure) using the PR's workflow content
138+
(the pull_request runs use the definition from main). The approve step runs early using
139+
GITHUB_TOKEN (before wf-changes detection or merge logic) so reviews are added when the
140+
pull_request workflow runs from main. The Enable auto-merge step uses `|| echo` so the
141+
workflow reports success even when merge enable falls back or is restricted. In rare cases
142+
where no review appears, use the emergency bypass in ci-branch-protection skill + #159
143+
(add bypass actor, `gh pr merge --admin`, remove bypass immediately). See also patchloom's
144+
auto-approve.yml for the reference pattern.
145+
136146
## Release PRs - Strong Guard
137147

138148
Release PRs (created by release-please, titled "chore: release ..." or "chore(main): release ...", or labeled `autorelease: pending`) MUST NEVER be merged (with `gh pr merge`, `--auto`, or otherwise) without the user's explicit approval.

0 commit comments

Comments
 (0)