11name : Auto Merge Approved PRs
22
33on :
4+ issue_comment :
5+ types : [created, edited]
6+ workflow_run :
7+ workflows : ["*", "!Auto Merge Approved PRs"]
8+ types : [completed]
9+ pull_request_target :
10+ types : [opened, edited, synchronize, reopened, ready_for_review]
11+ workflow_dispatch :
12+ # Recover the approval queue even if a completion trigger is missed.
413 schedule :
5- - cron : ' 17 * * * *' # Every hour at minute 17 UTC
6- workflow_dispatch : # Allow manual triggering
14+ - cron : " 7,22,37,52 * * * *"
15+
16+ concurrency :
17+ group : auto-merge-approved-prs
18+ cancel-in-progress : false
719
820permissions :
921 contents : write
@@ -12,226 +24,22 @@ permissions:
1224
1325jobs :
1426 auto-merge-prs :
27+ if : >-
28+ github.event_name != 'issue_comment' ||
29+ (github.event.issue.pull_request &&
30+ github.event.comment.user.login == 'carlospolop')
1531 runs-on : ubuntu-latest
16-
32+ timeout-minutes : 15
1733 steps :
18- - name : Checkout repository
34+ # These triggers have write permissions: use only default-branch code.
35+ - name : Checkout trusted merger code
1936 uses : actions/checkout@v4
2037 with :
21- fetch-depth : 1 # Only need latest commit for PR operations
22- token : ${{ secrets.PAT_TOKEN }}
23-
24- - name : Configure git
25- run : |
26- git config --global user.email "action@github.com"
27- git config --global user.name "GitHub Action"
28-
29- - name : Verify GitHub CLI
30- run : gh --version
31-
32- - name : Check for running workflows
33- id : check_workflows
34- run : |
35- gh_with_retry() {
36- local max_attempts=5
37- local base_sleep_seconds=2
38- local attempt=1
39- local stderr_file
40- stderr_file=$(mktemp)
41-
42- while true; do
43- if gh "$@" 2>"$stderr_file"; then
44- rm -f "$stderr_file"
45- return 0
46- fi
47-
48- local exit_code=$?
49- if [ "$attempt" -ge "$max_attempts" ]; then
50- echo "gh command failed after $max_attempts attempts: gh $*" >&2
51- cat "$stderr_file" >&2
52- rm -f "$stderr_file"
53- return "$exit_code"
54- fi
55-
56- local sleep_for=$((base_sleep_seconds * attempt))
57- echo "gh command failed (attempt $attempt/$max_attempts): gh $*" >&2
58- cat "$stderr_file" >&2
59- echo "Retrying in ${sleep_for}s..." >&2
60- sleep "$sleep_for"
61- attempt=$((attempt + 1))
62- done
63- }
64-
65- # Get all running workflows except this one
66- running_workflows=$(gh_with_retry run list --status in_progress --json workflowName,name --repo "$GITHUB_REPOSITORY" --jq '.[].name' | grep -v "Auto Merge Approved PRs" | wc -l)
67- echo "running_workflows=$running_workflows" >> $GITHUB_OUTPUT
68-
69- if [ "$running_workflows" -gt 0 ]; then
70- echo "Found $running_workflows running workflows. Exiting to avoid conflicts."
71- echo "should_continue=false" >> $GITHUB_OUTPUT
72- else
73- echo "No other workflows running. Proceeding with auto-merge."
74- echo "should_continue=true" >> $GITHUB_OUTPUT
75- fi
76- env :
77- GH_TOKEN : ${{ secrets.PAT_TOKEN }}
78-
79- - name : Find and merge approved PRs
80- if : steps.check_workflows.outputs.should_continue == 'true'
81- run : |
82- gh_with_retry() {
83- local max_attempts=5
84- local base_sleep_seconds=2
85- local attempt=1
86- local stderr_file
87- stderr_file=$(mktemp)
88-
89- while true; do
90- if gh "$@" 2>"$stderr_file"; then
91- rm -f "$stderr_file"
92- return 0
93- fi
94-
95- local exit_code=$?
96- if [ "$attempt" -ge "$max_attempts" ]; then
97- echo "gh command failed after $max_attempts attempts: gh $*" >&2
98- cat "$stderr_file" >&2
99- rm -f "$stderr_file"
100- return "$exit_code"
101- fi
102-
103- local sleep_for=$((base_sleep_seconds * attempt))
104- echo "gh command failed (attempt $attempt/$max_attempts): gh $*" >&2
105- cat "$stderr_file" >&2
106- echo "Retrying in ${sleep_for}s..." >&2
107- sleep "$sleep_for"
108- attempt=$((attempt + 1))
109- done
110- }
111-
112- authorized_user="carlospolop"
113- max_merges=2
114-
115- echo "Authorized user: $authorized_user"
116- echo "Looking for PRs with exact comment 'merge' from $authorized_user..."
117-
118- # Get all open PRs, paginating through the full result set instead of
119- # relying on `gh pr list`'s default page size.
120- prs=$(gh_with_retry api --paginate "repos/$GITHUB_REPOSITORY/pulls?state=open&per_page=100" | jq -s 'add | map({number, title, url: .html_url, author: {login: .user.login}})')
121-
122- if [ "$prs" = "[]" ]; then
123- echo "No open PRs found."
124- exit 0
125- fi
126-
127- # Create a temp file to track merge count
128- echo "0" > /tmp/merged_count
129-
130- # Process each PR
131- echo "$prs" | jq -r '.[] | @base64' | while IFS= read -r pr_data; do
132- current_count=$(cat /tmp/merged_count)
133- if [ "$current_count" -ge "$max_merges" ]; then
134- echo "Reached maximum merge limit ($max_merges). Stopping."
135- break
136- fi
137-
138- pr_info=$(echo "$pr_data" | base64 --decode)
139- pr_number=$(echo "$pr_info" | jq -r '.number')
140- pr_title=$(echo "$pr_info" | jq -r '.title')
141- pr_url=$(echo "$pr_info" | jq -r '.url')
142- pr_author=$(echo "$pr_info" | jq -r '.author.login')
143-
144- echo "Checking PR #$pr_number: $pr_title (author: $pr_author)"
145-
146- # Special case: allow merge without "merge" comment for Research Update Enhanced PRs by the authorized user
147- eligible_by_title=false
148- if [[ "$pr_title" == "Research Update Enhanced"* && "$pr_author" == "$authorized_user" ]]; then
149- echo "PR #$pr_number qualifies for direct merge based on title and author."
150- eligible_by_title=true
151- fi
152-
153- # Check if any comment from carlospolop contains exactly "merge"
154- has_merge_comment=false
155- if [ "$eligible_by_title" != true ]; then
156- # Get all comments for this PR
157- if ! comments=$(gh_with_retry pr view "$pr_number" --json comments --jq '.comments[]' --repo "$GITHUB_REPOSITORY"); then
158- echo "Failed to fetch comments for PR #$pr_number after retries. Skipping PR."
159- continue
160- fi
161-
162- # Print all comment authors for debugging
163- echo "Comments in PR #$pr_number:"
164- echo "$comments" | jq -r '" - Author: " + .author.login + " | Comment: " + (.body | split("\n")[0] | .[0:100])'
165-
166- echo "$comments" | jq -r '.author.login + "|" + .body' | while IFS='|' read -r comment_author comment_body; do
167- if [ "$comment_author" = "$authorized_user" ]; then
168- if echo "$comment_body" | grep -iExq "merge"; then
169- echo "Found exact 'merge' comment from $authorized_user in PR #$pr_number"
170- echo "true" > /tmp/has_merge_comment_$pr_number
171- break
172- fi
173- fi
174- done
175- fi
176-
177- if [ -f "/tmp/has_merge_comment_$pr_number" ]; then
178- has_merge_comment=true
179- fi
180-
181- if [ "$has_merge_comment" = true ] || [ "$eligible_by_title" = true ]; then
182- echo "Attempting to merge PR #$pr_number..."
183-
184- # Get PR details including head branch
185- if ! pr_details=$(gh_with_retry pr view "$pr_number" --json headRefName,baseRefName --repo "$GITHUB_REPOSITORY"); then
186- echo "Failed to fetch details for PR #$pr_number after retries. Skipping PR."
187- continue
188- fi
189- head_branch=$(echo "$pr_details" | jq -r '.headRefName')
190- base_branch=$(echo "$pr_details" | jq -r '.baseRefName')
191-
192- # --- Polling for non-UNKNOWN mergeable status ---
193- max_retries=10
194- retry=0
195- while true; do
196- if ! pr_mergeable=$(gh_with_retry pr view "$pr_number" --json mergeable --jq '.mergeable' --repo "$GITHUB_REPOSITORY"); then
197- echo "Failed to fetch mergeable status for PR #$pr_number after retries."
198- pr_mergeable="UNKNOWN"
199- fi
200- if [ "$pr_mergeable" != "UNKNOWN" ]; then
201- break
202- fi
203- if [ $retry -ge $max_retries ]; then
204- echo "Timeout: mergeable status is still UNKNOWN after $max_retries retries"
205- break
206- fi
207- echo "mergeable status UNKNOWN, retrying in 2s..."
208- sleep 2
209- retry=$((retry + 1))
210- done
211-
212- if [ "$pr_mergeable" = "MERGEABLE" ]; then
213- if gh pr merge "$pr_number" --merge --delete-branch --repo "$GITHUB_REPOSITORY"; then
214- echo "Successfully merged PR #$pr_number: $pr_title"
215- current_count=$(cat /tmp/merged_count)
216- echo $((current_count + 1)) > /tmp/merged_count
217- else
218- echo "Failed to merge PR #$pr_number: $pr_title"
219- fi
220- elif [ "$pr_mergeable" = "CONFLICTED" ] || [ "$pr_mergeable" = "CONFLICTING" ]; then
221- echo "PR #$pr_number has conflicts. Skipping auto-merge so it can be resolved manually."
222- else
223- echo "PR #$pr_number is not mergeable (status: $pr_mergeable)"
224- fi
225- else
226- echo "No exact 'merge' comment found from $authorized_user in PR #$pr_number"
227- fi
228-
229- rm -f "/tmp/has_merge_comment_$pr_number"
230- done
231-
232- final_count=$(cat /tmp/merged_count)
233- echo "Auto-merge process completed. Merged $final_count PRs."
234- rm -f /tmp/merged_count
38+ ref : ${{ github.event.repository.default_branch }}
39+ persist-credentials : false
40+ fetch-depth : 1
23541
42+ - name : Merge approved PRs when other workflows are idle
43+ run : python3 scripts/merge_approved_prs.py
23644 env :
23745 GH_TOKEN : ${{ secrets.PAT_TOKEN }}
0 commit comments