Skip to content

Errors creating fork relationship between 2 projects that have forks

This happens when trying to create a fork relationship between 2 projects that already have forks, consider these fork networks:

graph TD
  subgraph fork network 1
  gitlab-->gitlab-f1
  gitlab-->gitlab-f2
  gitlab-->gitlab-f3
  end
  subgraph fork network 2
  gitlab'-->gitlab'-f1
  gitlab'-->gitlab'-f2
  end

If the owner of gitlab' wants to create the for relationship to gitlab using the API: https://docs.gitlab.com/ee/api/projects.html#fork-relationship

This will currently cause an error.

Proposal:

Join the fork networks of both projects when this happens, this means that fork network 2 gets deleted, and its members join fork network 1:

graph TD
  subgraph fork network 1
  gitlab-->gitlab-f1
  gitlab-->gitlab-f2
  gitlab-->gitlab-f3
  gitlab-->gitlab'
  gitlab'-->gitlab'-f1
  gitlab'-->gitlab'-f2
  end

https://sentry.gitlab.net/gitlab/gitlabcom/issues/2391758/?referrer=gitlab_plugin

ActiveRecord::RecordNotSaved: Failed to remove the existing associated fork_network_member. The record failed to save after its foreign key was set to nil.
  active_record/associations/has_one_association.rb:91:in `remove_target!'
    raise RecordNotSaved, "Failed to remove the existing associated #{reflection.name}. " \
  active_record/associations/has_one_association.rb:52:in `block in replace'
    remove_target!(options[:dependent]) if target && !target.destroyed? && assigning_another_record
  active_record/associations/has_one_association.rb:105:in `transaction_if'
    yield
  active_record/associations/has_one_association.rb:51:in `replace'
    transaction_if(save) do
  active_record/associations/has_one_association.rb:75:in `set_new_record'
    replace(record, false)
...
(158 additional frame(s) were not displayed)
Edited by Bob Van Landuyt