@@ -37,73 +37,82 @@ jobs:
3737 run : |
3838 authorized_user="carlospolop"
3939 max_merges=2
40-
40+
4141 echo "Authorized user: $authorized_user"
4242 echo "Looking for PRs with exact comment 'merge' from $authorized_user..."
43-
43+
4444 # Get all open PRs
4545 prs=$(gh pr list --state open --json number,title,url --repo "$GITHUB_REPOSITORY")
46-
46+
4747 if [ "$prs" = "[]" ]; then
4848 echo "No open PRs found."
4949 exit 0
5050 fi
51-
51+
5252 # Create a temp file to track merge count
5353 echo "0" > /tmp/merged_count
54-
54+
5555 # Process each PR
5656 echo "$prs" | jq -r '.[] | @base64' | while IFS= read -r pr_data; do
5757 current_count=$(cat /tmp/merged_count)
5858 if [ "$current_count" -ge "$max_merges" ]; then
5959 echo "Reached maximum merge limit ($max_merges). Stopping."
6060 break
6161 fi
62-
62+
6363 pr_info=$(echo "$pr_data" | base64 --decode)
6464 pr_number=$(echo "$pr_info" | jq -r '.number')
6565 pr_title=$(echo "$pr_info" | jq -r '.title')
6666 pr_url=$(echo "$pr_info" | jq -r '.url')
67-
67+
6868 echo "Checking PR #$pr_number: $pr_title"
69-
69+
7070 # Get all comments for this PR
7171 comments=$(gh pr view "$pr_number" --json comments --jq '.comments[]' --repo "$GITHUB_REPOSITORY")
72-
72+
7373 # Print all comment authors for debugging
7474 echo "Comments in PR #$pr_number:"
7575 echo "$comments" | jq -r '" - Author: " + .author.login + " | Comment: " + (.body | split("\n")[0] | .[0:100])'
76-
76+
7777 # Check if any comment from carlospolop contains exactly "merge"
7878 has_merge_comment=false
7979 echo "$comments" | jq -r '.author.login + "|" + .body' | while IFS='|' read -r comment_author comment_body; do
80- # Check if the comment author is exactly "carlospolop"
8180 if [ "$comment_author" = "$authorized_user" ]; then
82- # Check for exact match "merge" (case-insensitive)
8381 if echo "$comment_body" | grep -iExq "merge"; then
8482 echo "Found exact 'merge' comment from $authorized_user in PR #$pr_number"
8583 echo "true" > /tmp/has_merge_comment_$pr_number
8684 break
8785 fi
8886 fi
8987 done
90-
91- # Check if merge comment was found
88+
9289 if [ -f "/tmp/has_merge_comment_$pr_number" ]; then
9390 has_merge_comment=true
9491 fi
95-
92+
9693 if [ "$has_merge_comment" = true ]; then
9794 echo "Attempting to merge PR #$pr_number..."
98-
99- # Check if PR can be merged
100- pr_mergeable=$(gh pr view "$pr_number" --json mergeable --jq '.mergeable' --repo "$GITHUB_REPOSITORY")
101-
95+
96+ # --- Polling for non-UNKNOWN mergeable status ---
97+ max_retries=10
98+ retry=0
99+ while true; do
100+ pr_mergeable=$(gh pr view "$pr_number" --json mergeable --jq '.mergeable' --repo "$GITHUB_REPOSITORY")
101+ if [ "$pr_mergeable" != "UNKNOWN" ]; then
102+ break
103+ fi
104+ if [ $retry -ge $max_retries ]; then
105+ echo "Timeout: mergeable status is still UNKNOWN after $max_retries retries"
106+ break
107+ fi
108+ echo "mergeable status UNKNOWN, retrying in 2s..."
109+ sleep 2
110+ retry=$((retry + 1))
111+ done
112+
102113 if [ "$pr_mergeable" = "MERGEABLE" ]; then
103- # Merge the PR (specify repo explicitly since we're not in a git directory)
104114 if gh pr merge "$pr_number" --merge --delete-branch --repo "$GITHUB_REPOSITORY"; then
105115 echo "Successfully merged PR #$pr_number: $pr_title"
106- # Update merge count in temp file
107116 current_count=$(cat /tmp/merged_count)
108117 echo $((current_count + 1)) > /tmp/merged_count
109118 else
@@ -115,16 +124,13 @@ jobs:
115124 else
116125 echo "No exact 'merge' comment found from $authorized_user in PR #$pr_number"
117126 fi
118-
119- # Clean up temp file
127+
120128 rm -f "/tmp/has_merge_comment_$pr_number"
121129 done
122-
123- # Read final merge count from temp file
130+
124131 final_count=$(cat /tmp/merged_count)
125132 echo "Auto-merge process completed. Merged $final_count PRs."
126-
127- # Clean up merge count file
128133 rm -f /tmp/merged_count
134+
129135 env :
130136 GH_TOKEN : ${{ secrets.PAT_TOKEN }}
0 commit comments