Fix wrong source branch when creating MR from issue

What does this MR do and why?

Fixes an intermittent bug where clicking "Create merge request" from an issue's Development widget could redirect to the "New merge request" page with a wrong source branch — a suffixed name like <branch>-2 — and show "Source branch <branch>-2 does not exist", because that suffixed branch was never actually created.

The fix changes createBranch() in app/assets/javascripts/work_items/components/work_item_development/work_item_create_branch_merge_request_modal.vue to return the branch name from the server response of POST /-/branches (the actual, server-sanitized created branch name) on success, and null on failure. createMergeRequest() now uses that returned name to build the New MR URL and returns early if branch creation failed. This guarantees the URL always matches the branch that was actually created, and avoids opening the New MR page for a branch that does not exist.

This approach was suggested in review and is more robust than the earlier version (which captured the branch name into a local before creating the branch) because it relies on the server's authoritative branch name.

Root cause

The modal is always mounted and runs init() on load, fetching a suggested branch name from the can_create_branch endpoint and storing it in this.branchName (the base name, since no branch exists yet). Opening the modal (a showModal watcher) runs init() again, firing a fresh request. The modal's primary button is not disabled while that request is in flight, so the user can submit immediately: createMergeRequest() runs and await this.createBranch() creates the base branch. The second init() request then resolves and, because the branch now exists, returns a suffixed suggestion (-2) which overwrites this.branchName. The New MR URL was then built from that overwritten value.

Testing

The modal Jest suite passes. A regression test in spec/frontend/work_items/components/work_item_development/work_item_create_branch_merge_request_modal_spec.js reproduces the race deterministically: mount with the modal closed so opening it re-runs init(); the later can_create_branch response returns the suffixed name; the branch-creation response returns the base name; then assert the New MR URL uses the base name. It was verified to fail without the fix (URL contained the -2 name) and pass with it. A test was also added asserting the New MR page is not opened when branch creation fails.

Also fixes a recurring intermittent failure on master in the feature spec spec/features/work_items/issues/user_creates_branch_and_merge_request_spec.rb (example: "when branch name is auto-generated creates a merge request").

Edited by Mario Celi

Merge request reports

Loading
Loading