Merge when pipeline succeeds accidentally merge before it finishes on commit status creation
Summary
This bug is related to #14064 (closed). Setting merge request to be automatically merged will unintentionally merge if there is a non-branch pipeline currently running, and we post a new commit status to the commit. This means the commit might be merged when it is not safe to merge yet. The underlying issue is because posting commit status will create a new pipeline if there is no branch pipeline currently running. When the new pipeline is created, GitLab will think that the Merge Request pipeline has succeeded and it triggers the automatic merge.
Steps to reproduce
- Create a new project, initialise it with
README.md
- Create a new branch, add
.gitlab-ci.yml
with the following code (change the time so it's enough for you to execute the API), commit, and push:
stages:
- test
test:
stage: test
script:
- sleep 600
only:
- merge_requests
- Create a new Merge Request from that branch against
master
. - While pipeline is running, click merge when pipeline succeeds
- Hit the API with the required parameters only:
curl -X POST -vL -H 'Private-Token: <token>' "gitlab.com/api/v4/projects/<project id>/statuses/<commit sha>?state=success"
- Merge Request will be merged when the original pipeline still running.
Example Project
(If possible, please create an example project here on GitLab.com that exhibits the problematic behavior, and link to it here in the bug report)
aufarg/pipeline-test!3 (merged). It can be seen that the original pipeline (marked as detached as it is a merge request pipeline) is created before the merge and finished after the merge.
What is the current bug behavior?
Merge when pipeline succeed merge the merge request before the pipeline is finished.
What is the expected correct behavior?
Merge when pipeline succeed merge the merge request after the pipeline is finished.
Relevant logs and/or screenshots
N/A
Output of checks
This bug happens on GitLab.com
Results of GitLab environment info
N/A
Results of GitLab application Check
N/A
Possible fixes
MR gitlab-foss!32792 (closed) will fix it. Alternatively, we can tied the automatic merge to the currently running pipeline.