Reset project namespace when a project transfer rolls back

What does this MR do and why?

A failed async project transfer can leave the project namespace's parent_id (and traversal_ids) pointing at the transfer target while projects.namespace_id stays on the old namespace. This is the remaining source of the drift that !251257 cleans up, so the BBM there does not need to be requeued after this lands.

Root cause
  1. Projects::TransferService calls save_project! inside Project.transaction. Project#ensure_project_namespace_in_sync copies the new namespace_id onto the cached project.project_namespace as parent_id, and autosave writes it.
  2. A later step in the transaction raises, so the database changes roll back. The in-memory ProjectNamespace keeps the new parent_id as a dirty attribute.
  3. rollback_side_effects only resets project, not its project namespace.
  4. Projects::TransferWorker holds that same project namespace object and calls cancel_transfer! on failure, both when the service returns false and in its rescue StandardError. The state-machine save persists the stale parent_id, and traversal_ids are recomputed from it.

Since the async worker became the only transfer path, every transfer that fails after save_project! produces one drifted row.

This MR makes two changes to Projects::TransferService:

  1. rollback_side_effects now resets the project namespace, alongside the existing project.reset. Every exception raised inside the transaction passes through it, so both worker failure paths are covered.
  2. rollback_side_effects now runs only for failures inside the transaction. remove_paid_features, update_pending_builds and post_update_hooks run after the transaction has committed. Before, a failure in one of them still ran the rollback: it rewrote the repository configuration to the old path, and the worker cancelled a transfer that had already happened. It then created a "transfer failed" to-do, and Sidekiq retried the job, which failed with "Project is already in this namespace". Now those errors are sent to error tracking, and the transfer is reported as a success. Each step is rescued separately, so a failure in one does not skip the others. Suggested in review: !262788 (comment 4044637633)

References

Screenshots or screen recordings

Not applicable, backend only. The new worker spec fails without the fix:

expected: 1
     got: 3            # project_namespace.parent_id
expected: [1, 2]
     got: [3, 2]       # project_namespace.traversal_ids

How to set up and validate locally

  1. In the Rails console, pick a project you can transfer and a target group, and make the transfer fail after the project is saved:
    project = Project.find(<id>); target = Group.find(<id>); user = User.find(<id>)
    Projects::TransferService.prepend(Module.new { def update_integrations = raise(StandardError, 'boom') })
    Projects::TransferWorker.new.perform(project.id, target.id, user.id) rescue nil
  2. Check that the project namespace still matches the project:
    project.reload.project_namespace.reload.parent_id == project.namespace_id # => true (false before this MR)
  3. To check a failure after the commit, raise from a post-commit step instead and run the worker again. The transfer completes, and the worker neither raises nor creates a "transfer failed" to-do:
    Projects::TransferService.prepend(Module.new { def post_update_hooks(*) = raise(StandardError, 'boom') })
    Projects::TransferWorker.new.perform(project.id, target.id, user.id)
    project.reload.namespace == target # => true

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 Aakriti Gupta

Merge request reports

Loading
Loading