You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
gh pr merge sometimes fails with "Base branch was modified. Review and try the merge again." #8092
We're automating merging PRs with Actions using gh command, but it sometimes fails with the error message GraphQL: Base branch was modified. Review and try the merge again. (mergePullRequest).
Steps to reproduce the behavior
Although it might be possible to create a reproduction workflow, but let me explain by what it does in overview because tedious.
Checkout the repository and make some changes.
Push and create a new branch with git push origin HEAD:update-20230928175133
Create a PR with gh pr create --base develop --head update-20230928175133 --title 'Update develop' --body "some body"
Merge the PR with gh pr merge update-20230928175133 --merge --admin
This step fails with GraphQL: Base branch was modified. Review and try the merge again. (mergePullRequest)
The version is gh version 2.30.0 (2023-05-30).
This is slightly off topic but our Actions Workflow is aimed for updating k8s manifests, and is triggered by dispatching via API, sometimes multiple requests concurrently.
Expected vs actual behavior
I believe this is a time lag of GitHub internal processing, and it has the same origin as we see in browsers the merge button grayed out in a few seconds when different PR is merged.
I hope gh handles this error gracefully for easiness of use; i.e. retry automatically when this error happens.
Logs
+ gh pr merge update-20230928175133 --merge --admin
GraphQL: Base branch was modified. Review and try the merge again. (mergePullRequest)
Reacted by Matan Agazan, danielsalama3, Stefan Kuksenko, Adam Snyder, James Braza, Eskander Bejaoui, Alfredo Hernández, H.K. and Mykola Morozov
This error coming from the GitHub GraphQL API mergePullRequest mutation is stating there have been other changes merged into your base branch and you are unable to merge in your changes despite being a repository administrator. The --admin flag is largely to bypass the GitHub CLI client checks on the PR; if there are branch protection rules or repository rulesets that prevent administrators from bypassing them, then you will have to satisfy the rules in order to merge.
If one of these rules is in place, then we might need to figure out how to ensure your branch is up to date via the CLI so you can merge it. updatePullRequestBranch comes to mind 🤔
We enable Require status checks before merging but we do not enable Require branches to be up to date before merging. We use --admin to allow it bypass lint checks. We now retry the command to work around this issue.
# Retry to work around "Base branch was modified." error.# Ref: https://github.com/cli/cli/issues/8092foriin {1..3};doif gh pr merge "${branch}" --merge --admin;thenbreakfiif [[ $i-eq 3 ]];thenexit 1
fi
sleep 5
done
Reacted by Benjamin Ørskov, Stewart Thomson, Stefan Kuksenko, Eskander Bejaoui and xinyan-elloyds
@itchyny@matanagazan : Out of curiosity, how does this change if you remove --admin?
You mentioned above you add it in order to ignore your lint status check. Have you tried making that check optional and not leveraging the admin bypass?
@itchyny , thank you sincerely for following up and doing a little more testing! 🙇 ✨
I'm raising this up internally with our PR folks to understand the edge case in merging PRs. I'm generally hesitant building in retry logic and increasing API pressure if possible, hence I'm trying to solve this at the source. 💪 Thank you for your patience!
See this discussion – this is happens when the mergeability computation hasn't completed yet; there is some information about this process in the Get PR REST documentation. I believe this would happen if the merge request is the first API request after the base branch has changed; at that time, mergeability is not computed yet, so the request fails. Because this triggers the computation, a retry a few seconds later will be successful (unless there's yet another commit to the base branch in such short order).
@matthiasr has the right of it: the base branch has changed between generating the merge commit and GitHub trying to merge that merge commit.
From internal discussions, there are no sure fire ideas on how to improve the current experience either server or client side because of what is going on under the hood. There is concern that building in wholesale retry logic would create issues. So the advice, would be putting in a small weight on your end to have it succeed without error more frequently. 🙇
Thank you for sharing your discussion conclusions. I understand the difficulty of fixing this kind of behavior (I imagine the complexity of internal asynchronous jobs of mergeability computation). But what solution you would recommend to solve this kind of concurrent processing situation? Do you mean by putting a small wait, sleeping for random duration before invoking the command? This looks less reliable way of solving the issue than retrying like #8092 (comment). I thought handling the errors within the command enables us to implement more sophisticated retrying logic (not sure what it means in graphql but in general practice retry only on 5xx errors or network connection errors, but not on 4xx errors). If gh doesn't support it, users would keep retrying the whole command regardless of the error kinds.
Do you mean by putting a small wait, sleeping for random duration before invoking the command? This looks less reliable way of solving the issue than retrying like #8092 (comment).
I imagine both would be useful to have: giving the backend GitHub pull request service a moment to handle the merging and retrying in case it we catch it at some point.
I believe the sleep/wait suggestion was for giving the pull request merge process to recompute commits to avoid an error for race conditions.
Just wanted to chime in here and say I think adding in an automatic retry mechanism inside of gh feels appropriate considering we know that there is not any path to improve this on the platform side of things. We have a couple cases of using backoff and retrying in the codebase already to address platform limitations, for example in repo fork. I don't think it would be too difficult to do something similar for pr merge.
Reacted by Stefan Kuksenko, Clayton Yochum, Arthur Kepler and gracevaidya
Activity
andyfeller commentedon Oct 2, 2023
@itchyny : thank you for your patience! 🙇
Are there any branch protection rules and/or repository rulesets in place that are requiring the branch be up to date before merging?
This error coming from the GitHub GraphQL API
mergePullRequestmutation is stating there have been other changes merged into your base branch and you are unable to merge in your changes despite being a repository administrator. The--adminflag is largely to bypass the GitHub CLI client checks on the PR; if there are branch protection rules or repository rulesets that prevent administrators from bypassing them, then you will have to satisfy the rules in order to merge.If one of these rules is in place, then we might need to figure out how to ensure your branch is up to date via the CLI so you can merge it.
updatePullRequestBranchcomes to mind 🤔itchyny commentedon Oct 2, 2023
We enable
Require status checks before mergingbut we do not enableRequire branches to be up to date before merging. We use--adminto allow it bypass lint checks. We now retry the command to work around this issue.matanagazan commentedon Oct 4, 2023
I also experience the same error and I use
--adminflagandyfeller commentedon Oct 10, 2023
@itchyny @matanagazan : Out of curiosity, how does this change if you remove
--admin?You mentioned above you add it in order to ignore your lint status check. Have you tried making that check optional and not leveraging the admin bypass?
itchyny commentedon Oct 24, 2023
I created a reproducible minimal action workflow without
--adminflag. The workflow sometimes succeeds (https://github.com/itchyny/gh-issues-8092/actions/runs/6624911833), but it sometimes fails (https://github.com/itchyny/gh-issues-8092/actions/runs/6624918682). Looking into the job failed, we seeGraphQL: Base branch was modified. Review and try the merge again. (mergePullRequest)error.andyfeller commentedon Oct 27, 2023
@itchyny , thank you sincerely for following up and doing a little more testing! 🙇 ✨
I'm raising this up internally with our PR folks to understand the edge case in merging PRs. I'm generally hesitant building in retry logic and increasing API pressure if possible, hence I'm trying to solve this at the source. 💪 Thank you for your patience!
matthiasr commentedon Nov 1, 2023
See this discussion – this is happens when the mergeability computation hasn't completed yet; there is some information about this process in the Get PR REST documentation. I believe this would happen if the merge request is the first API request after the base branch has changed; at that time, mergeability is not computed yet, so the request fails. Because this triggers the computation, a retry a few seconds later will be successful (unless there's yet another commit to the base branch in such short order).
andyfeller commentedon Nov 16, 2023
@matthiasr has the right of it: the base branch has changed between generating the merge commit and GitHub trying to merge that merge commit.
From internal discussions, there are no sure fire ideas on how to improve the current experience either server or client side because of what is going on under the hood. There is concern that building in wholesale retry logic would create issues. So the advice, would be putting in a small weight on your end to have it succeed without error more frequently. 🙇
itchyny commentedon Nov 17, 2023
Thank you for sharing your discussion conclusions. I understand the difficulty of fixing this kind of behavior (I imagine the complexity of internal asynchronous jobs of mergeability computation). But what solution you would recommend to solve this kind of concurrent processing situation? Do you mean by putting a small wait, sleeping for random duration before invoking the command? This looks less reliable way of solving the issue than retrying like #8092 (comment). I thought handling the errors within the command enables us to implement more sophisticated retrying logic (not sure what it means in graphql but in general practice retry only on 5xx errors or network connection errors, but not on 4xx errors). If gh doesn't support it, users would keep retrying the whole command regardless of the error kinds.
andyfeller commentedon Dec 4, 2023
I imagine both would be useful to have: giving the backend GitHub pull request service a moment to handle the merging and retrying in case it we catch it at some point.
I believe the sleep/wait suggestion was for giving the pull request merge process to recompute commits to avoid an error for race conditions.
samcoe commentedon Dec 4, 2023
Just wanted to chime in here and say I think adding in an automatic retry mechanism inside of
ghfeels appropriate considering we know that there is not any path to improve this on the platform side of things. We have a couple cases of using backoff and retrying in the codebase already to address platform limitations, for example inrepo fork. I don't think it would be too difficult to do something similar forpr merge.Add retry logic
HOTFIX: add --admin to gh merge