Cannot merge into non-default branch when using branch name push rules
Summary
If a project has a branch name push rule (feature|fix\/.*
) on a project and then attempts to complete a merge request between two branches (none of which are the default branch), then the merge request fails and shows this error Merge failed: Something went wrong during merge pre-receive hook. Branch name does not follow the pattern 'feature|fix\/.*'. Please try again.
Tested on GitLab 12.9.2-ee
Steps to reproduce
This was reported in ZD (internal use only). There is a video in this ticket detailing the instructions.
The instructions are:
- Create a new project and commit a new file to the
master
branch. - Create a
dev
branch. - Add a branch name push rule of
feature|fix\/.*
to the project. - Add a new branch
fix/random
. Notice that it matches the branch name push rule we created. - Create a merge request on the
fix/random
branch (the MR doesn't contain any changes yet and will be WIP).- Set the target branch to the
dev
branch. - Uncheck Delete source branch when merge request is accepted.
- Set the target branch to the
- Add a file to the
fix/random
so we have something to merge. - Resolve the WIP status of the MR
- Attempt to merge the MR and observe the
Merge failed: Something went wrong during merge pre-receive hook. Branch name does not follow the pattern 'feature|fix\/.*'. Please try again.
error
To workaround the problem, you then need to:
- Change the default branch on the project to the target branch we want to merge into (in this case, change the default branch to
dev
) - Try merging the MR again, this time it will be successful
To summarize, the following scenario in the project causes the bug:
- Project default branch is
master
- Project has a branch name push rule of
feature|fix\/.*
- MR has a source branch of
fix/random
and a target branch ofdev
- MR fails if the user attempts to merge
Example Project
What is the current bug behavior?
If the user tries to complete the MR request in this scenario, the MR fails.
What is the expected correct behavior?
The MR should be successful, as the regex rule for the branch name matches. It should not matter what the default branch name is.
Relevant logs and/or screenshots
(Paste any relevant logs - please use code blocks (```) to format console output, logs, and code as it's tough to read otherwise.)
Output of checks
(If you are reporting a bug on GitLab.com, write: This bug happens on GitLab.com)
Results of GitLab environment info
Expand for output related to GitLab environment info
(For installations with omnibus-gitlab package run and paste the output of: `sudo gitlab-rake gitlab:env:info`) (For installations from source run and paste the output of: `sudo -u git -H bundle exec rake gitlab:env:info RAILS_ENV=production`)
Results of GitLab application Check
Expand for output related to the GitLab application check
(For installations with omnibus-gitlab package run and paste the output of:
sudo gitlab-rake gitlab:check SANITIZE=true
)(For installations from source run and paste the output of:
sudo -u git -H bundle exec rake gitlab:check RAILS_ENV=production SANITIZE=true
)(we will only investigate if the tests are passing)
Possible fixes
A quick investigation shows that in this function https://gitlab.com/gitlab-org/gitlab/-/blob/v12.8.1-ee/ee/lib/ee/gitlab/checks/push_rules/branch_check.rb#L34, this is where the code is failing.
It also shows why we were able to workaround the issue by changing the default branch, as shown on line 37.