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-11-24 15:15:51 +05:30
|
|
|
# Close existing MRs coming from the project and remove it from the fork network
|
2016-06-02 11:05:42 +05:30
|
|
|
def execute
|
2020-01-01 13:55:28 +05:30
|
|
|
fork_network = @project.fork_network
|
2020-11-24 15:15:51 +05:30
|
|
|
forked_from = @project.forked_from_project
|
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
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
log_info(message: "UnlinkForkService: Unlinking fork network", fork_network_id: fork_network.id)
|
|
|
|
|
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|
|
2021-06-08 01:23:25 +05:30
|
|
|
::MergeRequests::CloseService.new(project: @project, current_user: @current_user).execute(mr)
|
2021-03-08 18:12:59 +05:30
|
|
|
log_info(message: "UnlinkForkService: Closed merge request", merge_request_id: mr.id)
|
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
|
2021-04-01 16:36:13 +05:30
|
|
|
|
|
|
|
@project.leave_pool_repository
|
2020-01-01 13:55:28 +05:30
|
|
|
end
|
|
|
|
|
2021-03-08 18:12:59 +05:30
|
|
|
# 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
|
|
|
|
|
2020-01-01 13:55:28 +05:30
|
|
|
# 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-11-24 15:15:51 +05:30
|
|
|
[forked_from, @project].compact.each do |project|
|
2020-01-01 13:55:28 +05:30
|
|
|
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
|
2016-06-02 11:05:42 +05:30
|
|
|
end
|
|
|
|
end
|
2023-01-13 00:05:48 +05:30
|
|
|
|
|
|
|
Projects::UnlinkForkService.prepend_mod_with('Projects::UnlinkForkService')
|