Fix auto-merge workflow to automatically merge auto-approved dependabot PRs #4626
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Problem
The existing auto-merge workflow was not reliably merging dependabot PRs after they were auto-approved. The workflow had a race condition where it would check for approvals before they were fully recorded in GitHub's system, causing the auto-merge to be skipped.
Solution
Enhanced the
auto-merge.yml
workflow with several key improvements:1. Added
workflow_run
TriggerThe workflow now triggers after the "Approve dependabot" workflow completes, ensuring it runs at the right time:
2. Implemented Retry Logic
Added intelligent retry logic to wait for approvals to be recorded:
3. Fixed PR Number Extraction
Added logic to extract PR numbers correctly for different event types:
pull_request_target
andpull_request_review
events: usesgithub.event.pull_request.number
workflow_run
events: queries the GitHub API to find the PR by commit SHA4. Enhanced Conditional Logic
Updated the job condition to handle both direct dependabot events and workflow_run events:
How It Works
auto-approve-dependabot.yml
approves the PRauto-merge.yml
triggers viaworkflow_run
eventDocumentation
Added comprehensive documentation:
README.md
in.github/workflows/
directory documenting the entire auto-merge systemTesting
The workflow will be tested automatically when dependabot creates new PRs. It can also be verified by:
@dependabot rebase
on an existing dependabot PRCloses #[issue_number]
Original prompt
💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.