Add "Delete source branch" button to closed merge requests

What does this MR do and why?

This MR adds a "Delete Source Branch" button to the closed MR widget.

  • Extracts the post-merge delete source branch logic into a reusable mixin sourceBranchRemovalMixin, and uses it in both MRWidgetMerged and MRWidgetClosed.
  • Fixes a bug in the merged MR widget where the "Delete source branch" button was not removed after a successful deletion. The button now disappears immediately by setting mr.sourceBranchRemoved = true directly from the API response, rather than relying on MRWidgetUpdateRequested which triggered a GraphQL refetch that did not include sourceBranchExists.
  • A new analytics event i_code_review_post_close_delete_branch is introduced to track usage separately from the post-merge equivalent.

References

Screenshots or screen recordings

Before After
image image

How to set up and validate locally

Use the following snippet to create 2 merge requests quickly:

project = Project.find_by_full_path('x')
user = project.first_owner
repo = project.repository
ts = Time.now.to_i

2.times do |i|
  branch_name = "test-delete-source-branch-#{ts}-#{i + 1}"

  repo.create_branch(branch_name, project.default_branch)

  repo.create_file(
    user,
    "test-#{ts}-#{i + 1}.txt",
    "test content #{i + 1}",
    message: "Add test file #{i + 1}",
    branch_name: branch_name,
  )

  mr = MergeRequests::CreateService.new(
    project: project,
    current_user: user,
    params: {
      title: "Test delete source branch #{i + 1}",
      source_branch: branch_name,
      target_branch: project.default_branch,
      force_remove_source_branch: '0',
    },
  ).execute

  puts "Created MR !#{mr.iid}: #{mr.title} (#{mr.source_branch})"
end

Test the change with a closed MR:

  1. Close one of the merge requests
  2. Wait a few moments for the MR widget to update - the "Delete Source Branch" button should appear.
  3. Click the "Delete Source Branch" button - if successful the button will disappear, and you will see an XHR request to DELETE http://gdk.test:3000/<group>/<project>/-/branches/<branch> return a 200 / {"message":"Branch was deleted"}

To ensure that the change did not break existing functionality, test the change with a merged MR:

  1. Merge one of the merge requests.
  2. Wait a few moments for the MR widget to update - the "Delete Source Branch" button should appear.
  3. Click the "Delete Source Branch" button - if successful the button will disappear, and you will see an XHR request to DELETE http://gdk.test:3000/<group>/<project>/-/branches/<branch> return a 200 / {"message":"Branch was deleted"}

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 Anton Smith

Merge request reports

Loading