2018-11-18 11:00:15 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-06-02 11:05:42 +05:30
|
|
|
module Projects
|
|
|
|
class UnlinkForkService < BaseService
|
2020-01-01 13:55:28 +05:30
|
|
|
# If a fork is given, it:
|
|
|
|
#
|
|
|
|
# - Saves LFS objects to the root project
|
|
|
|
# - Close existing MRs coming from it
|
|
|
|
# - Is removed from the fork network
|
|
|
|
#
|
|
|
|
# If a root of fork(s) is given, it does the same,
|
|
|
|
# but not updating LFS objects (there'll be no related root to cache it).
|
2016-06-02 11:05:42 +05:30
|
|
|
def execute
|
2020-01-01 13:55:28 +05:30
|
|
|
fork_network = @project.fork_network
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
return unless fork_network
|
2018-03-17 18:26:18 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
save_lfs_objects
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
merge_requests = fork_network
|
2018-03-17 18:26:18 +05:30
|
|
|
.merge_requests
|
|
|
|
.opened
|
2020-01-01 13:55:28 +05:30
|
|
|
.from_and_to_forks(@project)
|
2016-06-02 11:05:42 +05:30
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
merge_requests.find_each do |mr|
|
2017-09-10 17:25:29 +05:30
|
|
|
::MergeRequests::CloseService.new(@project, @current_user).execute(mr)
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
Project.transaction do
|
|
|
|
# Get out of the fork network as a member and
|
|
|
|
# remove references from all its direct forks.
|
|
|
|
@project.fork_network_member.destroy
|
|
|
|
@project.forked_to_members.update_all(forked_from_project_id: nil)
|
|
|
|
|
|
|
|
# The project is not necessarily a fork, so update the fork network originating
|
|
|
|
# from this project
|
|
|
|
if fork_network = @project.root_of_fork_network
|
|
|
|
fork_network.update(root_project: nil, deleted_root_project_name: @project.full_name)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# When the project getting out of the network is a node with parent
|
|
|
|
# and children, both the parent and the node needs a cache refresh.
|
|
|
|
[@project.forked_from_project, @project].compact.each do |project|
|
|
|
|
refresh_forks_count(project)
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
|
|
|
private
|
2017-09-10 17:25:29 +05:30
|
|
|
|
|
|
|
def refresh_forks_count(project)
|
|
|
|
Projects::ForksCountService.new(project).refresh_cache
|
|
|
|
end
|
2020-01-01 13:55:28 +05:30
|
|
|
|
2020-03-09 13:42:32 +05:30
|
|
|
# TODO: Remove this method once all LfsObjectsProject records are backfilled
|
|
|
|
# for forks.
|
|
|
|
#
|
|
|
|
# See https://gitlab.com/gitlab-org/gitlab/issues/122002 for more info.
|
2020-01-01 13:55:28 +05:30
|
|
|
def save_lfs_objects
|
|
|
|
return unless @project.forked?
|
|
|
|
|
|
|
|
lfs_storage_project = @project.lfs_storage_project
|
|
|
|
|
|
|
|
return unless lfs_storage_project
|
|
|
|
return if lfs_storage_project == @project # that project is being unlinked
|
|
|
|
|
|
|
|
lfs_storage_project.lfs_objects.find_each do |lfs_object|
|
|
|
|
lfs_object.projects << @project unless lfs_object.projects.include?(@project)
|
|
|
|
end
|
|
|
|
end
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|