Ensure merge request is fully prepared before checking its mergeability
Background
In #214742 (closed), it was found out that we are checking mergeability even if the pipeline wasn't created yet.
The pipeline is being created asynchronously so it's possible that the mergeability check happens before it and marks the merge request as mergeable which is not accurate.
Proposal
- Add a new
preparing
state toMergeRequest#merge_status
state machine. - Set
merge_status
topreparing
on merge request creation (inMergeRequests#CreateService
). - Set
merge_status
tounchecked
when fully prepared (inMergeRequests::AfterCreateService
). This means that all preparations are done. - Ensure that
MergeRequest#mergeable?
will always return false whenmerge_status
is stillpreparing
.
NOTE: We need to ensure that we transition states correctly to avoid having stale states that could lead to bugs.