Skip to content

Commit f5c860f

Browse files
committed
auto merge7
1 parent d58e346 commit f5c860f

File tree

1 file changed

+87
-6
lines changed

1 file changed

+87
-6
lines changed

.github/workflows/auto-merge-queue.yml

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,9 @@ jobs:
285285
286286
echo "🚀 Adding PR #$PR_NUMBER to merge queue with PAT..."
287287
288-
# Get PR ID using GraphQL
289-
PR_ID=$(gh api graphql \
288+
# Check PR mergeability status first
289+
echo "Checking PR mergeability..."
290+
MERGEABLE_INFO=$(gh api graphql \
290291
-f owner="${{ github.repository_owner }}" \
291292
-f repo="${{ github.event.repository.name }}" \
292293
-F number="$PR_NUMBER" \
@@ -297,13 +298,68 @@ jobs:
297298
id
298299
title
299300
baseRefName
301+
mergeable
302+
mergeableState
303+
mergeStateStatus
300304
}
301305
}
302-
}' --jq '.data.repository.pullRequest.id')
306+
}')
307+
308+
PR_ID=$(echo "$MERGEABLE_INFO" | jq -r '.data.repository.pullRequest.id')
309+
MERGEABLE=$(echo "$MERGEABLE_INFO" | jq -r '.data.repository.pullRequest.mergeable')
310+
MERGEABLE_STATE=$(echo "$MERGEABLE_INFO" | jq -r '.data.repository.pullRequest.mergeableState')
311+
MERGE_STATE_STATUS=$(echo "$MERGEABLE_INFO" | jq -r '.data.repository.pullRequest.mergeStateStatus')
312+
313+
echo "PR ID: $PR_ID"
314+
echo "Mergeable: $MERGEABLE"
315+
echo "Mergeable State: $MERGEABLE_STATE"
316+
echo "Merge State Status: $MERGE_STATE_STATUS"
317+
318+
# Wait for mergeability check if still unknown
319+
if [[ "$MERGEABLE" == "null" ]] || [[ "$MERGEABLE_STATE" == "UNKNOWN" ]]; then
320+
echo "⏳ Mergeability check still in progress, waiting..."
321+
sleep 10
322+
323+
# Re-check mergeability
324+
MERGEABLE_INFO=$(gh api graphql \
325+
-f owner="${{ github.repository_owner }}" \
326+
-f repo="${{ github.event.repository.name }}" \
327+
-F number="$PR_NUMBER" \
328+
-f query='
329+
query($owner: String!, $repo: String!, $number: Int!) {
330+
repository(owner: $owner, name: $repo) {
331+
pullRequest(number: $number) {
332+
id
333+
mergeable
334+
mergeableState
335+
mergeStateStatus
336+
}
337+
}
338+
}')
339+
340+
MERGEABLE=$(echo "$MERGEABLE_INFO" | jq -r '.data.repository.pullRequest.mergeable')
341+
MERGEABLE_STATE=$(echo "$MERGEABLE_INFO" | jq -r '.data.repository.pullRequest.mergeableState')
342+
MERGE_STATE_STATUS=$(echo "$MERGEABLE_INFO" | jq -r '.data.repository.pullRequest.mergeStateStatus')
343+
344+
echo "After wait - Mergeable: $MERGEABLE, State: $MERGEABLE_STATE, Status: $MERGE_STATE_STATUS"
345+
fi
346+
347+
# Check if PR is ready for merge queue
348+
if [[ "$MERGEABLE" == "false" ]] || [[ "$MERGEABLE_STATE" == "CONFLICTING" ]]; then
349+
echo "❌ PR has merge conflicts and cannot be added to merge queue"
350+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
351+
-f body="❌ Cannot add to merge queue: PR has merge conflicts. Please resolve conflicts first."
352+
exit 1
353+
fi
354+
355+
if [[ "$MERGE_STATE_STATUS" != "CLEAN" ]] && [[ "$MERGE_STATE_STATUS" != "UNSTABLE" ]]; then
356+
echo "⚠️ PR merge state is $MERGE_STATE_STATUS, proceeding with caution..."
357+
fi
303358
304359
echo "📋 PR ID: $PR_ID"
305360
306-
# Enqueue PR using GraphQL mutation
361+
# Enqueue PR using GraphQL mutation with error handling
362+
echo "🔄 Attempting to enqueue PR..."
307363
RESULT=$(gh api graphql \
308364
-f pr_id="$PR_ID" \
309365
-f query='
@@ -317,16 +373,41 @@ jobs:
317373
position
318374
}
319375
}
320-
}')
376+
}' 2>&1) || ENQUEUE_FAILED=true
321377
322378
echo "GraphQL Result: $RESULT"
323379
380+
# Check for errors in the response
381+
if [[ "$ENQUEUE_FAILED" == "true" ]] || echo "$RESULT" | grep -q "errors"; then
382+
echo "❌ Failed to add PR to merge queue"
383+
384+
ERROR_MSG=$(echo "$RESULT" | jq -r '.errors[]?.message // "Unknown error"' 2>/dev/null || echo "$RESULT")
385+
echo "Error details: $ERROR_MSG"
386+
387+
# Post helpful error comment
388+
if echo "$ERROR_MSG" | grep -q -i "mergeability"; then
389+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
390+
-f body="❌ Cannot add to merge queue: Mergeability check has not completed. This usually resolves automatically - please try again in a few minutes or remove and re-add the auto-merge label."
391+
elif echo "$ERROR_MSG" | grep -q -i "force-push\|branch protection"; then
392+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
393+
-f body="❌ Cannot add to merge queue: Branch protection rules prevent merge queue operations. Please check that merge queue is enabled for the **master-gmq** branch and has appropriate permissions."
394+
else
395+
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
396+
-f body="❌ Failed to add to merge queue: $ERROR_MSG. Please check repository settings and try again."
397+
fi
398+
399+
exit 1
400+
fi
401+
324402
QUEUE_POSITION=$(echo "$RESULT" | jq -r '.data.enqueuePullRequest.mergeQueueEntry.position // "unknown"')
325403
echo "✅ Successfully added to merge queue at position: $QUEUE_POSITION"
326404
327405
# Comment on PR with success
328406
gh api repos/${{ github.repository }}/issues/$PR_NUMBER/comments \
329-
-f body="🤖 Automatically added to merge queue at position **$QUEUE_POSITION** after CI success ✅ (Event: ${{ github.event_name }}, Trigger: Auto-merge label detected with successful CI)"
407+
-f body="🤖 Successfully added to merge queue at position **$QUEUE_POSITION** after CI success ✅
408+
409+
Event: ${{ github.event_name }}
410+
Trigger: Auto-merge label detected with successful CI"
330411

331412
env:
332413
GITHUB_TOKEN: ${{ secrets.MERGE_QUEUE_PAT }}

0 commit comments

Comments
 (0)