Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 23 additions & 41 deletions scripts/dev-lead-fix-reviews.sh
Original file line number Diff line number Diff line change
Expand Up @@ -481,15 +481,18 @@ has_tier1_blockers() {
[ "${failing_checks:-0}" -gt 0 ] || [ "${changes_requested:-0}" -gt 0 ] || [ "${unresolved_bot_threads:-0}" -gt 0 ]
}

# try_enable_auto_merge: enables auto-merge (squash) on the PR if reviewDecision is
# APPROVED and auto-merge is not already set. Safe to call speculatively — checks
# eligibility first and is idempotent if auto-merge is already on.
# try_enable_auto_merge: enables auto-merge (squash) on the PR by default whenever
# dev-lead works on it. We do NOT gate on reviewDecision here: GitHub holds the
# merge until branch protection is satisfied (required reviews approved, review
# threads resolved, required checks green), so enabling early is safe and means a
# PR merges the moment it becomes mergeable — with no further dev-lead event needed.
Comment thread
don-petry marked this conversation as resolved.
Outdated
# Safe to call speculatively: it is idempotent when auto-merge is already on.
# Pass "true" as first arg for strict mode: API errors propagate and a merge failure
# exits non-zero rather than emitting a warning (use for the enable-auto-merge intent).
try_enable_auto_merge() {
local strict="${1:-false}"
if [ "${DEV_LEAD_DRY_RUN:-false}" = "true" ]; then
echo "[dry-run] would enable auto-merge if PR #${PR_NUMBER} is APPROVED"
echo "[dry-run] would enable auto-merge (squash) on PR #${PR_NUMBER}"
return 0
fi
# Refresh HEAD_SHA to the commit that is now the PR head. commit_and_push may
Expand All @@ -499,51 +502,26 @@ try_enable_auto_merge() {
current_head=$(git rev-parse HEAD 2>/dev/null || true)
[ -n "$current_head" ] && HEAD_SHA="$current_head"

local auto_merge_state review_decision

local auto_merge_state
if [ "$strict" = "true" ]; then
auto_merge_state=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}" --jq '.auto_merge // empty')
if [ -n "$auto_merge_state" ]; then
echo "::notice::PR #${PR_NUMBER} auto-merge already enabled"
return 0
fi
review_decision=$(gh api graphql -f query='
query($owner:String!,$repo:String!,$pr:Int!){
repository(owner:$owner,name:$repo){
pullRequest(number:$pr){reviewDecision}
}
}' \
-F owner="${REPO%%/*}" -F repo="${REPO##*/}" -F pr="$PR_NUMBER" \
--jq '.data.repository.pullRequest.reviewDecision')
else
auto_merge_state=$(gh api "repos/${REPO}/pulls/${PR_NUMBER}" \
--jq '.auto_merge // empty' 2>/dev/null || true)
if [ -n "$auto_merge_state" ]; then
echo "::notice::PR #${PR_NUMBER} auto-merge already enabled"
return 0
fi
review_decision=$(gh api graphql -f query='
query($owner:String!,$repo:String!,$pr:Int!){
repository(owner:$owner,name:$repo){
pullRequest(number:$pr){reviewDecision}
}
}' \
-F owner="${REPO%%/*}" -F repo="${REPO##*/}" -F pr="$PR_NUMBER" \
--jq '.data.repository.pullRequest.reviewDecision' 2>/dev/null || true)
fi
if [ -n "$auto_merge_state" ]; then
echo "::notice::PR #${PR_NUMBER} auto-merge already enabled"
return 0
fi

if [ "${review_decision:-}" = "APPROVED" ]; then
echo "::notice::PR #${PR_NUMBER} is APPROVED — enabling auto-merge (squash)"
set -- --auto --squash
[ -n "${HEAD_SHA:-}" ] && set -- "$@" --match-head-commit "$HEAD_SHA"
if [ "$strict" = "true" ]; then
gh pr merge "$PR_NUMBER" --repo "$REPO" "$@"
else
gh pr merge "$PR_NUMBER" --repo "$REPO" "$@" 2>/dev/null || \
echo "::warning::auto-merge could not be enabled on PR #${PR_NUMBER} — check repository settings and token permissions"
fi
echo "::notice::PR #${PR_NUMBER} — enabling auto-merge (squash); GitHub will merge once branch protection is satisfied"
set -- --auto --squash
[ -n "${HEAD_SHA:-}" ] && set -- "$@" --match-head-commit "$HEAD_SHA"
if [ "$strict" = "true" ]; then
gh pr merge "$PR_NUMBER" --repo "$REPO" "$@"
else
echo "::notice::PR #${PR_NUMBER} reviewDecision=${review_decision:-unknown} — not yet eligible for auto-merge"
gh pr merge "$PR_NUMBER" --repo "$REPO" "$@" 2>/dev/null || \
Comment thread
don-petry marked this conversation as resolved.
Outdated
echo "::warning::auto-merge could not be enabled on PR #${PR_NUMBER} — check repository settings and token permissions"
fi
Comment thread
don-petry marked this conversation as resolved.
Outdated
Comment thread
don-petry marked this conversation as resolved.
Outdated
}

Expand Down Expand Up @@ -905,6 +883,10 @@ case "$INTENT_TYPE" in
else
post_reviews_terminal "on-mention" "no-changes" "Engine ran but made no changes."
fi
# Enable auto-merge by default when the mention targets a PR (issue mentions
# carry no PR_NUMBER and are skipped). GitHub holds the merge until branch
# protection is satisfied.
[ -n "${PR_NUMBER:-}" ] && try_enable_auto_merge
Comment thread
don-petry marked this conversation as resolved.
Outdated
fi
exit "$rc"
;;
Expand Down
26 changes: 26 additions & 0 deletions tests/dev-lead/unit/test_fix_reviews.bats
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,32 @@ STUB
[[ "$output" == *"would enable auto-merge"* ]]
}

@test "fix-reviews: try_enable_auto_merge dry-run output present for on-mention" {
export INTENT_TYPE="on-mention"
export DEV_LEAD_DRY_RUN="true"
export USER_INSTRUCTION="Please fix the tests"
export PR_DESCRIPTION="A test pull request"

run bash "$FIX_REVIEWS_SCRIPT"

[ "$status" -eq 0 ]
[[ "$output" == *"would enable auto-merge"* ]]
}

@test "fix-reviews: auto-merge enabled by default (not gated on APPROVED reviewDecision)" {
# Regression guard: dev-lead enables auto-merge whenever it works on a PR;
# GitHub holds the merge until branch protection is satisfied. The dry-run
# message must not reintroduce an "if APPROVED" eligibility gate.
export INTENT_TYPE="fix-reviews"
export DEV_LEAD_DRY_RUN="true"

run bash "$FIX_REVIEWS_SCRIPT"

[ "$status" -eq 0 ]
[[ "$output" == *"would enable auto-merge"* ]]
[[ "$output" != *"if PR #"*"is APPROVED"* ]]
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.

@test "fix-reviews: terminal marker written after successful fix-reviews run" {
export INTENT_TYPE="fix-reviews"
export DEV_LEAD_DRY_RUN="true"
Expand Down
Loading