From 8ec01fd1684cd695708dd213f18bee7caa947b97 Mon Sep 17 00:00:00 2001 From: donpetry-bot <{}+donpetry-bot@users.noreply.github.com> Date: Tue, 9 Jun 2026 21:44:15 +0000 Subject: [PATCH 1/5] =?UTF-8?q?feat:=20implement=20issue=20#405=20?= =?UTF-8?q?=E2=80=94=20petry-projects=20=E2=80=94=20workflow=20failures=20?= =?UTF-8?q?detected=202026-05-28?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- scripts/dev-lead-intent.sh | 9 ++++ scripts/lib/pr-worktree.sh | 14 ++++++ .../issue_comment_sonarqube_closed_pr.json | 23 +++++++++ tests/dev-lead/unit/test_intent_reviews.bats | 14 ++++++ tests/dev-lead/unit/test_pr_worktree.bats | 48 +++++++++++++++++-- 5 files changed, 104 insertions(+), 4 deletions(-) create mode 100644 tests/dev-lead/fixtures/events/issue_comment_sonarqube_closed_pr.json diff --git a/scripts/dev-lead-intent.sh b/scripts/dev-lead-intent.sh index 0a4b769a4..e09fde37c 100755 --- a/scripts/dev-lead-intent.sh +++ b/scripts/dev-lead-intent.sh @@ -303,6 +303,15 @@ case "$EVENT_NAME" in exit 0 fi + # Skip comments on closed/merged PRs — nothing to modify on a merged branch. + # This is a fast-path guard; checkout_pr_in_worktree provides a deeper safety + # net, but failing early here avoids spending tokens on a no-op run (issue #405). + pr_state=$(jq -r '.issue.state // empty' "$EVENT_PATH" 2>/dev/null || true) + if [ "${pr_state:-}" = "closed" ]; then + emit_skip "pr-already-closed" + exit 0 + fi + commenter=$(jq -r '.comment.user.login // empty' "$EVENT_PATH" 2>/dev/null || true) comment_body=$(jq -r '.comment.body // empty' "$EVENT_PATH" 2>/dev/null || true) pr_number=$(jq -r '.issue.number // empty' "$EVENT_PATH" 2>/dev/null || true) diff --git a/scripts/lib/pr-worktree.sh b/scripts/lib/pr-worktree.sh index c5487b2ac..19c2d56b1 100644 --- a/scripts/lib/pr-worktree.sh +++ b/scripts/lib/pr-worktree.sh @@ -51,6 +51,19 @@ PR_WORKTREE_AGENT_DIR="" checkout_pr_in_worktree() { local pr="$1" repo="$2" + # Exit cleanly when the PR is already merged or closed (issue #405). + # `gh pr checkout` fails with "fatal: couldn't find remote ref" when the + # branch was deleted after merging. Detect this early and skip gracefully. + # MUST run before the EXIT trap is registered — if exit 0 fires after the + # trap is set, the trap captures and replays the caller's EXIT handler + # prematurely (observable as bats reporting "Executed 2 instead of 1 tests"). + local _pr_state="" + _pr_state="$(gh pr view "$pr" --repo "$repo" --json state --jq '.state' 2>/dev/null || true)" + if [ -n "$_pr_state" ] && [ "$_pr_state" != "OPEN" ]; then + echo "::notice::PR #${pr} is ${_pr_state} — skipping checkout (nothing to modify on a merged/closed PR)" + exit 0 + fi + PR_WORKTREE_AGENT_DIR="$(pwd)" # mktemp -d gives us a private parent dir; the worktree leaf must NOT pre-exist @@ -82,6 +95,7 @@ checkout_pr_in_worktree() { # and release whatever worktree currently holds it: detach the main agent # checkout in place (it cannot be removed), force-remove any other. git worktree prune 2>/dev/null || true + local _pr_branch="" _pr_branch="$(gh pr view "$pr" --repo "$repo" --json headRefName --jq '.headRefName' 2>/dev/null || true)" if [ -n "$_pr_branch" ]; then diff --git a/tests/dev-lead/fixtures/events/issue_comment_sonarqube_closed_pr.json b/tests/dev-lead/fixtures/events/issue_comment_sonarqube_closed_pr.json new file mode 100644 index 000000000..66834678f --- /dev/null +++ b/tests/dev-lead/fixtures/events/issue_comment_sonarqube_closed_pr.json @@ -0,0 +1,23 @@ +{ + "_test_expected_intent": "skip", + "action": "created", + "issue": { + "number": 519, + "title": "feat(pr-review): mirror dispatch inputs as workflow_call inputs", + "state": "closed", + "pull_request": { + "url": "https://api.github.com/repos/petry-projects/.github-private/pulls/519", + "merged_at": "2026-06-09T21:00:00Z" + } + }, + "comment": { + "id": 4001, + "body": "## Quality Gate passed\n\nNo new issues.", + "user": { + "login": "sonarqubecloud[bot]", + "type": "Bot" + } + }, + "repository": { "full_name": "petry-projects/.github-private" }, + "sender": { "login": "sonarqubecloud[bot]", "type": "Bot" } +} diff --git a/tests/dev-lead/unit/test_intent_reviews.bats b/tests/dev-lead/unit/test_intent_reviews.bats index cf6418932..d7d0d1fd2 100644 --- a/tests/dev-lead/unit/test_intent_reviews.bats +++ b/tests/dev-lead/unit/test_intent_reviews.bats @@ -273,3 +273,17 @@ EOF [ "$status" -eq 0 ] [ "$(_get_env INTENT_TYPE)" = "rebase" ] } + +@test "reviews: issue_comment trusted-bot on already-closed PR → skip pr-already-closed" { + # Regression guard for issue #405: a SonarQube comment on a merged PR used to + # trigger fix-bot-comment, which then crashed at checkout_pr_in_worktree because + # the branch was deleted. The classifier must emit skip before that point. + export GITHUB_EVENT_NAME="issue_comment" + export GITHUB_EVENT_PATH="$FIXTURES_DIR/issue_comment_sonarqube_closed_pr.json" + + run bash "$INTENT_SCRIPT" + + [ "$status" -eq 0 ] + [ "$(_get_env INTENT_TYPE)" = "skip" ] + [ "$(_get_env INTENT_REASON)" = "pr-already-closed" ] +} diff --git a/tests/dev-lead/unit/test_pr_worktree.bats b/tests/dev-lead/unit/test_pr_worktree.bats index d34e50344..a0b9d45bf 100644 --- a/tests/dev-lead/unit/test_pr_worktree.bats +++ b/tests/dev-lead/unit/test_pr_worktree.bats @@ -11,17 +11,25 @@ LIB="$SCRIPT_DIR/scripts/lib/pr-worktree.sh" setup() { STUB_BIN_DIR="$(mktemp -d)" + # PR_STATE_API controls the value returned for `gh pr view --json state` queries. + # Defaults to "OPEN" so existing tests are unaffected; set to "MERGED" or + # "CLOSED" in individual tests to exercise the merged/closed-PR early-exit path. + export PR_STATE_API="${PR_STATE_API:-OPEN}" + # gh stub: `gh pr checkout ` switches the worktree to the PR branch, # mirroring the real command (which is what made the old prompt files vanish). # `gh pr view --json headRefName --jq .headRefName` returns the PR's head # branch (`old` in this fixture) so the collision-prevention logic in # checkout_pr_in_worktree can identify and release a stale holder (issue #470). + # `gh pr view --json state --jq .state` returns ${PR_STATE_API:-OPEN} so + # individual tests can exercise the merged/closed-PR guard (issue #405). cat > "$STUB_BIN_DIR/gh" <<'GHEOF' #!/usr/bin/env bash -case "$1 $2" in - "pr checkout") git checkout -q old ;; - "pr view") echo "old" ;; - *) echo "{}" ;; +case "$*" in + *"--json state"*) echo "${PR_STATE_API:-OPEN}" ;; + "pr checkout"*) git checkout -q old ;; + "pr view"*) echo "old" ;; + *) echo "{}" ;; esac GHEOF chmod +x "$STUB_BIN_DIR/gh" @@ -224,3 +232,35 @@ teardown() { run git -C "$AGENT_REPO" symbolic-ref -q HEAD [ "$status" -ne 0 ] } + +# --- issue #405: merged/closed PR early-exit guard --------------------------- +# When a trusted-bot comment arrives on an already-merged PR (branch deleted), +# checkout_pr_in_worktree must exit 0 cleanly rather than crashing with +# "fatal: couldn't find remote ref" from `gh pr checkout`. + +@test "checkout_pr_in_worktree: exits cleanly when PR is already merged (MERGED state)" { + source "$LIB" + cd "$AGENT_REPO" + export PR_STATE_API="MERGED" + + run checkout_pr_in_worktree 519 owner/repo + + [ "$status" -eq 0 ] + [[ "$output" == *"PR #519 is MERGED"* ]] + [[ "$output" == *"skipping checkout"* ]] + # The agent repo must NOT have been switched to another branch by gh pr checkout. + [ "$(git -C "$AGENT_REPO" rev-parse --abbrev-ref HEAD)" = "main" ] +} + +@test "checkout_pr_in_worktree: exits cleanly when PR is closed without merge (CLOSED state)" { + source "$LIB" + cd "$AGENT_REPO" + export PR_STATE_API="CLOSED" + + run checkout_pr_in_worktree 99 owner/repo + + [ "$status" -eq 0 ] + [[ "$output" == *"PR #99 is CLOSED"* ]] + [[ "$output" == *"skipping checkout"* ]] + [ "$(git -C "$AGENT_REPO" rev-parse --abbrev-ref HEAD)" = "main" ] +} From 5948b52787a0e7bf13f3c8f8e673241fc583a220 Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Tue, 9 Jun 2026 21:49:14 +0000 Subject: [PATCH 2/5] fix(bot): address bot feedback [skip ci-relay] --- scripts/lib/pr-worktree.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scripts/lib/pr-worktree.sh b/scripts/lib/pr-worktree.sh index 19c2d56b1..2d71f7a1d 100644 --- a/scripts/lib/pr-worktree.sh +++ b/scripts/lib/pr-worktree.sh @@ -59,7 +59,7 @@ checkout_pr_in_worktree() { # prematurely (observable as bats reporting "Executed 2 instead of 1 tests"). local _pr_state="" _pr_state="$(gh pr view "$pr" --repo "$repo" --json state --jq '.state' 2>/dev/null || true)" - if [ -n "$_pr_state" ] && [ "$_pr_state" != "OPEN" ]; then + if [ "$_pr_state" = "MERGED" ] || [ "$_pr_state" = "CLOSED" ]; then echo "::notice::PR #${pr} is ${_pr_state} — skipping checkout (nothing to modify on a merged/closed PR)" exit 0 fi From d12afaf3653a467fe7541173c0c27982ee8d9589 Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Tue, 9 Jun 2026 21:55:18 +0000 Subject: [PATCH 3/5] chore: apply manual instructions [skip ci-relay] --- scripts/lib/pr-worktree.sh | 12 ++++++++---- tests/dev-lead/unit/test_pr_worktree.bats | 22 +++++++++------------- 2 files changed, 17 insertions(+), 17 deletions(-) diff --git a/scripts/lib/pr-worktree.sh b/scripts/lib/pr-worktree.sh index 2d71f7a1d..732749258 100644 --- a/scripts/lib/pr-worktree.sh +++ b/scripts/lib/pr-worktree.sh @@ -57,9 +57,15 @@ checkout_pr_in_worktree() { # MUST run before the EXIT trap is registered — if exit 0 fires after the # trap is set, the trap captures and replays the caller's EXIT handler # prematurely (observable as bats reporting "Executed 2 instead of 1 tests"). + local _pr_json="" local _pr_state="" - _pr_state="$(gh pr view "$pr" --repo "$repo" --json state --jq '.state' 2>/dev/null || true)" - if [ "$_pr_state" = "MERGED" ] || [ "$_pr_state" = "CLOSED" ]; then + local _pr_branch="" + _pr_json="$(gh pr view "$pr" --repo "$repo" --json state,headRefName 2>/dev/null || true)" + if [ -n "$_pr_json" ]; then + _pr_state="$(echo "$_pr_json" | jq -r '.state // empty' 2>/dev/null || true)" + _pr_branch="$(echo "$_pr_json" | jq -r '.headRefName // empty' 2>/dev/null || true)" + fi + if [ -n "$_pr_state" ] && [ "$_pr_state" != "OPEN" ]; then echo "::notice::PR #${pr} is ${_pr_state} — skipping checkout (nothing to modify on a merged/closed PR)" exit 0 fi @@ -96,8 +102,6 @@ checkout_pr_in_worktree() { # checkout in place (it cannot be removed), force-remove any other. git worktree prune 2>/dev/null || true - local _pr_branch="" - _pr_branch="$(gh pr view "$pr" --repo "$repo" --json headRefName --jq '.headRefName' 2>/dev/null || true)" if [ -n "$_pr_branch" ]; then # Resolve the agent worktree's ROOT for comparison. `git worktree list` # emits each worktree's top-level, fully-resolved path, but diff --git a/tests/dev-lead/unit/test_pr_worktree.bats b/tests/dev-lead/unit/test_pr_worktree.bats index a0b9d45bf..4c889a2e2 100644 --- a/tests/dev-lead/unit/test_pr_worktree.bats +++ b/tests/dev-lead/unit/test_pr_worktree.bats @@ -11,25 +11,21 @@ LIB="$SCRIPT_DIR/scripts/lib/pr-worktree.sh" setup() { STUB_BIN_DIR="$(mktemp -d)" - # PR_STATE_API controls the value returned for `gh pr view --json state` queries. - # Defaults to "OPEN" so existing tests are unaffected; set to "MERGED" or - # "CLOSED" in individual tests to exercise the merged/closed-PR early-exit path. - export PR_STATE_API="${PR_STATE_API:-OPEN}" + # PR_STATE_API controls the state returned by the gh stub's `pr view` JSON response. + # Set unconditionally so the runner environment cannot leak in and break unrelated tests. + export PR_STATE_API="OPEN" # gh stub: `gh pr checkout ` switches the worktree to the PR branch, # mirroring the real command (which is what made the old prompt files vanish). - # `gh pr view --json headRefName --jq .headRefName` returns the PR's head - # branch (`old` in this fixture) so the collision-prevention logic in - # checkout_pr_in_worktree can identify and release a stale holder (issue #470). - # `gh pr view --json state --jq .state` returns ${PR_STATE_API:-OPEN} so - # individual tests can exercise the merged/closed-PR guard (issue #405). + # `gh pr view` returns a JSON object with state and headRefName so the combined + # call in checkout_pr_in_worktree handles both the closed-PR guard (issue #405) + # and the stale-worktree release (issue #470) in a single round-trip. cat > "$STUB_BIN_DIR/gh" <<'GHEOF' #!/usr/bin/env bash case "$*" in - *"--json state"*) echo "${PR_STATE_API:-OPEN}" ;; - "pr checkout"*) git checkout -q old ;; - "pr view"*) echo "old" ;; - *) echo "{}" ;; + "pr checkout"*) git checkout -q old ;; + "pr view"*) echo "{\"state\": \"${PR_STATE_API:-OPEN}\", \"headRefName\": \"old\"}" ;; + *) echo "{}" ;; esac GHEOF chmod +x "$STUB_BIN_DIR/gh" From 059dd2ef8db731019a680217f072c67c2e748bd2 Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Wed, 10 Jun 2026 18:45:07 +0000 Subject: [PATCH 4/5] chore: apply manual instructions [skip ci-relay] --- .github/workflows/pr-review.yml | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/.github/workflows/pr-review.yml b/.github/workflows/pr-review.yml index 5e01abbcf..7d943b1e1 100644 --- a/.github/workflows/pr-review.yml +++ b/.github/workflows/pr-review.yml @@ -49,16 +49,16 @@ on: GH_PAT: required: false - # ── Reusable-only (#497 self-host pinning) ─────────────────────────────── - # Event-driven triggering now lives in the thin caller `pr-review-trigger.yml`, - # which invokes this workflow pinned to `pr-review.yml@pr-review/stable` and - # passes `agent_ref: pr-review/stable` so the agent's own scripts run at the - # pinned version too. Keeping this file reusable-only is what lets - # .github-private's OWN PR reviews run the known-good `stable` release — not - # whatever is on the branch under review — which is what breaks the - # self-hosting circular dependency. The job below still reads `github.event.*` - # (inherited from the caller's triggering event) and `inputs.*` (forwarded by - # the stub), so its behaviour is unchanged. +# ── Reusable-only (#497 self-host pinning) ─────────────────────────────── +# Event-driven triggering now lives in the thin caller `pr-review-trigger.yml`, +# which invokes this workflow pinned to `pr-review.yml@pr-review/stable` and +# passes `agent_ref: pr-review/stable` so the agent's own scripts run at the +# pinned version too. Keeping this file reusable-only is what lets +# .github-private's OWN PR reviews run the known-good `stable` release — not +# whatever is on the branch under review — which is what breaks the +# self-hosting circular dependency. The job below still reads `github.event.*` +# (inherited from the caller's triggering event) and `inputs.*` (forwarded by +# the stub), so its behaviour is unchanged. permissions: contents: read From c1dfe851a5547d99ff5270400ad7bdc8604b0294 Mon Sep 17 00:00:00 2001 From: donpetry-bot <281750570+donpetry-bot@users.noreply.github.com> Date: Wed, 10 Jun 2026 21:24:52 +0000 Subject: [PATCH 5/5] chore: apply manual instructions [skip ci-relay] --- scripts/dev-lead-intent.sh | 2 +- scripts/lib/pr-worktree.sh | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/dev-lead-intent.sh b/scripts/dev-lead-intent.sh index e09fde37c..fb25d19a9 100755 --- a/scripts/dev-lead-intent.sh +++ b/scripts/dev-lead-intent.sh @@ -307,7 +307,7 @@ case "$EVENT_NAME" in # This is a fast-path guard; checkout_pr_in_worktree provides a deeper safety # net, but failing early here avoids spending tokens on a no-op run (issue #405). pr_state=$(jq -r '.issue.state // empty' "$EVENT_PATH" 2>/dev/null || true) - if [ "${pr_state:-}" = "closed" ]; then + if [[ "${pr_state:-}" == "closed" ]]; then emit_skip "pr-already-closed" exit 0 fi diff --git a/scripts/lib/pr-worktree.sh b/scripts/lib/pr-worktree.sh index 732749258..a18b9dbdc 100644 --- a/scripts/lib/pr-worktree.sh +++ b/scripts/lib/pr-worktree.sh @@ -61,11 +61,11 @@ checkout_pr_in_worktree() { local _pr_state="" local _pr_branch="" _pr_json="$(gh pr view "$pr" --repo "$repo" --json state,headRefName 2>/dev/null || true)" - if [ -n "$_pr_json" ]; then + if [[ -n "$_pr_json" ]]; then _pr_state="$(echo "$_pr_json" | jq -r '.state // empty' 2>/dev/null || true)" _pr_branch="$(echo "$_pr_json" | jq -r '.headRefName // empty' 2>/dev/null || true)" fi - if [ -n "$_pr_state" ] && [ "$_pr_state" != "OPEN" ]; then + if [[ -n "$_pr_state" && "$_pr_state" != "OPEN" ]]; then echo "::notice::PR #${pr} is ${_pr_state} — skipping checkout (nothing to modify on a merged/closed PR)" exit 0 fi