Fix create merge request button for forked projects

What does this MR do and why?

On a forked project, when there is an open merge request from the fork branch targeting the upstream project, the commits page incorrectly shows "Create merge request" instead of "View open merge request". Clicking "Create merge request" in that state errors. This MR fixes the bug.

Root cause. project.mergeRequests in GraphQL is scoped to the target project: MergeRequest.of_projects filters on target_project_id. When the frontend queries the fork for open merge requests, fork-to-upstream MRs are invisible because they live on the upstream project, not the fork.

The two GraphQL fields this MR consumes were added in now-merged backend MRs: sourcedMergeRequests in !255737 (merged) and defaultMergeRequestTarget in !260053 (merged).

Sourced detection applies to forks only. Non-forked projects deliberately keep the existing lookup: a branch there can legitimately have a second merge request toward a different target branch, so sourced detection would incorrectly hide the create button.

References

Screenshots or screen recordings

Before After
before-open-mr Button shows "Create merge request" despite an open fork→upstream MR after-open-mr Button shows "View open merge request" linking to the upstream MR
before-create-mr-error Clicking the button errors: "These branches already have an open merge request" after-closed-mr After closing the MR, "Create merge request" returns with merge_request[target_project_id] in the URL
Video: clicking "View open merge request" and landing on the upstream MR

Edge cases from the first review round

Both are cases where mr_can_target_upstream? is false, so defaultMergeRequestTarget resolves to the fork itself: the button gates on the fork's own permissions and the create URL carries no merge_request[target_project_id].

Upstream disables merge requests after the fork Private fork of a public project
edge-a-commits "Create merge request" still shows (previously it disappeared, gated on the upstream's permission) edge-b-commits "Create merge request" targets the fork (previously it pointed at the upstream and errored)
edge-a-new-mr Clicking it opens a fork-to-fork merge request edge-b-new-mr Clicking it opens a fork-to-fork merge request

How to set up and validate locally

  1. In GDK, fork a project under a second user account.
  2. Push a branch to the fork.
  3. Open a merge request from that fork branch targeting the upstream project's default branch.
  4. Visit the fork's commits page for that branch. The button should read "View open merge request" and link to the MR. Any open merge request sourced from the branch counts now, not only ones targeting the upstream default branch.
  5. Close or delete the MR and reload the commits page. "Create merge request" should appear only if the user has createMergeRequestIn permission on the upstream project, and the link should carry merge_request[target_project_id] pointing at the upstream project ID.
  6. Optionally, enable the fork's "merge requests target this project" setting (Settings > Merge requests > Default target). Verify that the "Create merge request" button reappears based on the fork's own permissions and that the link no longer carries merge_request[target_project_id].

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Chaoyue Zhao

Merge request reports

Loading
Loading