2018-07-17 12:50:37 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-03-22 10:34:35 -04:00
|
|
|
module Projects
|
|
|
|
class UnlinkForkService < BaseService
|
2020-09-15 08:09:30 -04:00
|
|
|
# Close existing MRs coming from the project and remove it from the fork network
|
2016-03-22 10:34:35 -04:00
|
|
|
def execute
|
2019-11-28 10:06:57 -05:00
|
|
|
fork_network = @project.fork_network
|
2020-09-15 08:09:30 -04:00
|
|
|
forked_from = @project.forked_from_project
|
2016-03-22 10:34:35 -04:00
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
return unless fork_network
|
2017-11-03 06:31:29 -04:00
|
|
|
|
2021-01-07 13:10:38 -05:00
|
|
|
log_info(message: "UnlinkForkService: Unlinking fork network", fork_network_id: fork_network.id)
|
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
merge_requests = fork_network
|
2017-11-03 06:31:29 -04:00
|
|
|
.merge_requests
|
|
|
|
.opened
|
2019-11-28 10:06:57 -05:00
|
|
|
.from_and_to_forks(@project)
|
2016-03-22 10:34:35 -04:00
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
merge_requests.find_each do |mr|
|
2021-05-11 23:10:21 -04:00
|
|
|
::MergeRequests::CloseService.new(project: @project, current_user: @current_user).execute(mr)
|
2021-01-07 13:10:38 -05:00
|
|
|
log_info(message: "UnlinkForkService: Closed merge request", merge_request_id: mr.id)
|
2016-03-22 10:34:35 -04:00
|
|
|
end
|
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
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
|
2021-03-31 14:09:19 -04:00
|
|
|
|
|
|
|
@project.leave_pool_repository
|
2019-11-28 10:06:57 -05:00
|
|
|
end
|
|
|
|
|
2021-01-07 13:10:38 -05:00
|
|
|
# rubocop: disable Cop/InBatches
|
|
|
|
Project.uncached do
|
|
|
|
@project.forked_to_members.in_batches do |fork_relation|
|
|
|
|
fork_relation.pluck(:id).each do |fork_id| # rubocop: disable CodeReuse/ActiveRecord
|
|
|
|
log_info(message: "UnlinkForkService: Unlinked fork of root_project", project_id: @project.id, forked_project_id: fork_id)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
# rubocop: enable Cop/InBatches
|
|
|
|
|
2019-11-28 10:06:57 -05:00
|
|
|
# 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.
|
2020-09-15 08:09:30 -04:00
|
|
|
[forked_from, @project].compact.each do |project|
|
2019-11-28 10:06:57 -05:00
|
|
|
refresh_forks_count(project)
|
|
|
|
end
|
2016-03-22 10:34:35 -04:00
|
|
|
end
|
2019-11-28 10:06:57 -05:00
|
|
|
|
|
|
|
private
|
2017-08-14 09:22:09 -04:00
|
|
|
|
|
|
|
def refresh_forks_count(project)
|
|
|
|
Projects::ForksCountService.new(project).refresh_cache
|
|
|
|
end
|
2016-03-22 10:34:35 -04:00
|
|
|
end
|
|
|
|
end
|